I'm a beginner with Java:
for (int i = 0; i <= 4; i++) {
taken = false;
spun = wheel.spinWheel();
for (PrizeCard a : playerOne) {
if (spun.getID() == a.getID()) {
taken = true;
}
}
if (!taken) {
playerOne.add(spun);
} else {
i--;
}
}
For this code I am trying to pick out objects from another arraylist randomly and add it to the arraylist known as playerOne. However, the arraylist playerOne cannot hold the same object again (each object different by thier ID number, that is what the method .getID() returns).
So what I have believe should work, spinWheel() is the method that randomly returns an object, then it goes through each object and checks it's ID with previous one, if it is the same, it will not add the object and spin one more because I subtracted one from the variable i.
Any help would be greatly appreciated, thanks!
Clarification if needed:
If I have an arraylist named holder:
["Hello", "Dinosaur", "Chicken"]
And another arraylist named list2:
[]
How would I make it so I randomly get values from holder, add it to list2, but make sure that I only add it if it is not already added.
For instance, if I add "Chicken", it will never add "Chicken" into list 2 again.
Here is the other methods:
import java.util.*;
import java.lang.*;
public class GameWheel {
private ArrayList<PrizeCard> prizeCards;
private int currentPos;
public GameWheel() {
prizeCards = new ArrayList<PrizeCard>();
currentPos = 0;
prizeCards = initGameWheel();
prizeCards = scramble();
}
public ArrayList<PrizeCard> initGameWheel() {
ArrayList<PrizeCard> init = new ArrayList<PrizeCard>();
for (int i=1; i <= 40; i++) {
if (i%2 == 1)
init.add(new PrizeCard(i, "red", i*10));
else if (i%10 == 0)
init.add(new PrizeCard(i, "black", i*200));
else
init.add(new PrizeCard(i, "blue", i*100));
}
return init;
}
public ArrayList<PrizeCard> getPrizeCards() {
return prizeCards;
}
public PrizeCard spinWheel() {
int power = (int)(Math.random()*100 + 1);
int newPos = (currentPos + power) % prizeCards.size();
currentPos = newPos;
return prizeCards.get(currentPos);
}
}
public class PrizeCard {
private int id;
private String color;
private int prizeAmount;
public PrizeCard() {
id = 0;
color = "red";
prizeAmount = 0;
}
public PrizeCard(int nID, String nColor, int nPrizeAmount) {
id = nID;
color = nColor;
prizeAmount = nPrizeAmount;
}
public int getID() {
return id;
}
public int getPrizeAmount() {
return prizeAmount;
}
public String toString() {
return "ID: " + id +
", Color: " + color +
", Prize Amount: $" + prizeAmount;
}
}