/*
Alex Drikos, Yazhou Liu, Terence Nguyen, Dante Sangoi
11/13/2023
Summative Unit Project
Attempting to create minesweeper
*/
/*
NOTES AND TO DO LIST
--> USE # FOR CLOSED AND - FOR OPEN
--> USE LETTERS AND NUMBERS FOR ROWS AND COLUMNS
--> USE FUNCTION TO CONVERT LETTERS TO NUMBERS
--> NEED RNG TO PLACE BOMBS
--> CHECK IF SPOT ALREADY HAS BOMBS
--> FUNCTION FOR CHECKING ADJACENT SQUARES FOR BOMBS
SCENES
--> GAME
--> HELP SCREEN
LATER
--> FLAGGING
--> IF HAVE TIME, CREATE A BIGGER BOARD
*/
// VARIABLE DECLARATION
int randint;
int randint2;
int choice;
int inputX;
int inputY;
int inputCell;
bool gameStatus = true;
int counter;
String mainBoard[8][8] = { // [y][x]
//0 1 2 3 4 5 6 7
{"#", "#", "#", "#", "#", "#", "#", "#"}, // A 0
{"#", "#", "#", "#", "#", "#", "#", "#"}, // B 1
{"#", "#", "#", "#", "#", "#", "#", "#"}, // C 2
{"#", "#", "#", "#", "#", "#", "#", "#"}, // D 3
{"#", "#", "#", "#", "#", "#", "#", "#"}, // E 4
{"#", "#", "#", "#", "#", "#", "#", "#"}, // F 5
{"#", "#", "#", "#", "#", "#", "#", "#"}, // G 6
{"#", "#", "#", "#", "#", "#", "#", "#"} // H 7
};
String bombBoard[8][8] = { // [y][x]
//0 1 2 3 4 5 6 7
{"00", "10", "20", "30", "40", "50", "60", "70"}, // A 0
{"10", "11", "12", "13", "14", "15", "16", "17"}, // B 1
{"20", "21", "22", "23", "24", "25", "26", "27"}, // C 2
{"30", "31", "32", "33", "34", "35", "36", "37"}, // D 3
{"40", "41", "42", "43", "44", "45", "46", "47"}, // E 4
{"50", "51", "52", "53", "54", "55", "56", "57"}, // F 5
{"60", "61", "62", "63", "64", "65", "66", "67"}, // G 6
{"70", "71", "72", "73", "74", "75", "76", "77"} // H 7
};
// CUSTOM FUNCTIONS
// Prints the board
void printBoard() {
Serial.println(" 0 1 2 3 4 5 6 7");
Serial.println(" _ _ _ _ _ _ _ _");
for (int y=0;y<8;y++) {
Serial.print(String(y)+" |");
for (int x=0;x<8;x++) {
Serial.print(mainBoard[y][x]+" ");
}
Serial.println();
}
}
// Generate Mines
void generateBombs(int x, int y) {
for (int i=0;i<10;i++) {
randint = random(0,8);
randint2 = random(0,8);
while (randint == y) {
randint = random(0,8);
}
while (randint2 == x) {
randint2 = random(0,8);
}
if (bombBoard[randint][randint2] != "XX") {
bombBoard[randint][randint2] = "XX";
}
else {
i--;
}
}
}
// Finds bombs in adjacent tiles
void findAdjacent(int x, int y) { // a and b, row and column, letter and number
// Have nested for loop that counts all around the inputted coordinate
if (y < 0 || y > 7 || x < 0 || x > 7 || bombBoard[y][x] == "OO") {
return;
}
int count=0; // counts amt of bombs around
if (bombBoard[y][x]=="XX") {
revealBombs();
}
else {
count += checkTile(y-1,x-1); // top left
count += checkTile(y-1,x); // top
count += checkTile(y-1,x+1); // top right
count += checkTile(y,x-1); // left
count += checkTile(y,x+1); // right
count += checkTile(y+1,x-1); // btm left
count += checkTile(y+1,x); // btm
count += checkTile(y+1,x+1); // btm right
bombBoard[y][x] = "OO";
if (count > 0) {
mainBoard[y][x] = count;
}
else if (count == 0) {
mainBoard[y][x] = "-";
//CHECK ADJACENT TILES TO CHECK EMPTY CELLS
findAdjacent(x-1,y-1); // top left
delay(10);
findAdjacent(x,y-1); // top
delay(10);
findAdjacent(x+1,y-1); // top right
delay(10);
findAdjacent(x-1,y); // left
delay(10);
findAdjacent(x+1,y); // right
delay(10);
findAdjacent(x-1,y+1); // btm left
delay(10);
findAdjacent(x,y+1); // btm
delay(10);
findAdjacent(x+1,y+1); // btm right
delay(10);
}
}
}
int checkTile(int row, int column) {
if (row < 0 || row > 7 || column < 0 || column > 7) {
return 0;
}
if (bombBoard[row][column] == "XX") {
return 1;
}
return 0;
}
void revealBombs() {
for (int y=0;y<8;y++) {
for (int x=0;x<8;x++) {
if (bombBoard[y][x] == "XX") {
mainBoard[y][x] = "X";
}
else {
mainBoard[y][x] = "-";
}
}
}
gameStatus = false;
}
void setup()
{
Serial.begin(115200);
randomSeed(analogRead(0));
while(choice!=2){
Serial.println("WELCOME TO BOMB SWEEPER(TM): HELP SCREEN(1) PLAY GAME(2)");
while(Serial.available()==0){}
choice=Serial.parseInt();
if(choice==1){
Serial.println("-----HOW TO PLAY-----");
Serial.println("In Bomb Sweeper there is a grid full of hidden mines that are revealed by adjacent tiles.");
Serial.println("The number will be shown when there is a bomb in either of the corners or sides connected.");
Serial.println("The objective is to remove every non bomb space.");
Serial.println("IF YOU CLICK ON A BOMB YOU WILL DIE");
}
}
if(choice==2){
while (gameStatus == true) {
delay(500);
Serial.print("Enter X Coordinate (0-7): ");
Serial.flush();
delay(100);
while(Serial.available()==1){}
inputX=Serial.parseInt();
Serial.println(inputX);
delay(250);
Serial.print("Enter Y Coordinate (0-7): ");
Serial.flush();
delay(100);
while(Serial.available()==1){}
inputY=Serial.parseInt();
Serial.println(inputY);
delay(500);
if (counter == 0) {
generateBombs(inputX, inputY);
counter++;
}
findAdjacent(inputX, inputY);
printBoard();
}
}
} // setup end
void loop()
{
}