Hey everyone,
New to Processing (and non-web programming) and having a slight issue with displaying matching values with text() on the screen as I search through an array.
The search works correctly, and println(); correctly identifies the match positions within the array. However, the text(); function within the main draw(); loop is not working as expected.
First, if there are no matches then the last element in the array - a_Random6[5] - does not get shown at all.
Secondly, if the match is found with any position and the last position, the text is displayed correctly and clearly in green. If the match is between any other two positions - a_Random6[0 thru 4] - then the green gets overwritten as the while and for loops continue.
I could make another array as a cache, store the positions of the matches in it, and then print all the values from the main array at once after all the loops are done. I’m just not sure if this would be the best way, or if there is a better way to draw the text while staying within the loops.
Screenshot links are below the mcve (at least I think it’s minimal… if you see something that can be better minimized feel free to mention it).
I’d prefer answers in pseudocode (better learning if I actually write the code ), but if you use any functions beyond what is already in the code please specify the exact function name - I can use the Reference from there. Lastly, I am looking to stay within native Processing functions, not pulling in native Java or Processing libraries (even though I know that would open doors to FAR more efficient code).
Thanks!
int a_Random6[] = new int[6];
int i_Step,i_Index,i_centerX,i_centerY;
void setup(){
frameRate(30);
size(800,400);
i_centerX = width/2; //let's figure out exactly where we are!
i_centerY = height/2;
//Fill our array with random values to start
for (int i=0; i<6; i++) {
a_Random6[i] = int(random(1,49));
};
};
void draw(){
background(#000000);
textSize(40);
//use 2x for() loops to draw 1x6 grid
stroke(#999999);
strokeWeight(3);
fill(#999999);
for(int i_VertCount=0;i_VertCount<7;i_VertCount+=1){
line((i_centerX-270)+(90*i_VertCount),i_centerY+45,(i_centerX-270)+(90*i_VertCount),i_centerY-45);
};
for(int i_HorizCount=0;i_HorizCount<2;i_HorizCount+=1){
line(i_centerX-270,(i_centerY-45)+(90*i_HorizCount),(i_centerX+270),(i_centerY-45)+(90*i_HorizCount));
};
textSize(24);
textAlign(CENTER,CENTER);
if(i_Step==0){
//output element values to output window inside the 1x6 graphic
for (int i_TextPos1=0;i_TextPos1<6;i_TextPos1+=1){
text(a_Random6[i_TextPos1],((i_centerX-315)+(90*(i_TextPos1+1))),i_centerY);
};
}
if(i_Step==1){
//start with a full grid of constantly changing random numbers
for(int i_Loop=0;i_Loop<6;i_Loop=i_Loop+1){
a_Random6[i_Loop] = int(random(1,49));
fill(#444444);
text(a_Random6[i_Loop],((i_centerX-315)+(90*(i_Loop+1))),i_centerY);
};
delay(50);
}
if(i_Step==2){
//freeze the random integers, and show all and mark matches
for(int i_Loop=0;i_Loop<a_Random6.length;i_Loop+=1){
fill(#444444);
i_Index=i_Loop+1;
while(i_Index<a_Random6.length){
if(a_Random6[i_Loop]==a_Random6[i_Index]){
fill(#159915);
text(a_Random6[i_Index],((i_centerX-225)+(90*(i_Index))),i_centerY);
text(a_Random6[i_Loop],((i_centerX-225)+(90*(i_Loop))),i_centerY);
println("Integers in array position "+i_Index+" & "+i_Loop+" match. The value is:"+a_Random6[i_Index]);
}else{
fill(#444444);
text(a_Random6[i_Loop],((i_centerX-225)+(90*(i_Loop))),i_centerY);
};
i_Index+=1;
};
};
noLoop();
}
};
void mousePressed(){
//is mouse button clicked the LEFT one? If yes increment i_Step.
if(mouseButton==LEFT&&i_Step<2){i_Step+=1;};
//is mouse button clicked the RIGHT one? If yes decrement i_Step.
if(mouseButton==RIGHT&&i_Step<3&&i_Step>1){i_Step-=1;loop();};
};