TwoPlayerGameOfLife/theGameOfLife.pde
2022-05-15 17:24:15 +02:00

219 lines
5.6 KiB
Plaintext

boolean stop = false, iteration = false, sandbox = false;
String turn = "blue", lost = "";
int count = 1, sz = 20, prev = 0;
PVector latest;
GOL gol;
void setup() {
fullScreen();
sz = height/16;
//size(1050, 800);
//orientation(LANDSCAPE);
int many = 20;
int scale = height/many;//40;
gol = new GOL(0.20, scale, width-(width-(scale*many)), height);
//int i = round((gol.wdt/scale)/2), j = round((gol.hgt/scale)/2);
//for (int a = 0; a < 5; a++) {
// int g = i, h = j;
// if (a == 0) {
// h+=1;
// } else if (a == 1) {
// g-=1;
// } else if (a == 2) {
// h-=1;
// } else if (a == 3) {
// h-=1;
// g+=1;
// }
// gol.cells[g][h].col = 1;
//}
//int i = round((gol.wdt/scale)/2), j = round((gol.hgt/scale)/2);
//gol.cells[i][j].col = 0;
//frameRate(10);
//int i = round((width/scale)/2), j = round((height/scale)/2);
//for (int a = 0; a < 4; a++) {
// int g = i, h = j;
// if (a == 0) {
// g-=1;
// h-=1;
// } else if (a == 1) {
// g+=1;
// h-=1;
// } else if (a == 2) {
// g+=1;
// h+=1;
// } else if (a == 3) {
// g-=1;
// h+=1;
// }
// gol.cells[g][h].col = 1;
//}
//for (int b = 1; b < 11; b++) {
// PVector cell = new PVector(round(random(6,width/scale-7)), round(random(6,height/scale-7)));
// for (int a = 0; a < 13; a++) {
// int g = int(cell.x), h = int(cell.y);
// if (a == 0) {
// g+=1;
// h-=1;
// } else if (a == 1) {
// g+=2;
// h-=1;
// } else if (a == 2) {
// g+=3;
// h-=1;
// } else if (a == 3) {
// g+=4;
// h-=1;
// } else if (a == 4) {
// g+=5;
// h-=1;
// } else if (a == 5) {
// g+=6;
// h-=1;
// } else if (a == 6) {
// g+=6;
// } else if (a == 7) {
// g+=6;
// h+=1;
// } else if (a == 8) {
// g+=5;
// h+=2;
// } else if (a == 9) {
// g+=3;
// h+=3;
// } else if (a == 10) {
// g+=2;
// h+=3;
// } else if (a == 11) {
// h+=2;
// }
// if (gol.cells[g][h].col == 0) {
// gol.cells[g][h].col = 1;
// } else {
// gol.cells[g][h].col = 0;
// }
// }
//}
//println("Resolution: "+gol.wdt/gol.scl+"x"+gol.hgt/gol.scl);
textSize(sz);
}
void draw() {
fill(255);
stroke(255);
rect(gol.wdt, 0, width-gol.wdt, height);
fill(255, 0, 0);
text("Red alive Cells: "+gol.redd, gol.wdt+10, 10+sz);
fill(0, 0, 255);
text("Blue alive Cells: "+gol.bluee, gol.wdt+10, 2*(10+sz));
if (turn == "red") {
fill(255, 0, 0);
} else if (turn == "blue") {
fill(0, 0, 255);
} else {
fill(0);
}
text("It's "+turn+"'s turn.", gol.wdt+10, 4*(10+sz));
noFill();
stroke(0);
strokeWeight(2);
rect(gol.wdt+10, 5*(10+sz), sz*4+30, sz+10);
if (sandbox == true) {
rect(gol.wdt+10, 6*(10+sz)+10, sz*4+120, sz);
}
rect(gol.wdt+10, 7*(10+sz)+10, sz*4+220, sz);
rect(gol.wdt+10+sz*4+10+30, 5*(10+sz), sz*5+35, sz+10);
strokeWeight(1);
fill(0);
text("Continue", gol.wdt+15, 6*(10+sz)-10);
if (sandbox == true) {
text("Switch Turn", gol.wdt+15, 7*(10+sz)-10);
}
text("Sandbox Mode", gol.wdt+15, 8*(10+sz)-10);
text("Undo Move", gol.wdt+15+sz*4+10+10+20, 6*(10+sz)-10);
gol.calculate(stop);
if (stop == true) {
stop = false;
if (turn == "blue" && (lost == "" || lost == "blue")) {
turn = "red";
} else if (turn == "red" && (lost == "" || lost == "red")) {
turn = "blue";
}
}
//println(frameRate);
//stop = !stop;
}
void mousePressed() {
if (mouseX <= gol.wdt) {
if (iteration == false) {
latest = new PVector(round(mouseX/gol.scl), round(mouseY/gol.scl));
gol.mouse(gol.cells, latest, false);
if (sandbox == false) {
iteration = true;
}
}
} else {
PVector pos1 = new PVector(gol.wdt+10, 5*(10+sz));
PVector w1 = new PVector(sz*4+30, sz+10);
PVector pos2 = new PVector(gol.wdt+10+sz*4+10+30, 5*(10+sz));
PVector w2 = new PVector(sz*5+35, sz+10);
PVector pos3 = new PVector(gol.wdt+10, 7*(10+sz)+10);
PVector w3 = new PVector(sz*4+220, sz);
PVector pos4 = new PVector(gol.wdt+10, 6*(10+sz)+10);
PVector w4 = new PVector(sz*4+120, sz);
boolean skip = false;
if (mouseX >= pos1.x && mouseX <= pos1.x+w1.x) {
if (mouseY >= pos1.y && mouseY <= pos1.y+w1.y) {
stop = !stop;
skip = true;
}
}
//if (skip == false) {
if (mouseX >= pos2.x && mouseX <= pos2.x+w2.x) {
if (mouseY >= pos2.y && mouseY <= pos2.y+w2.y) {
if (sandbox == false) {
if (iteration == true) {
if (gol.cells != gol.old) {
//gol.cells = gol.old;
gol.mouse(gol.cells, latest, true);
iteration = false;
} else {
println("Error?!");
}
}
} else {
if (gol.cells != gol.old) {
//gol.cells = gol.old;
gol.mouse(gol.cells, latest, true);
iteration = false;
} else {
println("Error?!");
}
}
}
}
//}
if (sandbox == true) {
if (mouseX >= pos4.x && mouseX <= pos4.x+w4.x) {
if (mouseY >= pos4.y && mouseY <= pos4.y+w4.y) {
if (turn == "blue") {
turn = "red";
} else if (turn == "red") {
turn = "blue";
}
}
}
}
if (mouseX >= pos3.x && mouseX <= pos3.x+w3.x) {
if (mouseY >= pos3.y && mouseY <= pos3.y+w3.y) {
sandbox = !sandbox;
//iteration = false;
}
}
}
}