xxxxxxxxxx
bool inGame = true;
byte gameState = 0; //0 = neutral, 1 = won, 2 = lost
int board[][] = new int[16][16]; // create 256 integers (Sets data for Mine, or amount of neighboring mines (0-8). )
int board2[][] = new int[16][16]; //256 integers (Sets data for open, hidden, or flagged space.)
int mines;
int ww;
int hh;
int mines;
int x;
int y;
void setup(){
size(513, 518); // 512 (16x16 Game)
start();
}//End Void Setup
void draw(){
background(200);
stroke(0);
fill(0);
int ww = 16;
int hh = 16;
int x;
int y = - 11;
for(int c = 0; c < ww; c++){
int x = 16;
int y = y + 32;
for(int r = 0; r < hh; r++){
fill(0);
stroke(0);
if( board2[c][r] == 1){
if(board[c][r] != 0){
text(board[c][r],x-1,y+4);}
}else{
fill(200);
rect(x,y,32,32);
}
if(board2[c][r] == 2){
fill(0);
text("F",x-1,y+4);
}
x = x + 32;
}
}
int NonMines = 0;
for(int c = 0; c < 16; c++;){
for( int r = 0; r < 16; r++){
if(board[r][c] != -1 && board2[r][c] == 1){ NonMines +=1; }
}
}
if(NonMines >= 216){
print("You Win!");
inGame = false;
gameState = 1;
}
if(inGame == false)
{
if(gameState == 1)
{
fill(200);
rectMode(CENTER);
rect(width/2,height/2,400,200);
fill(0);
textSize(18);
textMode(CENTER);
text("You Win!",width/2-38,height/2,200,400);
}
else{
fill(200);
rectMode(CENTER);
rect(width/2,height/2,400,200);
textSize(18);
fill(0);
textMode(CENTER);
text("You Lose!",width/2-40,height/2);
}
}
} //End void draw
void mousePressed(){
if(inGame == true){
if(mouseButton == LEFT){
xx = ceil((ceil(mouseX))/32); //Round onto the nearest grid spot(X) (took me a while to get this simple but complicated design)
yy = ceil((ceil(mouseY))/32);
//Click on Mine:
if(board2[yy-1][xx-1] == 0){
if(board[yy-1][xx-1] == -1){
//print("Game Over!");
gameState = 2;
inGame = false;
}
}
//Click on Flag or NonMine:
if(board2[yy-1][xx-1] != 2 && board2[yy-1][xx-1] != 1){
if(board[yy-1][xx-1] == 0){ //print(" Opening neighbors! ");
//Open all neighbors:
if(yy-1 > 0){ board2[(yy-1)-1][xx-1] = 1; }
if(yy-1 < 15){ board2[(yy-1)+1][xx-1] = 1; }
if(xx-1 < 15){ board2[yy-1][(xx-1)+1] = 1; }
if(xx-1 > 0){ board2[yy-1][(xx-1)-1] = 1; }
if(yy-1 > 0 && xx-1 < 15){ board2[(yy-1)-1][(xx-1)+1] = 1; }
if(yy-1 > 0 && xx-1 > 0){ board2[(yy-1)-1][(xx-1)-1] = 1; }
if(yy-1 < 15 && xx-1 < 15){ board2[(yy-1)+1][(xx-1)+1] = 1; }
if(yy-1 < 15 && xx-1 > 0){ board2[(yy-1)+1][(xx-1)-1] = 1; }
int opening = 1;
while(opening > 0){
opening = 0;
for(int c = 0; c < 16; c++;){
for(int r = 0; r < 16; r++){
if(board[c][r] == 0){
if(board2[c][r] == 1){
if(c > 0){ if(board2[c-1][r] == 0 && board2[c-1][r] != 2){ board2[c-1][r] = 1; opening += 1; }}
if(c < 15){ if(board2[c+1][r] == 0 && board2[c+1][r] != 2){board2[c+1][r] = 1; opening += 1; }}
if(r < 15){ if(board2[c][r+1] == 0 && board2[c][r+1] != 2){board2[c][r+1] = 1; opening += 1; }}
if(r > 0){ if(board2[c][r-1] == 0 && board2[c][r-1] != 2){board2[c][r-1] = 1; opening += 1; }}
if(c > 0 && r < 15){ if(board2[c-1][r+1] == 0 && board2[c-1][r+1] != 2){board2[c-1][r+1] = 1; opening += 1;}}
if(c > 0 && r > 0){if(board2[c-1][r-1] == 0 && board2[c-1][r-1] != 2){board2[c-1][r-1] = 1; opening += 1;}}
if(c < 15 && r < 15){ if(board2[c+1][r+1] == 0 && board2[c+1][r+1] != 2){board2[c+1][r+1] = 1; opening += 1;}}
if(c < 15 && r > 0){ if(board2[c+1][r-1] == 0 && board2[c+1][r-1] != 2){board2[c+1][r-1] = 1; opening += 1;}}
} //End if 0 is open
} //End if 0
} //End Second FOR loop
} //End First FOR loop
//print(" " + opening + " ");
}//End While Loop
} // End if 0
board2[yy-1][xx-1] = 1;
} //end Click
}
if(mouseButton == RIGHT){
// xx = int(nfc((mouseX+16)/32));
// yy = int(nfc((mouseY+21)/32));
xx = ceil((ceil(mouseX))/32);
yy= ceil((ceil(mouseY))/32);
if(board2[yy-1][xx-1] == 2){
// print("F removed");
board2[yy-1][xx-1] = 0;}else{
if(board2[yy-1][xx-1] != 1 && board2[yy-1][xx-1] != 2){
board2[yy-1][xx-1] = 2; }
}
}
}
} //End Void mouseCLick
void start()
{
ww = 16;
hh = 16;
mines = 40;
textSize(13);
rectMode(CENTER);
x;
y = 0;
for(int i = 0; i < mines; i++){
cMine = int(random(16));
rMine = int(random(16));
if(board[cMine][rMine] == 0){
board[cMine][rMine] = -1;
}else{ i--;}
} //end for loop
for(int c = 0; c < ww; c++){
for(int r = 0; r < hh; r++){
int mines = 0;
if(board[c][r] == 0){
if(c > 0){ if(board[c-1][r] == -1){mines += 1; }}
if(c < 15){ if(board[c+1][r] == -1){mines += 1; }}
if(r < 15){ if(board[c][r+1] == -1){mines += 1; }}
if(r > 0){ if(board[c][r-1] == -1){mines += 1; }}
if(c > 0 && r < 15){ if(board[c-1][r+1] == -1){mines += 1;}}
if(c > 0 && r > 0){if(board[c-1][r-1] == -1){mines += 1;}}
if(c < 15 && r < 15){ if(board[c+1][r+1] == -1){mines += 1;}}
if(c < 15 && r > 0){ if(board[c+1][r-1] == -1){mines += 1;}}
board[c][r] = mines;
}
}
}
}
/*
Count all the mines in the 8 adjacent
cells:
N.W N N.E
\ | /
\ | /
W----Cell----E
/ | \
/ | \
S.W S S.E
Cell-->Current Cell (row, col)
N --> North (row-1, col)
S --> South (row+1, col)
E --> East (row, col+1)
W --> West (row, col-1)
N.E--> North-East (row-1, col+1)
N.W--> North-West (row-1, col-1)
S.E--> South-East (row+1, col+1)
S.W--> South-West (row+1, col-1)
* 0 Hidden
* 1 Open
* 2 Flagged
*/