mardi 29 septembre 2015

unable to generate random words from SQLite database in android

Background: Every time the user starts a new game with another player, he is given 4 words that are taken from the SQLite database, which remain in the current game activity until the game is finished. If the user has multiple games, each of those games will have a different set of words.

problem: I am able to generate a random set of words, but the problem is that my app generates the set of words only once, and this set is used in all the games of the current user, and any other user I log in as.

Here is my code:

  public class WordsListDatabaseHelper extends SQLiteOpenHelper {

protected static Set<String> mSelectedWords;
private static final String DB_NAME = "GAME_TABLE";
private static final int DB_VERSION = 1;
protected static LinkedList<String> mCopy;
public static int gameNumber = 0;


WordsListDatabaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE GAME ("
                    + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + "SELECTED_WORDS TEXT, "
                    + "GAME_NUMBER INTEGER);"
    );



    Collection<String> wordList = new LinkedList<String>();
    mSelectedWords = new LinkedHashSet<String>();

    wordList.add("ant");
    wordList.add("almond");
      //lots more words

    mCopy = new LinkedList<String>(wordList);

    Gson gson = new Gson();
    String wordsContainer = gson.toJson(mCopy);
    insertWordList(db, wordsContainer, gameNumber);

}

private static void insertWordList(SQLiteDatabase db, String wordsContainer, int gameNumber) {
    ContentValues wordValues = new ContentValues();

    Gson gson = new Gson();
    Collections.shuffle(mCopy);
    mSelectedWords.addAll(mCopy.subList(1,7));
    wordsContainer = gson.toJson(mSelectedWords);

    wordValues.put("SELECTED_WORDS", wordsContainer);
    wordValues.put("GAME_NUMBER", gameNumber);
    db.insert("GAME", null, wordValues);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

}

Here is the relevant code from FindingOpponentsActivity, which randomly matches the user with an opponent. When the match is created, the user is taken to StartGameActivity.

  Intent intentRand = new Intent(FindingOpponentActivity.this, StartGameActivity. .class);
                    int gameNum = WordsListDatabaseHelper.gameNumber;
                    intentRand.putExtra(
                            StartGameActivity.EXTRA_RAND_OPPONENT,
                            mOpponent.getObjectId());

                    intentRand.putExtra(
                            StartGameActivity.EXTRA_GAME_NUMBER,
                            gameNum);

                    sendPushNotification();
                    startActivity(intentRand);
                }

And now, this is the code in the onCreate() method of StartGameActivity:

  Intent intent = getIntent();
    mGameNum = intent.getIntExtra(EXTRA_GAME_NUMBER, 0);


    //create a cursor

    try {
        SQLiteOpenHelper wordsListDatabaseHelper = new WordsListDatabaseHelper(this);
        SQLiteDatabase db = wordsListDatabaseHelper.getReadableDatabase();
        Cursor cursor = db.query("GAME",
                new String[] {"SELECTED_WORDS"},
                "GAME_NUMBER =? ",
                new String[]{Integer.toString(mGameNum)},
                null, null, null);

        //move to the first record in the Cursor

        if (cursor.moveToFirst()) {
            //get wordList
            String savedWordList  = cursor.getString(0);

            Gson gson = new Gson();
            Type type = new TypeToken<ArrayList<String>>() {}.getType();
            ArrayList<String> finalWordList = gson.fromJson(savedWordList, type);
            mCopy = new LinkedList<String>();
            mCopy.addAll(finalWordList);


            word1 = (TextView) findViewById(R.id.word1);
            word1.setText(mCopy.get(1));

            word2 = (TextView) findViewById(R.id.word2);
            word2.setText(mCopy.get(2));

What I want is that every time a new game begins, the table has a new entry: a new GAME_NUMBER and wordsContainer (which contains the random words). Thus, each game of the user has a unique identifier (GAME_NUMBER). I think what I am doing wrong is not creating a new entry every time a new game begins, and this is what's causing the same list to appear, but I am totally lost as to how to implement the solution.

Aucun commentaire:

Enregistrer un commentaire