Created
April 25, 2019 11:57
-
-
Save thcrack/96cc69be48ef71a8679d7a746431908e to your computer and use it in GitHub Desktop.
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
boolean[][] grid ; | |
// Number of columns and rows in the grid | |
int cols = 10 ; | |
int rows = 10 ; | |
int cellWidth = 0 ; | |
int cellHeight = 0 ; | |
PImage win; | |
final int GAME_RUN=0,GAME_WIN=1; | |
int gameState=0; | |
// The grid status color | |
color turnOnColor = #F0F5FB ; | |
color turnOffColor = #022547 ; | |
void setup() { | |
size(400,400) ; | |
stroke(#95A5B5); | |
cellWidth = width / cols ; | |
cellHeight = height / rows ; | |
grid = new boolean[cols][rows] ; | |
win=loadImage("win.jpg"); | |
// initiallize require of execrise | |
for(int i = 1; i < 10; i += 3){ | |
int count = 1 + floor(random(2)); | |
int lastCol = -1; | |
int lastRow = -1; | |
for(int j = 0; j < count; j++){ | |
int col = floor(random(10)); | |
int row = i + floor(random(3)); | |
if(lastCol == col && lastRow == row){ | |
j--; | |
}else{ | |
grid[col][row] = !grid[col][row]; | |
if(col - 1 >= 0) grid[col - 1][row] = !grid[col - 1][row]; | |
if(col + 1 < cols) grid[col + 1][row] = !grid[col + 1][row]; | |
if(row - 1 >= 0) grid[col][row - 1] = !grid[col][row - 1]; | |
if(row + 1 < rows) grid[col][row + 1] = !grid[col][row + 1]; | |
lastCol = col; | |
lastRow = row; | |
} | |
} | |
} | |
} | |
void draw() { | |
background(0) ; | |
switch(gameState){ | |
case GAME_RUN: | |
// grid[][]= true/false; statusColor= turnOnColor/turnOffColor | |
for (int i = 0; i < cols; i++) { | |
for (int j = 0; j < rows; j++) { | |
color statusColor = (grid[i][j]) ? turnOnColor : turnOffColor ; | |
fill(statusColor) ; | |
rect(cellWidth*i, cellHeight*j, cellWidth, cellHeight) ; | |
} | |
} | |
// Detcet if all grid[][] is false | |
boolean isAllFalse = true; | |
for (int i = 0; i < cols; i++) { | |
for (int j = 0; j < rows; j++) { | |
if(grid[i][j]){ | |
isAllFalse = false; | |
break; | |
} | |
} | |
} | |
if (isAllFalse){ | |
gameState=GAME_WIN; | |
} | |
break; | |
case GAME_WIN: | |
image(win,0,0); | |
if(keyPressed){ | |
//initiallize code | |
for(int i = 1; i < 10; i += 3){ | |
int count = 1 + floor(random(2)); | |
int lastCol = -1; | |
int lastRow = -1; | |
for(int j = 0; j < count; j++){ | |
int col = floor(random(10)); | |
int row = i + floor(random(3)); | |
if(lastCol == col && lastRow == row){ | |
j--; | |
}else{ | |
grid[col][row] = !grid[col][row]; | |
if(col - 1 >= 0) grid[col - 1][row] = !grid[col - 1][row]; | |
if(col + 1 < cols) grid[col + 1][row] = !grid[col + 1][row]; | |
if(row - 1 >= 0) grid[col][row - 1] = !grid[col][row - 1]; | |
if(row + 1 < rows) grid[col][row + 1] = !grid[col][row + 1]; | |
lastCol = col; | |
lastRow = row; | |
} | |
} | |
} | |
gameState=GAME_RUN; | |
} | |
break; | |
} | |
} | |
void mouseClicked(){ | |
int col = mouseX / cellWidth; | |
int row = mouseY / cellHeight; | |
grid[col][row] = !grid[col][row]; | |
if(col - 1 >= 0) grid[col - 1][row] = !grid[col - 1][row]; | |
if(col + 1 < cols) grid[col + 1][row] = !grid[col + 1][row]; | |
if(row - 1 >= 0) grid[col][row - 1] = !grid[col][row - 1]; | |
if(row + 1 < rows) grid[col][row + 1] = !grid[col][row + 1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment