update sweep miner template

This commit is contained in:
Roy Qu 2022-08-08 16:18:42 +08:00
parent be1bf5717f
commit 0a57f7290b
1 changed files with 141 additions and 143 deletions

View File

@ -18,46 +18,44 @@ using namespace std;
#define ROWS 9 #define ROWS 9
#define COLUMNS 9 #define COLUMNS 9
const int empty = -1; #define EMPTY 0
const int mine = 0; #define MINE -1
class block { class Block {
public: public:
block(); Block();
block(int x, int y, int btype); Block(int x, int y, int btype);
// Mutators // Mutators
setx(int x); void setX(int x);
sety(int y); void setY(int y);
setrevealed(bool reveal); void setRevealed(bool reveal);
setmarked(bool mark); void setMarked(bool mark);
settype(int btype); void setType(int btype);
formatblock(int x, int y);
// Accessors // Accessors
int getx(); int getX();
int gety(); int getY();
bool getrevealed(); bool isRevealed();
bool getmarked(); bool isMarked();
int gettype(); int getType();
private: private:
// Coordinates of block // Coordinates of block
int xcord; int mX;
int ycord; int mY;
// States // States
bool revealed; bool mRevealed;
bool marked; bool mMarked;
// Type // Type
int type; // Ranges from -1 to 8 int mType; // Ranges from -1 to 8
}; };
// Create 2d array // Create 2d array
block gameboard[ROWS][COLUMNS]; Block gameboard[ROWS][COLUMNS];
// Generate random number // Generate random number
@ -66,19 +64,15 @@ int randomnum() {
} }
// Generate number blocks // Generate number blocks
void numbergeneration(int i, int j); void countMinesInNeighbors(int i, int j);
// Update empty spots // Update empty spots
void floodfill(int x, int y); void revealBlocks(int x, int y);
// Game states // Game states
bool gameover = false; bool gameover = false;
bool gamewon = false; bool gamewon = false;
void restartgame(); void restartGame();
// Block counts
int totalblocks = 0;
int main() int main()
{ {
@ -90,7 +84,7 @@ int main()
InitWindow(screenWidth, screenHeight, "Minesweeper by Matthew Zegar"); InitWindow(screenWidth, screenHeight, "Minesweeper by Matthew Zegar");
SetTargetFPS(60); SetTargetFPS(60);
restartgame(); restartGame();
// Mouse setup // Mouse setup
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -115,14 +109,14 @@ int main()
int x = floor(mouse.x/50); int x = floor(mouse.x/50);
if (y >= 0 && y < ROWS if (y >= 0 && y < ROWS
&& x >=0 && x < COLUMNS ) { && x >=0 && x < COLUMNS ) {
if (gameboard[y][x].getmarked() == false) { if (gameboard[y][x].isMarked() == false) {
gameboard[y][x].setrevealed(true); gameboard[y][x].setRevealed(true);
if (gameboard[y][x].gettype() == -1) { // CHECK IF 'EMPTY' if (gameboard[y][x].getType() == EMPTY) { // CHECK IF 'EMPTY'
gameboard[y][x].setrevealed(false); gameboard[y][x].setRevealed(false);
floodfill(y, x); revealBlocks(y, x);
} }
if (gameboard[y][x].gettype() == 0) { if (gameboard[y][x].getType() == MINE) {
gameover = true; gameover = true;
} }
} }
@ -138,11 +132,11 @@ int main()
int x = floor(mouse.x/50); int x = floor(mouse.x/50);
if (y >= 0 && y < ROWS if (y >= 0 && y < ROWS
&& x >=0 && x < COLUMNS ) { && x >=0 && x < COLUMNS ) {
if (gameboard[y][x].getrevealed() == false) { if (gameboard[y][x].isRevealed() == false) {
if (gameboard[y][x].getmarked() == false) { if (gameboard[y][x].isMarked() == false) {
gameboard[y][x].setmarked(true); gameboard[y][x].setMarked(true);
} else { } else {
gameboard[y][x].setmarked(false); gameboard[y][x].setMarked(false);
} }
} }
} }
@ -153,36 +147,36 @@ int main()
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
for (int i = 0; i < ROWS; ++i) { for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLUMNS; ++j) { for (int j = 0; j < COLUMNS; ++j) {
if (gameboard[i][j].getrevealed() == false) { if (gameboard[i][j].isRevealed() == false) {
DrawRectangle(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, LIGHTGRAY); DrawRectangle(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, LIGHTGRAY);
DrawRectangleLines(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, BLACK); DrawRectangleLines(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, BLACK);
} else { } else {
if (gameboard[i][j].gettype() == 0) { if (gameboard[i][j].getType() == MINE) {
DrawRectangle(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, RED); DrawRectangle(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, RED);
DrawRectangleLines(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, BLACK); DrawRectangleLines(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, BLACK);
} else if (gameboard[i][j].gettype() == -1) { } else if (gameboard[i][j].getType() == EMPTY) {
DrawRectangle(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, GRAY); DrawRectangle(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, GRAY);
DrawRectangleLines(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, BLACK); DrawRectangleLines(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, BLACK);
} else if (gameboard[i][j].gettype() == 1) { } else if (gameboard[i][j].getType() == 1) {
DrawRectangle(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, DARKGRAY); DrawRectangle(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, DARKGRAY);
DrawRectangleLines(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, BLACK); DrawRectangleLines(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, BLACK);
DrawText(TextFormat("%i", gameboard[i][j].gettype()), gameboard[i][j].gety()*50+20, gameboard[i][j].getx()*50+15, 25, SKYBLUE); DrawText(TextFormat("%i", gameboard[i][j].getType()), gameboard[i][j].getY()*50+20, gameboard[i][j].getX()*50+15, 25, SKYBLUE);
} else if (gameboard[i][j].gettype() == 2) { } else if (gameboard[i][j].getType() == 2) {
DrawRectangle(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, DARKGRAY); DrawRectangle(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, DARKGRAY);
DrawRectangleLines(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, BLACK); DrawRectangleLines(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, BLACK);
DrawText(TextFormat("%i", gameboard[i][j].gettype()), gameboard[i][j].gety()*50+20, gameboard[i][j].getx()*50+15, 25, GREEN); DrawText(TextFormat("%i", gameboard[i][j].getType()), gameboard[i][j].getY()*50+20, gameboard[i][j].getX()*50+15, 25, GREEN);
} else if (gameboard[i][j].gettype() == 3) { } else if (gameboard[i][j].getType() == 3) {
DrawRectangle(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, DARKGRAY); DrawRectangle(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, DARKGRAY);
DrawRectangleLines(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, BLACK); DrawRectangleLines(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, BLACK);
DrawText(TextFormat("%i", gameboard[i][j].gettype()), gameboard[i][j].gety()*50+20, gameboard[i][j].getx()*50+15, 25, RED); DrawText(TextFormat("%i", gameboard[i][j].getType()), gameboard[i][j].getY()*50+20, gameboard[i][j].getX()*50+15, 25, RED);
} else if (gameboard[i][j].gettype() == 4) { } else if (gameboard[i][j].getType() == 4) {
DrawRectangle(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, DARKGRAY); DrawRectangle(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, DARKGRAY);
DrawRectangleLines(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, BLACK); DrawRectangleLines(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, BLACK);
DrawText(TextFormat("%i", gameboard[i][j].gettype()), gameboard[i][j].gety()*50+20, gameboard[i][j].getx()*50+15, 25, DARKBLUE); DrawText(TextFormat("%i", gameboard[i][j].getType()), gameboard[i][j].getY()*50+20, gameboard[i][j].getX()*50+15, 25, DARKBLUE);
} else if (gameboard[i][j].gettype() >= 5) { } else if (gameboard[i][j].getType() >= 5) {
DrawRectangle(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, DARKGRAY); DrawRectangle(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, DARKGRAY);
DrawRectangleLines(gameboard[i][j].gety()*50, gameboard[i][j].getx()*50, 50, 50, BLACK); DrawRectangleLines(gameboard[i][j].getY()*50, gameboard[i][j].getX()*50, 50, 50, BLACK);
DrawText(TextFormat("%i", gameboard[i][j].gettype()), gameboard[i][j].gety()*50+20, gameboard[i][j].getx()*50+15, 25, MAROON); DrawText(TextFormat("%i", gameboard[i][j].getType()), gameboard[i][j].getY()*50+20, gameboard[i][j].getX()*50+15, 25, MAROON);
} }
} }
} }
@ -192,20 +186,20 @@ int main()
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
for (int i = 0; i < ROWS; ++i) { for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLUMNS; ++j) { for (int j = 0; j < COLUMNS; ++j) {
if (gameboard[i][j].getmarked() == true and gameboard[i][j].getrevealed() == false) { if (gameboard[i][j].isMarked() && !gameboard[i][j].isRevealed() ) {
DrawRectangle(gameboard[i][j].gety()*50+15, gameboard[i][j].getx()*50+10, 10, 10, RED); DrawRectangle(gameboard[i][j].getY()*50+15, gameboard[i][j].getX()*50+10, 10, 10, RED);
DrawRectangle(gameboard[i][j].gety()*50+25, gameboard[i][j].getx()*50+10, 5, 25, BLACK); DrawRectangle(gameboard[i][j].getY()*50+25, gameboard[i][j].getX()*50+10, 5, 25, BLACK);
DrawRectangle(gameboard[i][j].gety()*50+20, gameboard[i][j].getx()*50+35, 15, 5, BLACK); DrawRectangle(gameboard[i][j].getY()*50+20, gameboard[i][j].getX()*50+35, 15, 5, BLACK);
} }
} }
} }
// Update variable to determine if won // Update variable to determine if won
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
totalblocks = 0; int totalblocks = 0;
for (int i = 0; i < ROWS; ++i) { for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLUMNS; ++j) { for (int j = 0; j < COLUMNS; ++j) {
if (gameboard[i][j].gettype() >= 1 and gameboard[i][j].getrevealed() == false) { if (gameboard[i][j].getType() >= 1 and gameboard[i][j].isRevealed() == false) {
totalblocks++; totalblocks++;
} }
} }
@ -222,12 +216,12 @@ int main()
for (int i = 0; i < ROWS; ++i) { for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLUMNS; ++j) { for (int j = 0; j < COLUMNS; ++j) {
gameboard[i][j].setrevealed(true); gameboard[i][j].setRevealed(true);
} }
} }
if ((IsMouseButtonReleased(MOUSE_RIGHT_BUTTON))) { if ((IsMouseButtonReleased(MOUSE_RIGHT_BUTTON))) {
restartgame(); restartGame();
gamewon = false; gamewon = false;
} }
} }
@ -240,9 +234,14 @@ int main()
DrawText("right click to restart", 101, 251, 25, WHITE); DrawText("right click to restart", 101, 251, 25, WHITE);
DrawText("right click to restart", 100, 250, 25, BLACK); DrawText("right click to restart", 100, 250, 25, BLACK);
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLUMNS; ++j) {
gameboard[i][j].setRevealed(true);
}
}
if ((IsMouseButtonReleased(MOUSE_RIGHT_BUTTON))) { if ((IsMouseButtonReleased(MOUSE_RIGHT_BUTTON))) {
restartgame(); restartGame();
gameover = false; gameover = false;
} }
} }
@ -261,31 +260,33 @@ int main()
} }
// Floodfill algorithm // Floodfill algorithm
void floodfill(int x, int y) { void revealBlocks(int x, int y) {
if (x >= 0 and x <ROWS and y >= 0 and y < COLUMNS) { if (x >= 0 && x <ROWS && y >= 0 && y < COLUMNS) {
if (gameboard[x][y].gettype() != -1) { if (gameboard[x][y].getType() != EMPTY) {
gameboard[x][y].setrevealed(true); gameboard[x][y].setRevealed(true);
} else { } else {
if (gameboard[x][y].getrevealed() == false) { if (gameboard[x][y].isRevealed() == false) {
gameboard[x][y].setrevealed(true); gameboard[x][y].setRevealed(true);
floodfill(x-1, y); revealBlocks(x-1, y);
//floodfill(x-1, y-1); revealBlocks(x-1, y-1);
floodfill(x, y-1); revealBlocks(x-1, y+1);
//floodfill(x+1, y-1);
floodfill(x+1, y); revealBlocks(x, y-1);
//floodfill(x+1, y+1); revealBlocks(x, y+1);
floodfill(x, y+1);
//floodfill(x-1, y+1); revealBlocks(x+1, y-1);
revealBlocks(x+1, y);
revealBlocks(x+1, y+1);
} }
} }
} }
} }
// Restart the game // Restart the game
void restartgame() { void restartGame() {
for (int i = 0; i < ROWS; ++i) { for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLUMNS; ++j) { for (int j = 0; j < COLUMNS; ++j) {
gameboard[i][j] = block(i,j,-1); gameboard[i][j] = Block(i,j,EMPTY);
} }
} }
@ -295,7 +296,7 @@ void restartgame() {
for (int i = 0; i < ROWS; ++i) { for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLUMNS; ++j) { for (int j = 0; j < COLUMNS; ++j) {
if (randomnum() == 1) { if (randomnum() == 1) {
gameboard[i][j].settype(0); gameboard[i][j].setType(MINE);
} }
} }
} }
@ -304,29 +305,29 @@ void restartgame() {
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
for (int i = 0; i < 9; ++i) { for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) { for (int j = 0; j < 9; ++j) {
if (gameboard[i][j].gettype() != 0) { if (gameboard[i][j].getType() != MINE) {
numbergeneration(i,j); countMinesInNeighbors(i,j);
} }
} }
} }
} }
// Generates the numbered blocks based on mines // Generates the numbered blocks based on mines
void numbergeneration(int i, int j) { void countMinesInNeighbors(int i, int j) {
int count = 0; int count = 0;
// Check left hand side // Check left hand side
if ((j-1)>=0) { if ((j-1)>=0) {
if ((i-1) >= 0) { if ((i-1) >= 0) {
if (gameboard[i-1][j-1].gettype() == 0) { if (gameboard[i-1][j-1].getType() == MINE) {
count++; count++;
} }
} }
if (gameboard[i][j-1].gettype() == 0) { if (gameboard[i][j-1].getType() == MINE) {
count++; count++;
} }
if ((i+1) < ROWS) { if ((i+1) < ROWS) {
if (gameboard[i+1][j-1].gettype() == 0) { if (gameboard[i+1][j-1].getType() == MINE) {
count++; count++;
} }
} }
@ -335,13 +336,13 @@ void numbergeneration(int i, int j) {
// Check middle // Check middle
if ((i-1) >= 0) { if ((i-1) >= 0) {
if (gameboard[i-1][j].gettype() == 0) { if (gameboard[i-1][j].getType() == MINE) {
count++; count++;
} }
} }
if ((i+1) < ROWS) { if ((i+1) < ROWS) {
if (gameboard[i+1][j].gettype() == 0) { if (gameboard[i+1][j].getType() == MINE) {
count++; count++;
} }
} }
@ -349,89 +350,86 @@ void numbergeneration(int i, int j) {
// Check right // Check right
if ((j+1)<COLUMNS) { if ((j+1)<COLUMNS) {
if ((i-1) >= 0) { if ((i-1) >= 0) {
if (gameboard[i-1][j+1].gettype() == 0) { if (gameboard[i-1][j+1].getType() == MINE) {
count++; count++;
} }
} }
if (gameboard[i][j+1].gettype() == 0) { if (gameboard[i][j+1].getType() == MINE) {
count++; count++;
} }
if ((i+1) <= ROWS) { if ((i+1) <= ROWS) {
if (gameboard[i+1][j+1].gettype() == 0) { if (gameboard[i+1][j+1].getType() == MINE) {
count++; count++;
} }
} }
} }
if (count>0) if (count>0)
gameboard[i][j].settype(count); gameboard[i][j].setType(count);
} }
block::block() { Block::Block() {
xcord = 0; mX = 0;
ycord = 0; mY = 0;
revealed = false; mRevealed = false;
marked = false; mMarked = false;
type = 0; mType = 0;
} }
block::block(int x, int y, int btype) { Block::Block(int x, int y, int btype) {
xcord = x; mX = x;
ycord = y; mY = y;
revealed = false; mRevealed = false;
marked = false; mMarked = false;
type = btype; mType = btype;
} }
/////////////////////// ///////////////////////
// Mutators // Mutators
/////////////////////// ///////////////////////
block::setx(int x) { void Block::setX(int x) {
xcord = x; mX = x;
} }
block::sety(int y) { void Block::setY(int y) {
ycord = y; mY = y;
} }
block::setrevealed(bool reveal) { void Block::setRevealed(bool reveal) {
revealed = reveal; mRevealed = reveal;
} }
block::setmarked(bool mark) { void Block::setMarked(bool mark) {
marked = mark; mMarked = mark;
} }
block::settype(int btype) { void Block::setType(int btype) {
type = btype; mType = btype;
}
block::formatblock(int x, int y) {
} }
/////////////////////// ///////////////////////
// Accessors // Accessors
/////////////////////// ///////////////////////
int block::getx() { int Block::getX() {
return xcord; return mX;
} }
int block::gety() { int Block::getY() {
return ycord; return mY;
} }
bool block::getrevealed() { bool Block::isRevealed() {
return revealed; return mRevealed;
} }
bool block::getmarked() { bool Block::isMarked() {
return marked; return mMarked;
} }
int block::gettype() { int Block::getType() {
return type; return mType;
} }