Randomize String Array And Display All Of The Values
Solution 1:
Getting a blank button or just with "Masons" or "Villager" String
That is because you only set the Button's text with the last element of the list. Which is either null
or "Masons"
(not seeing how it could be "Villager"
).
for (int s = 1; s < characters.length; s++)
{
randomButton.setText(characters[s]);
}
If I check the log I see that all the others cards(characters) get displayed as "null"
You only set position 0 of your array. For example, you don't initialize the positions, so these int
values default to 0.
int demoniac;
int guard;
intall;
Then
for(i = 0; i < all; i++){
add(demoniac, "Demoniac");
add(guard, "Guard");
Really, that loop shouldn't be entered because all
equals 0.
Additionally
Collections are zero-indexed, so this doesn't print element 0. You need to set int s = 0;
.
for (int s = 1; s < characters.length; s++)
It isn't clear to me what the add(int character, String name)
method is returning, but if you explain it, I will update this answer.
I believe this code fulfills most of what you are trying to achieve
// Where the characters are storedprivate ArrayList<String> characters;
publicvoidinitDeck() {
if (characters == null)
characters = new ArrayList<String>();
// Extract the numbers if you actually need them, otherwise, they just are constants
addCharacter("Demoniac", 1, characters);
addCharacter("Guard", 1, characters);
addCharacter("Medium", 1, characters);
addCharacter("Mythomaniac", 1, characters);
addCharacter("Owl", 1, characters);
addCharacter("Werehamster", 1, characters);
addCharacter("Villager", 5, characters);
addCharacter("Masons", 1, characters);
}
publicvoidaddCharacter(String name, int amount, ArrayList<String> cards) {
if (amount < 0) {
thrownew IllegalArgumentException("Must add a non-negative number of characters for " + name);
}
// Don't use '==' for Stringsif (name.equals("Villager")) {
if (amount != 5 || amount != 12) {
thrownew IllegalArgumentException("There can only be 5 or 12 " + name);
}
}
for (int i = 0; i < amount; i++) {
cards.add(name);
}
}
publicintsearchCharacters(String character, ArrayList<String> cards) {
return cards.indexOf(character);
}
public Map<String, Integer> getAllCharacterPositions() {
Map<String, Integer> allPositions = new LinkedHashMap<String, Integer>();
for (int i = 0; i < characters.size(); i++) {
allPositions.put(characters.get(i), i);
}
return allPositions;
}
voidrun() {
// initialize the characters
initDeck();
// shuffle them
Collections.shuffle(characters);
// print them all outfor (int i = 0; i < characters.size(); i++) {
System.out.printf("%d: %s\n", i, characters.get(i));
}
// Find the position of a character
System.out.println();
String findCharacter = "Owl";
// Option 1 -- always linear search lookup
System.out.printf("%d: %s\n", searchCharacters(findCharacter, characters), findCharacter);
// Option 2 -- one-time linear scan, constant lookup
Map<String, Integer> positions = getAllCharacterPositions();
System.out.printf("%d: %s\n", positions.get(findCharacter), findCharacter);
// Get a random character
System.out.println();
Random rand = new Random(System.currentTimeMillis());
int randPos = rand.nextInt(characters.size());
System.out.printf("%d: %s\n", randPos, characters.get(randPos));
// randomButton.setText(characters.get(randPos));
}
Solution 2:
Given the array is already shuffled, just look at the first card:
publicvoidshow(View view){
randomButton.setText(characters[0]);
}
If you want to navigate that deck I suggest you put the shuffled list in to a Queue
, where you can look at the next card (peek
) or take the next card (poll
):
privatestaticQueue<string> buildNewShuffledDeck(String[] characters){
List<String> shuffledCharacterList = newArrayList<String>(characters);
Collections.shuffle(shuffledCharacterList);
Queue<string> deck = newArrayDeque(shuffledCharacterList);
return deck;
}
publicvoidshow(View view){
String nextCard = deck.peek();
if (nextCard != null)
randomButton.setText(nextCard);
else//deck is empty...
}
Then to take from the deck, say on the random button click:
StringnextCard= deck.poll();
General advice on arrays: Stop using them in favor of other data types that are far more useful and interchangeable.
Then next step advice, make a class that represents a Card
and stop using Strings
, the string you currently have is just one property of a card.
Solution 3:
You are just displaying the last character name that you add Replace with this
publicvoidshow(View view){
Randomr=newRandom(System.currentTimeMillis());
randomButton.setText(characters[r.nexInt(characters.length)])
}
Post a Comment for "Randomize String Array And Display All Of The Values"