Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Leonard Craft, 700636514
- Minesweeper.c
- Update log:
- Start - 4/13/17
- Boards - 4/15/17
- Primary work - 4/20/17
- Flood fill - 4/24/17
- This program plays a game of Minesweeper.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- typedef struct tile
- {
- int isMine;
- int isRevealed;
- int numberOfMines;
- } tile_t;
- /* Prints the board. If the tile hasn't been revealed yet, print -; if
- the tile was revealed and was a mine, print *; if the tile was revealed
- and wasn't a mine, print the number of mines in its neighboring squares.
- */
- void printBoard(tile_t** grid)
- {
- int i, j;
- printf("\nColumn: ");
- for (j = 0; j < 10; j++)
- {
- printf("%-3d", j);
- }
- printf("\nRow:\n");
- for (i = 0; i < 10; i++)
- {
- printf(" %d ", i);
- for (j = 0; j < 10; j++)
- {
- if (grid[i][j].isRevealed)
- {
- if (grid[i][j].isMine)
- {
- printf("%-3c", '*');
- }
- else
- {
- printf("%-3d", grid[i][j].numberOfMines);
- }
- }
- else
- {
- printf("%-3c", '-');
- }
- }
- printf("\n");
- }
- }
- /* Cycles through neighboring squares, and returns the number of mines
- found. */
- int numberOfMines(tile_t** grid, int i, int j)
- {
- int result = 0;
- //Top-Left
- if (i - 1 >= 0 && j - 1 >= 0)
- {
- if (grid[i - 1][j - 1].isMine)
- {
- result++;
- }
- }
- //Top-Center
- if (i - 1 >= 0)
- {
- if (grid[i - 1][j].isMine)
- {
- result++;
- }
- }
- //Top-Right
- if (i - 1 >= 0 && j + 1 < 10)
- {
- if (grid[i - 1][j + 1].isMine)
- {
- result++;
- }
- }
- //Middle-Right
- if (j + 1 < 10)
- {
- if (grid[i][j + 1].isMine)
- {
- result++;
- }
- }
- //Bottom-Right
- if (i + 1 < 10 && j + 1 < 10)
- {
- if (grid[i + 1][j + 1].isMine)
- {
- result++;
- }
- }
- //Bottom-Middle
- if (i + 1 < 10)
- {
- if (grid[i + 1][j].isMine)
- {
- result++;
- }
- }
- //Bottom-Left
- if (i + 1 < 10 && j - 1 >= 0)
- {
- if (grid[i + 1][j - 1].isMine)
- {
- result++;
- }
- }
- //Middle-Left
- if (j - 1 >= 0)
- {
- if (grid[i][j - 1].isMine)
- {
- result++;
- }
- }
- return result;
- }
- /* Checks each tile surrounding the current tile; if that tile also has
- no mines surrounding it, check each tile surrounding that tile, and so
- on. */
- void floodFill(tile_t** grid, int i, int j)
- {
- //Top-Left
- if (i - 1 >= 0 && j - 1 >= 0)
- {
- if (!(grid[i-1][j-1].isRevealed))
- {
- grid[i-1][j-1].isRevealed = 1;
- if (grid[i-1][j-1].numberOfMines == 0)
- {
- floodFill(grid, i-1, j-1);
- }
- }
- }
- //Top-Center
- if (i - 1 >= 0)
- {
- if (!(grid[i-1][j].isRevealed))
- {
- grid[i-1][j].isRevealed = 1;
- if (grid[i-1][j].numberOfMines == 0)
- {
- floodFill(grid, i-1, j);
- }
- }
- }
- //Top-Right
- if (i - 1 >= 0 && j + 1 < 10)
- {
- if (!(grid[i-1][j+1].isRevealed))
- {
- grid[i-1][j+1].isRevealed = 1;
- if (grid[i-1][j+1].numberOfMines == 0)
- {
- floodFill(grid, i-1, j+1);
- }
- }
- }
- //Middle-Right
- if (j + 1 < 10)
- {
- if (!(grid[i][j+1].isRevealed))
- {
- grid[i][j+1].isRevealed = 1;
- if (grid[i][j+1].numberOfMines == 0)
- {
- floodFill(grid, i, j+1);
- }
- }
- }
- //Bottom-Right
- if (i + 1 < 10 && j + 1 < 10)
- {
- if (!(grid[i+1][j+1].isRevealed))
- {
- grid[i+1][j+1].isRevealed = 1;
- if (grid[i+1][j+1].numberOfMines == 0)
- {
- floodFill(grid, i+1, j+1);
- }
- }
- }
- //Bottom-Middle
- if (i + 1 < 10)
- {
- if (!(grid[i+1][j].isRevealed))
- {
- grid[i+1][j].isRevealed = 1;
- if (grid[i+1][j].numberOfMines == 0)
- {
- floodFill(grid, (i+1), j);
- }
- }
- }
- //Bottom-Left
- if (i + 1 < 10 && j - 1 >= 0)
- {
- if (!(grid[i+1][j-1].isRevealed))
- {
- grid[i+1][j-1].isRevealed = 1;
- if (grid[i+1][j-1].numberOfMines == 0)
- {
- floodFill(grid, (i+1), (j-1));
- }
- }
- }
- //Middle-Left
- if (j - 1 >= 0)
- {
- if (!(grid[i][j-1].isRevealed))
- {
- grid[i][j-1].isRevealed = 1;
- if (grid[i][j-1].numberOfMines == 0)
- {
- floodFill(grid, i, (j-1));
- }
- }
- }
- }
- /* Cycles through the board to check if the player has won. */
- int checkWin(tile_t** grid)
- {
- int hasWon = 1;
- int i, j;
- for (i = 0; i < 10; i++)
- {
- for (j = 0; j < 10; j++)
- {
- //If there is an unrevealed non-mine tile, the player continues.
- if (grid[i][j].isRevealed == 0 && grid[i][j].isMine == 0)
- {
- hasWon = 0;
- break;
- }
- }
- }
- return hasWon;
- }
- int main(int argc, char* argv[])
- {
- srand(time(NULL));
- tile_t** grid;
- int i, j, m;
- int mineNotSet;
- int hasWon;
- //Allocating memory for the grid.
- grid = (tile_t**) malloc(sizeof(tile_t*) * 10);
- for (i = 0; i < 10; i++)
- {
- grid[i] = (tile_t*) malloc(sizeof(tile_t) * 10);
- for (j = 0; j < 10; j++)
- {
- grid[i][j].isMine = 0;
- grid[i][j].isRevealed = 0;
- }
- }
- //Randomly fills grid with 10 mines.
- for (m = 0; m < 10; m++)
- {
- /* Checks for extra conditions; don't put a mine at 0x0 or where one
- has already been placed. */
- mineNotSet = 1;
- while (mineNotSet)
- {
- i = rand() % 10;
- j = rand() % 10;
- if (!((i == 0 && j == 0) || grid[i][j].isMine == 1))
- {
- grid[i][j].isMine = 1;
- mineNotSet = 0;
- }
- }
- }
- //Calculates the number of mines surrounding each tile.
- for (i = 0; i < 10; i++)
- {
- for (j = 0; j < 10; j++)
- {
- grid[i][j].numberOfMines = numberOfMines(grid, i, j);
- }
- }
- printf("********************************\nWelcome to Minesweeper!\n");
- printBoard(grid);
- hasWon = 0;
- while (!hasWon)
- {
- printf("Please enter the row and column to sweep: ");
- scanf("%d %d", &i, &j);
- //Cheat code to insta-win so I don't have to play through a full game.
- if (i == 99 && j == 99)
- {
- hasWon = 1;
- break;
- }
- //Checks to make sure user entered a valid row / column.
- while (i < 0 || i > 9 || j < 0 || j > 9)
- {
- printf("Invalid row / column. Please enter the row and column to sweep: ");
- scanf("%d %d", &i, &j);
- }
- grid[i][j].isRevealed = 1;
- /*If the square revealed has 0 mines around it, keep revealing
- squares until ones with mines are found. */
- if (!(grid[i][j].isMine) && grid[i][j].numberOfMines == 0)
- {
- floodFill(grid, i, j);
- }
- printBoard(grid);
- if (grid[i][j].isMine)
- {
- printf("You hit a mine! You lose.\n");
- break;
- }
- hasWon = checkWin(grid);
- }
- if (hasWon)
- {
- printf("Congratulations! You revealed all the squares correctly.\n");
- }
- free(grid);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement