Array List Question
in
Programming Questions
•
1 year ago
Below I have pasted the code for the start of a memory game I am making. Basically I have two arrayLists one for CompInput and one for Player Input. I want to check to make sure the the array Sizes are the same then make sure they are identical before moving onto the next round. However i get errors when using the .get(i) function in statements to call my array items. What is a way around this.
PFont f;
ArrayList playerInput;
ArrayList compInput;
int compSize;
int playerSize;
float r,g,b; //R1
float r2,g2,b2; //R2
float r3,g3,b3; //R3
float r4,g4,b4; //R4
int counter;
boolean start;
float timer;
void setup(){
size(600,600);
smooth();
background(255);
f = createFont("Arial",16,true);
playerInput = new ArrayList();
compInput = new ArrayList();
r = 255; g = 0; b = 0;//RECT 1 RED
r2 = 0; g2 = 255; b2 = 0;//RECT 2 GREEN
r3 = 0; g3 = 0; b3 = 255;//RECT 2 BLUE
r4 = 255; g4 = 255; b4 = 0;//RECT 2 GREEN
timer = 500;
start = false;
counter = 0;
}
void draw(){
background(255);
textFont(f);
fill(0);
textAlign(CENTER);
text("timer: "+timer,width/2,60);
compSize = compInput.size();
playerSize = playerInput.size();
if(start == true){
timer -= 2;
if(compSize == playerSize){
if(int(playerInput.get(counter-1)) = compInput.get(counter-1)) <<<<<<<<<<<<< RIGHT HERE
{
println("CORRECT!");
}
}
}
//IF NO KEYS ARE PRESSED RECTS HAVE NORMAL COLORS
if(keyPressed == false){
r = 255; g = 0; b = 0;//RECT 1 RED
r2 = 0; g2 = 255; b2 = 0;//RECT 2 GREEN
r3 = 0; g3 = 0; b3 = 255;//RECT 3 BLUE
r4 = 255; g4 = 255; b4 = 0;//RECT 4 YELLOW
}
if(timer <= -2){
println("YOU LOSE!");
noLoop();
}
translate(90,225);
pushMatrix();
fill(r,g,b);
rect(0,0,100,100);
popMatrix();
pushMatrix();
fill(r2,g2,b2);
rect(110,0,100,100);
popMatrix();
pushMatrix();
fill(r3,g3,b3);
rect(220,0,100,100);
popMatrix();
pushMatrix();
fill(r4,g4,b4);
rect(330,0,100,100);
popMatrix();
}
void keyPressed(){
if(timer < 500){ //ONLY REGISTER KEYS IF GAME STARTS
//KEY 1 FUNCTIONALITY
if(key == '1'){
r = 200; g = 10; b = 0;
playerInput.add('1');
delay(100);
counter += 1;
println(counter);
}
//KEY 2 FUNCTIONALITY
if(key == '2'){
r2 = 10; g2 = 200; b2 = 0;
playerInput.add('2');
delay(100);
counter += 1;
println(counter);
}
//KEY 3 FUNCTIONALITY
if(key == '3'){
r3 = 0; g3 = 10; b3 = 200;
playerInput.add('3');
delay(100);
counter += 1;
println(counter);
}
//KEY 4 FUNCTIONALITY
if(key == '4'){
r4 = 200; g4 = 200; b4 = 10;
playerInput.add('4');
delay(100);
counter += 1;
println(counter);
}
} //END OF IF STATEMENT
if(key == 's'){
start = true;
counter = 0;
;
compInput.add( int( random(1.0,5.0) ) );
println("CompInput ="+compInput);
}
if(key == 'z'){
println("PlayerInput ="+playerInput);
println(playerInput.get(counter-1));
println(playerInput.size());
//println(compArray);
}
}
1