Last active
December 15, 2015 12:59
-
-
Save ravicious/5264202 to your computer and use it in GitHub Desktop.
Conway's Game of Life in Processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ######################## | |
// Najistotniejsze zmienne: | |
// procentowa szansa, że komórka przy wypełnianiu | |
// planszy w setup() będzie martwa | |
int randThreshold = 90; | |
// rozmiar planszy (boardSize x boardSize) | |
int boardSize = 60; | |
// liczba klatek na sekundę | |
int frames = 6; | |
// ######################## | |
// | |
// Kod: | |
Board board; | |
void setup() { | |
size(600, 600); | |
frameRate(frames); | |
board = new Board(width, height, boardSize); | |
board.fillBoard(); | |
//noLoop(); | |
} | |
void draw() { | |
background(0); | |
stroke(0); | |
board.drawBoard(); | |
board.updateCells(); | |
} | |
class Board { | |
ArrayList cells; | |
int boardWidth, boardHeight, cellsCount, cellsWidth, cellsHeight; | |
Board(int tempWidth, int tempHeight, int tempCount) { | |
boardWidth = tempWidth; | |
boardHeight = tempHeight; | |
cellsCount = tempCount; | |
cells = new ArrayList(); | |
cellsWidth = boardWidth / cellsCount; | |
cellsHeight = boardHeight / cellsCount; | |
fillBoard(); | |
}; | |
// wypełnia planszę komórkami | |
void fillBoard() { | |
for (int row = 0; row < cellsCount; row++) { // rzędy | |
for (int col = 0; col < cellsCount; col++) { // kolumny | |
Cell newCell= new Cell(row, col, this); // tworzy nową komórkę i przypisuje ją do planszy | |
cells.add(newCell); | |
}; | |
}; | |
}; | |
// rysuje planszę | |
void drawBoard() { | |
for (int i = 0; i < cells.size(); i++) { | |
Cell cell = (Cell) cells.get(i); | |
if (cell.isAlive()) fill (255); | |
else fill(20); | |
rect(cellsWidth*cell.row, cellsHeight*cell.col, cellsWidth, cellsHeight); | |
}; | |
}; | |
// aktualizuje stan wszystkich komórek | |
void updateCells() { | |
for (int i = 0; i < cells.size(); i++) { | |
Cell cell = (Cell) cells.get(i); | |
cell.computeState(); | |
}; | |
for (int i = 0; i < cells.size(); i++) { | |
Cell cell = (Cell) cells.get(i); | |
cell.update(); | |
}; | |
}; | |
// zwraca komórkę znajdującą się w podanym wierszu i kolumnie | |
// | |
// komórki w ArrayList cells są po prostu numerowane kolejno | |
// my z kolei wyświetlamy komórki w kolumnach i rzędach, tutaj dla przykładu 20x20 | |
// 0 rząd: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
// 1 rząd: 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
// 2 rząd: 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
// Żeby więc uzyskać tę komórkę, na której nam zależy, mając do dyspozycji tylko | |
// numer wiersza i numer kolumny, używamy formuły | |
// <numer wiersza> * <ilość komórek w wierszu> + <numer kolumny> | |
Cell cell(int tempRow, int tempCol) { | |
Cell cell = (Cell) cells.get(tempRow*cellsCount + tempCol); | |
return cell; | |
}; | |
}; | |
class Cell { | |
int row, col; | |
boolean alive, newState; | |
Board board; | |
int aliveNeighbours; | |
// n8s = neighbours | |
// n8s to tablica z sąsiadami | |
ArrayList n8s = new ArrayList(); | |
Cell(int tempRow, int tempCol, Board tempBoard) { | |
row = tempRow; | |
col = tempCol; | |
board = tempBoard; | |
// wylosuj początkowy stan | |
float randomAlive = random(100); | |
if (randomAlive > randThreshold) { | |
alive = true; | |
}; | |
} | |
boolean isAlive() { | |
return alive; | |
} | |
// n8s to tablica z sąsiadami | |
ArrayList neighbours() { | |
if (n8s.size() == 0) { // jeśli tablica z sąsiadami jest pusta, poszukaj ich | |
getNeighbours(); | |
} | |
return n8s; | |
} | |
// pobiera koordynaty sąsiadów | |
void getNeighbours() { | |
int[] values = { | |
-1, 0, 1 | |
}; | |
for (int x = 0; x < values.length; x++) { // rzędy (row) | |
for (int y = 0; y < values.length; y++) { // kolumny (col) | |
int neighbourX = row+values[x]; | |
int neighbourY = col+values[y]; | |
// cztery pierwsze warunki sprawdzają, czy sąsiad nie leży poza planszą | |
// (neighbourX >= 0 && neighbourY >= 0) && | |
// && (neighbourX < board.cellsCount && neighbourY < board.cellsCount) | |
// | |
// dwa ostatnie warunki sprawdzają, czy sąsiad nie jest tak naprawdę komórką, | |
// która właśnie szuka sąsiadów | |
// (neighbourX != row || neighbourY != col) | |
if ((neighbourX >= 0 && neighbourY >= 0) && (neighbourX < board.cellsCount && neighbourY < board.cellsCount) && (neighbourX != row || neighbourY != col)) { | |
n8s.add(board.cell(neighbourX, neighbourY)); | |
}; | |
}; | |
}; | |
}; | |
// oblicza liczbę żywych sąsiadów | |
void countAliveNeighbours() { | |
aliveNeighbours = 0; | |
for (int i = 0; i < neighbours().size(); i++) { | |
Cell neighbour = (Cell) neighbours().get(i); | |
if (neighbour.isAlive()) { | |
aliveNeighbours++; | |
} | |
}; | |
}; | |
// oblicza, w jakim stanie będzie komórka w następnej turze | |
void computeState() { | |
countAliveNeighbours(); | |
int anc = aliveNeighbours; // anc - alive neighbours counter | |
if (anc < 2) { | |
newState = false; | |
} | |
else if (alive && (anc == 2 || anc == 3)) { | |
newState = true; | |
} | |
else if (alive && anc > 3) { | |
newState = false; | |
} | |
else if (alive == false && anc == 3) { | |
newState = true; | |
} | |
}; | |
// aktualizuje stan komórki | |
void update() { | |
alive = newState; | |
} | |
}; | |
//void mouseClicked() { | |
// redraw(); | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment