Android Studio - Array, ArrayList, List - JAVA

I am making a fortune-telling game for my school project, but I am completely new to Android Studio and Java.

At the moment, my code looks like this:

public class MainActivity extends AppCompatActivity {

    public int score = 0;
    int random = random();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button correct = (Button) findViewById(R.id.correct);
        Button other = (Button) findViewById(R.id.other);
        Button newGame = (Button) findViewById(R.id.newGame);
        TextView words = (TextView) findViewById(R.id.words);


    }

    public Integer random (){
        int random = (int )(Math.random() * 5);
        return random;
    }

    private String list[] =
            {"Dog", "Cat", "Mouse", "Elephant", "Rat", "Parrot"};

    public void clickedButton(View view) {
        TextView words = (TextView) findViewById(R.id.words);
        if (view.getId()== R.id.newGame)
        {
            words.setText(list[random]);
            score = 0;
            Log.i("score", "score = " + score);
        }
        if (view.getId() == R.id.correct)
        {
            // set maybe new array so new textview does not give the same value
            words.setText(list[random]);
            score = score +1;
            Log.i("score", "score = " + score);
        }
        if (view.getId() == R.id.other)
        {
            // set maybe new array so new textview does not give the same value
            words.setText(list[random]);
            Log.i("score", "score = " + score);
        }

    }

}

The idea is simple. I have an array with (at the moment) 6 words in it. Since I run the game, it gives me a random word on the screen that I have to describe to others. If they guess what is right, I’ll return, I will get another word, if I can’t go and get another word ... Well, the problem that I see is the chance that I will get the same word again. So I thought that there should be a way to solve this problem.

I was thinking of adding as if statements like index 1 or 2 or 3 then gave another word, but if I had 100 words, it would be a monkey job to do this.

, , , . , .

, , Array? , , , ?

+4
4

ArrayList ( ):

List<String> list;

ArrayList, :

list = new ArrayList<String>();
list.add("Dog");
list.add("Cat");
...
//or alternatively list.addAll(Arrays.asList(normalArray)); if you have your data in array

Collections.shuffle(list,new Random(System.nanoTime()); //randomly shuffles your list

:

int index = 0;

, , index:

words.setText(list.get(index++));

+5

, , ( ), index , , .

, int times, , :

// ...

// Random Shuffle
// Tip: Use this method to shuffle, may not be the fastest (not noticeable however), but it makes your array totally randomized.
public static void totalRandom(String[] array, int times)
{
    // Random Initialize
    Random randomGenerator = new Random();

    // Variables
    int first = 0;
    int second = 0;

    // Swap
    for (int i = 0; i < times; i++)
    {
        // Generates number from 0 to array length
        first = randomGenerator.nextInt(array.length);
        second = randomGenerator.nextInt(array.length);
        String word = array[second];
        array[second] = array[first];
        array[first] = word;
    }
}

// ...

, - ...

//...
int index = 0;

// ...
// Code for user input
// ...

if (userInput.equals(array[index]))
{
    // do stuff
    index++;
}
// ...
+1

. String.

...
public int score = 0;
public String cache; // new

words.setText(list[random]);

. :

...
cache = getRandomWord();
words.setText(cache);
...

:

private String getRandomWord(){
    int random = random();
    String temp = list[random];
    if(temp.equals(cache)){
        temp = getRandomWord();     
    }
    return temp;

}

Just put this method under your clickButton method. Random word required. Whether the word is equal to the previous word, the method will again call itself to select another word. If not, he will return a new word.

No need to drag and drop ArrayLists.

+1
source

I think an easier way is to deduce each displayed text from the copied list.

//when start a new game.
ArrayList<String> currentGameList = YourList.clone();

//when need next word.
String displayText = currentGameList.remove(randomIntInRestSize); //after called remove, you will get the object at list(index), and the object will be removed from the list.

Each game has started, you just need to clone a new list from the original list of words and play with the list.

0
source

Source: https://habr.com/ru/post/1626773/


All Articles