initial commit

This commit is contained in:
Baipyrus 2022-05-15 17:24:15 +02:00
commit 1cc22a26ab
4 changed files with 474 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
Game/*

75
Cell.pde Normal file
View File

@ -0,0 +1,75 @@
class Cell {
int col;
int dec;
Cell(int c, float possibility, int dd, int cc) {
if (c == 0) {
boolean skip = false;
if (possibility == 0) {
skip = true;
col = 0;
}
if (skip == false) {
if (random(1) >= 1-possibility) {
col = 1;
} else {
col = 0;
}
dec = round(random(1,2));
}
} else if (c == 1) {
col = cc;
dec = dd;
}
}
int aliveCheck(int x, int y, int scl, int w2, int h2) {
int alive = 0, reds = 0, blues = 0;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
int cols = (w2/scl), rows = (h2/scl);
PVector pos = new PVector((x+i+cols)%cols, (y+j+rows)%rows);
if (gol.cells[int(pos.x)][int(pos.y)].col == 1) {
if (gol.cells[int(pos.x)][int(pos.y)].dec == 1) {
blues++;
} else {
reds++;
}
}
alive += gol.cells[int(pos.x)][int(pos.y)].col;
}
}
alive -= col;
if (col == 0 && alive == 3) { // (alive == 1 || alive == 3 || alive == 5 || alive == 7)
//return 4;
if (reds > blues) {
return 3;
} else if (blues > reds) {
return 2;
} else if (blues == reds) {
return 4;
} else {
return 0;
}
} else if (col == 1 && (alive < 2 || alive > 3)) { // alive == 0 || alive == 2 || alive == 4 || alive == 6 ||alive == 8
return 0;
} else {
return col;
}
}
color colCheck() {
color temp = color(0);
if (col == 1) {
if (dec == 1) {
temp = color(0,0,255);
} else if (dec == 2) {
temp = color(255,0,0);
} else if (dec == 3) {
temp = color(255);
}
}
return temp;
}
}

180
GOL.pde Normal file
View File

@ -0,0 +1,180 @@
class GOL {
Cell[][] cells;
Cell[][] old;
int scl, wdt, hgt, redd, bluee;
boolean sskip = false;
GOL(float possibility, int s, int wd, int hg) {
wdt = wd;
hgt = hg;
scl = s;
cells = new Cell[wdt/scl][hgt/scl];
int re = 0, bl = 0, max = 30;
for (int x = 0; x < wdt/scl; x++) {
for (int y = 0; y < hgt/scl; y++) {
//if (y%2 == 0) {
// cells[x][y] = new Cell(1,0,3,1);
//} else {
// cells[x][y] = new Cell(1, 0, 0, 0);
//}
int rand = 0;
boolean live = false;
if (random(1) >= 1-possibility) {
live = true;
rand = round(random(1, 2));
}
if (rand == 1 && re < max && live == true) {
re++;
cells[x][y] = new Cell(1, 0, 2, 1);
} else if (rand == 2 && bl < max && live == true) {
bl++;
cells[x][y] = new Cell(1, 0, 1, 1);
} else {
cells[x][y] = new Cell(1, 0, 0, 0);
}
//if ((rand == 1 || rand == 2) && live == true) {
// cells[x][y] = new Cell(1, 0, 3, 1);
//} else {
// cells[x][y] = new Cell(1, 0, 0, 0);
//}
}
}
}
void calculate(boolean s) {
Cell[][] nCells = new Cell[round(wdt/scl)][round(hgt/scl)];
bluee=0;
redd =0;
for (int x = 0; x < round(wdt/scl); x++) {
for (int y = 0; y < round(hgt/scl); y++) {
if (s == true) {
nCells[x][y] = new Cell(2, 0, 0, 0);
int te = cells[x][y].aliveCheck(x, y, scl, wdt, hgt);
if (te == 0) {
nCells[x][y].col = 0;
nCells[x][y].dec = 0;
} else if (te == 1) {
nCells[x][y].col = 1;
nCells[x][y].dec = cells[x][y].dec;
} else if (te == 2) {
nCells[x][y].col = 1;
nCells[x][y].dec = 1;
} else if (te == 3) {
nCells[x][y].col = 1;
nCells[x][y].dec = 2;
} else if (te == 4) {
nCells[x][y].col = 1;
nCells[x][y].dec = 3;
}
}
if (cells[x][y].dec == 1) {
bluee++;
} else if (cells[x][y].dec == 2) {
redd++;
}
stroke(0);
fill(cells[x][y].colCheck());
rect(x*scl, y*scl, scl, scl);
}
}
if (s == true) {
cells = nCells;
if (iteration == true) {
iteration = false;
}
}
if (sskip == false) {
if (redd == 0) {
lost = "red";
sskip = true;
} else if (bluee == 0) {
lost = "blue";
sskip = true;
}
}
}
void ai() {
}
void mouse(Cell[][] t, PVector tt, boolean ttt) {
//if (mousePressed) {
int i = int(tt.x), j = int(tt.y);
if (i >= 0 && i <= round(wdt/scl)-1 && j >= 0 && j <= round(hgt/scl)-1) {
if (t[i][j].col == 1) {
t[i][j].col = 0;
if (ttt == false) {
prev = t[i][j].dec;
}
t[i][j].dec = 0;
} else {
t[i][j].col = 1;
if (ttt == false) {
if (turn == "red") {
t[i][j].dec = 2;
} else if (turn == "blue") {
t[i][j].dec = 1;
} else {
t[i][j].dec = 3;
}
} else {
t[i][j].dec = prev;
}
}
cells = t;
}
// boolean skip = false;
// int i = round(mouseX/scl), j = round(mouseY/scl);
// if (i <= wdt-1 && j <= hgt-1) {
// int xMax = wdt/scl, yMax = hgt/scl;
// if (i < 0 || i > xMax || j < 0 || j > yMax) {
// skip = true;
// }
// if (skip == false) {
// for (int a = 0; a < 4; a++) {
// int g = i, h = j;
// skip = false;
// if (a == 0) {
// if (g-1 >= 0) {
// g-=1;
// } else {
// skip = true;
// }
// } else if (a == 1) {
// if (h-1 >= 0) {
// h-=1;
// } else {
// skip = true;
// }
// } else if (a == 2) {
// if (g+1 <= xMax-1) {
// g+=1;
// } else {
// skip = true;
// }
// } else if (a == 3) {
// if (h+1 <= yMax-1) {
// h+=1;
// } else {
// skip = true;
// }
// }
// if (skip == false) {
// if (t[g][h].col == 0) {
// t[g][h].col = 1;
// } else {
// t[g][h].col = 0;
// }
// }
// }
// cells = t;
// }
// }
//}
}
}

218
theGameOfLife.pde Normal file
View File

@ -0,0 +1,218 @@
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;
}
}
}
}