8

I am making a guessing game for as my School project, but I am quite 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);
        }

    }

}

Idea is simple. I have my array with (at the moment) 6 words in it. So as I launch the game it gives me a random word on screen which I have to describe to others. If they guess it right I press correct, I get another word, if not I can pass and get another word... Well the problem I see is that there is chance that I get the same word over again. So I thought there should be a way how to fix this problem.

I thought about adding like if statements like if index 1 or 2 or 3 then give another word but if I had 100 words it would be monkey work to do it.

So I think there should be a chance how I can delete words from like temp. array, but there is no in-built method to do it.

I was reading that there are those arrayLists and lists but would not it be easier to use Array? and maybe some tips how to?

4
  • It's important to pick the right tool for the job. Array is fixed-length, while ArrayList allows you to extend it and (inefficiently) remove entries from the middle. A LinkedList would efficiently allow you to remove any entry, while being able to grow and shrink as necessary. Commented Jan 31, 2016 at 19:02
  • 1
    Personally I would mix the array up (swapping places using random), then just keep an index on what word you are on, so you don't have to track which words you've already went through in the array. Commented Jan 31, 2016 at 19:02
  • @RileyCarney do u mean like - my random is number 3. then I swap [3] and [1] with places. And for next random I just add variable x, which would be 1 and increase after every "correct" or "pass" being pressed? Commented Jan 31, 2016 at 19:07
  • @hexafraction thank you. Will read about LinkedList. Commented Jan 31, 2016 at 19:08

4 Answers 4

13

You can first Create an ArrayList (because it is more flexible):

List<String> list;

Then initialize the object in the constructor and shuffle the ArrayList like below:

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

Then you can keep track of the index of the last item you read:

int index = 0;

And every time you read an item just increase the index:

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

This will do the trick

Sign up to request clarification or add additional context in comments.

4 Comments

I find it weird that if I had 100 words I would have to have 100x list.add would not i?
@osiic21 you can also do like: list.addall(Arrays.asList(normalArray)); it is up to you
Instead of doing list.add("ex"); you could do something such as list = new ArrayList<String>(Arrays.asList({"You say yes", "I say no", "you say why"})); i.e. list = new ArrayList<String>(Arrays.asList(anyArray))
@osiic21 have this helped you solving your problem?
1

While there are a variety of ways solve your issue, personally I like mixing the array up (swapping places using random), then just keep an index on what word you are on in the array, so you don't have to keep track of the words in it.

I wrote a code for a similar program for doing a random shuffle on a deck of cards below, it goes through the int times amount, but you can remove it altogether and have a static loop value if you'd like:

// ...

// 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;
    }
}

// ...

then after in your class you would want to write something along the lines of...

//...
int index = 0;

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

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

Comments

1

You can cache your last word. Add a String member variable.

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

Instead of using

words.setText(list[random]);

you can call a method to request a new word. You also save this word to the cache:

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

Your new method looks like this:

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. It takes a random word. Is the word equal to the previous word, the method will call itself again to pick another word. If not, it will return the new word.

There is no need to shuffle ArrayLists.

3 Comments

But cache would save only 1 word so there is chance that I would get : dog, cat, dog. Am I right?
This works when the user only passes the word once. What happens if he passes the next word again?
Yes your chain is possible.
0

I think a more simple way is pop every shown text out from a 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.

Every game started, you just need to clone a new list from the original word list and play with the list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.