Trying to make a slot machine game, but can't figure out how to check if user won. Help?
Hey everyone, I'm trying to create a simple slot machine game with 5 images and 5 slots. I'm using objects. But I can't figure out how to check if user has won. What I have tried is bolded. Thank you very much.
Slot[] mySlot = new Slot[5]; //Create 5 slots
PImage[] img = new PImage[5];
int[] randPic = new int[5];
int [] countPic = new int [5];
void setup() {
size(1000, 800);
background(0);
for (int i=0; i<img.length; i++) {
img[i] = loadImage( "pic" + i + ".jpg");
}
for (int i = 0; i<5; i++) {
randPic[i] = (int)random(img.length); //Set as random number to pick random image later
//Parameters: X location, Y location, pic, timer, boolean if spinning.:
mySlot[i] = new Slot(width/5*i, 50, randPic[i], 1000, false);
}
}
void draw() {
for (int i=0;i<5;i++) {
mySlot[i].display();
}
}
class Slot {
int xLoc;
int yLoc;
int pic;
float timer;
boolean spin;
Slot(int tXLoc, int tYLoc, int tPic, float tTimer, boolean tSpin ) { //Objects and arguments
xLoc = tXLoc;
yLoc = tYLoc;
pic = tPic;
timer = tTimer;
spin = tSpin;
}
void display() {
if (keyPressed && key == 's') {
for (int i=0;i<5;i++) {
mySlot[i].spin = true; //Make spin true
}
}
for (int i=0;i<5;i++) {
if (!(mySlot[i].spin)) { //If the slot isn't spinning
image(img[randPic[i]], mySlot[i].xLoc, yLoc, width/5, 200); //Display random image
//What I have tried:
//Checks if images are the same, if they are adds onto countPic. Ex. if 5, then all 5 are the same
if (randPic[i]==0)
countPic[i]++;
else if (randPic[i] == 1)
countPic[i]++;
else if (randPic[i]==2)
countPic[i]++;
else if (randPic[i] == 3)
countPic[i]++;
else if (randPic[i] == 4)
countPic[i]++;
}
if (mySlot[i].spin) { //If slot is spinning
if ( mySlot[i].timer > 0) { //If timer is running
mySlot[i].timer=mySlot[i].timer-(i+1)*1; //To stop each one a bit after another
randPic[i] = (int)random(img.length); //Choose a random image
image(img[randPic[i]], mySlot[i].xLoc, yLoc, width/5, 200); //Display that random image
if (mySlot[i].timer == 0) //When timer is done, make spin false
mySlot[i].spin = false;
}
}
}
//What I have tried part 2, checking the count:
for (int i =0; i<5; i++) {
if (countPic[i]==5) //I fcountPic is 5 therefore all are the same
text("win", 10, 10);
else if (countPic[i]==4) //If countPic is 4 therefore 4 are the same
text("small win", 10, 10);
else if (countPic[i]==0) //If countPic is 0 therefore none are the same
text("none are the same, small win", 10, 10);
}
}
}