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

181 lines
4.6 KiB
Plaintext

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;
// }
// }
//}
}
}