Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Assignment 1 20T1 COMP1511: Minesweeper
- // minesweeper.c
- //
- // This program was written by Tam Do (z5312910)
- // on 07/03/2020
- //
- // Version 1.0.0 (2020-03-08): Assignment released.
- // Version 1.0.1 (2020-03-08): Fix punctuation in comment.
- // Version 1.0.2 (2020-03-08): Fix second line of header comment to say minesweeper.c
- #include <stdio.h>
- #include <stdlib.h>
- // Possible square states.
- #define VISIBLE_SAFE 0
- #define HIDDEN_SAFE 1
- #define HIDDEN_MINE 2
- // The size of the starting grid.
- #define SIZE 8
- // The possible command codes.
- #define DETECT_ROW 1
- #define DETECT_COL 2
- #define DETECT_SQUARE 3
- #define REVEAL_SQUARE 4
- #define GAMEPLAY_MODE 5
- #define DEBUG_MODE 6
- #define REVEAL_RADIAL 7
- // Add any extra #defines here.
- void initialise_field(int minefield[SIZE][SIZE]);
- void print_debug_minefield(int minefield[SIZE][SIZE]);
- // Game check function
- int game_check(int gameOver, int minefield[SIZE][SIZE]);
- // Detect 3x3 function
- int square_detect(int minefield[SIZE][SIZE], int sizeDetect, int rowOrCol, int colOrRow);
- // Detect row or column
- void detect_row_col(int integer, int minefield[SIZE][SIZE], int hintCounter);
- // Valid input check
- int valid_check(int minefield[SIZE][SIZE], int rowOrCol, int colOrRow, int numberOfMines);
- // Reveal square 3x3
- void reveal_square(int minefield[SIZE][SIZE], int rowOrCol, int colOrRow);
- // Gameplay Mode
- void gameplay_mode(int minefield[SIZE][SIZE], int gameOver, int gameLost);
- // Reveal radial
- void reveal_radial(int minefield[SIZE][SIZE], int rowX, int colY);
- // Mine check
- int check_mines(int minefield[SIZE][SIZE], int rowX, int colY);
- // Place your function prototyes here.
- int main(void) {
- int minefield[SIZE][SIZE];
- initialise_field(minefield);
- int gamePlayMode = 0; // Gameplay mode (false)
- printf("Welcome to minesweeper!\n");
- printf("How many mines? ");
- // TODO: Scan in the number of pairs of mines.
- int numMine;
- scanf("%d",&numMine);
- printf("Enter pairs:\n");
- int numRow;
- int numCol;
- int counterMine = 0;
- while (counterMine < numMine) {
- scanf("%d %d", &numRow, &numCol);
- if (numRow >= 0 && numRow <= 7) {
- if (numCol >= 0 && numCol <= 7) {
- if (minefield[numRow][numCol] != HIDDEN_MINE) {
- minefield[numRow][numCol] = HIDDEN_MINE;
- }
- }
- }
- counterMine++;
- }
- // TODO: Scan in the pairs of mines and place them on the grid.
- printf("Game Started\n");
- print_debug_minefield(minefield);
- // TODO: Scan in commands to play the game until the game ends.
- int integer = 0;
- int gameOver = 0; // Game Over mechanic
- int gameLost = 0;
- int hintCounter = 0;
- while (gameOver == 0) {
- int end = scanf("%d", &integer);
- if (end == EOF){
- break;
- }
- // Detect row and column
- if (integer == DETECT_ROW || integer == DETECT_COL) {
- detect_row_col(integer, minefield, hintCounter);
- hintCounter++;
- }
- if (integer == DETECT_SQUARE) { // Detect square
- int rowOrCol = 0;
- int colOrRow = 0;
- int sizeDetect = 0;
- scanf(" %d %d %d", &rowOrCol, &colOrRow, &sizeDetect);
- int numMines = square_detect(minefield, sizeDetect, rowOrCol, colOrRow);
- if (hintCounter >= 3) {
- printf("Help already used\n");
- } else {
- printf("There are %d mine(s) in the square centered at row %d, column %d of size %d\n", numMines, rowOrCol, colOrRow, sizeDetect);
- hintCounter++;
- }
- }
- if (integer == REVEAL_SQUARE) { // Reveal square
- int rowOrCol = 0;
- int colOrRow = 0;
- int numberOfMines = 0;
- scanf(" %d %d", &rowOrCol, &colOrRow);
- int numMines = valid_check(minefield, rowOrCol, colOrRow, numberOfMines);
- if (minefield[rowOrCol][colOrRow] == HIDDEN_MINE) { // Lose
- gameOver++;
- printf("Game over\n");
- if (gamePlayMode == 1) { // Lose in gameplay mode
- printf("xx\n");
- printf("/\\\n");
- gameLost = 1;
- }
- }
- if (numMines > 0 && gameOver == 0) { // Reveal 1x1
- minefield[rowOrCol][colOrRow] = VISIBLE_SAFE;
- int score = game_check(gameOver, minefield);
- gameOver = gameOver + score;
- }
- if (numMines == 0 && gameOver == 0) { // Reveal 3x3
- reveal_square(minefield, rowOrCol, colOrRow);
- int score = game_check(gameOver, minefield);
- gameOver = gameOver + score;
- }
- }
- if (integer == GAMEPLAY_MODE) {
- printf("Gameplay mode activated\n");
- gamePlayMode = 1;
- }
- if (integer == DEBUG_MODE) {
- printf ("Debug mode activated\n");
- gamePlayMode = 0;
- }
- if (integer == REVEAL_RADIAL) {
- int rowX;
- int colY;
- scanf(" %d %d", &rowX, &colY);
- reveal_radial(minefield, rowX, colY);
- }
- // Printing the minefield
- if (gamePlayMode == 0) {
- print_debug_minefield(minefield);
- } else {
- gameplay_mode(minefield, gameOver, gameLost);
- }
- // A game ends when the player wins, loses, or enters EOF (Ctrl+D).
- // You should display the minefield after each command has been processed.
- }
- return 0;
- }
- // Set the entire minefield to HIDDEN_SAFE.
- void initialise_field(int minefield[SIZE][SIZE]) {
- int i = 0;
- while (i < SIZE) {
- int j = 0;
- while (j < SIZE) {
- minefield[i][j] = HIDDEN_SAFE;
- j++;
- }
- i++;
- }
- }
- // Print out the actual values of the minefield.
- void print_debug_minefield(int minefield[SIZE][SIZE]) {
- int i = 0;
- while (i < SIZE) {
- int j = 0;
- while (j < SIZE) {
- printf("%d ", minefield[i][j]);
- j++;
- }
- printf("\n");
- i++;
- }
- }
- // Detect row or column
- void detect_row_col(int integer, int minefield[SIZE][SIZE], int hintCounter) {
- if (integer == DETECT_ROW) { // Detect Row
- int rowOrCol = 0;
- scanf(" %d", &rowOrCol);
- int counterRow = 0;
- int numberOfMines = 0;
- while (counterRow < SIZE) {
- if (minefield[rowOrCol][counterRow] == 2) {
- numberOfMines++;
- }
- counterRow++;
- }
- if (hintCounter >= 3) {
- printf("Help already used\n");
- } else {
- printf("There are %d mine(s) in row %d\n", numberOfMines, rowOrCol);
- }
- }
- if (integer == DETECT_COL) { // Detect Column
- int rowOrCol = 0;
- scanf(" %d", &rowOrCol);
- int counterCol = 0;
- int numberOfMines = 0;
- while (counterCol < SIZE) {
- if (minefield[counterCol][rowOrCol] == 2) {
- numberOfMines++;
- }
- counterCol++;
- }
- if (hintCounter >= 3) {
- printf("Help already used\n");
- } else {
- printf("There are %d mine(s) in column %d\n", numberOfMines, rowOrCol);
- }
- }
- }
- // Detect 3x3 for number of mines.
- int square_detect(int minefield[SIZE][SIZE], int sizeDetect, int rowOrCol, int colOrRow) {
- int numberOfMines = 0;
- int rowCounter = -1;
- while (rowCounter < sizeDetect - 1) {
- int colCounter = -1;
- while (colCounter < sizeDetect - 1) {
- // Valid input mechanism
- if (rowOrCol + rowCounter >= 0 && rowOrCol + rowCounter <= 7) {
- if (colOrRow + colCounter >= 0 && colOrRow + colCounter <= 7) {
- if (minefield[rowOrCol + rowCounter][colOrRow +
- colCounter] == HIDDEN_MINE){
- numberOfMines++;
- }
- }
- }
- colCounter++;
- }
- rowCounter++;
- }
- return numberOfMines;
- }
- // Valid input check
- int valid_check(int minefield[SIZE][SIZE], int rowOrCol, int colOrRow, int numberOfMines) {
- int rowCounter = -1;
- while (rowCounter < 2) { // Checking mechanism
- int colCounter = -1;
- while (colCounter < 2) {
- // Valid input mechanism
- if (rowOrCol + rowCounter >= 0 && rowOrCol + rowCounter <= 7) {
- if (colOrRow + colCounter >= 0 && colOrRow + colCounter <= 7) {
- if (minefield[rowOrCol + rowCounter][colOrRow +
- colCounter] == HIDDEN_MINE){
- numberOfMines++;
- }
- }
- }
- colCounter++;
- }
- rowCounter++;
- }
- return numberOfMines;
- }
- // Check to see if game is won
- int game_check(int gameOver, int minefield[SIZE][SIZE]) {
- int gameCounter = 0;
- if (gameOver == 0) {
- int untouchedSquare = 0;
- int gameRow = 0;
- while (gameRow < SIZE) {
- int gameCol = 0;
- while (gameCol < SIZE) {
- if (minefield[gameRow][gameCol] == HIDDEN_SAFE) {
- untouchedSquare++;
- }
- gameCol++;
- }
- gameRow++;
- }
- if (untouchedSquare == 0) {
- printf("Game Won!\n");
- gameCounter++;
- }
- }
- return gameCounter;
- }
- // Reveal square
- void reveal_square(int minefield[SIZE][SIZE], int rowOrCol, int colOrRow) {
- int rowCounter = -1;
- while (rowCounter < 2) {
- int colCounter = -1;
- while (colCounter < 2) {
- if (rowOrCol + rowCounter >= 0 && rowOrCol + rowCounter <= 7) {
- if (colOrRow + colCounter >= 0 && colOrRow + colCounter <= 7) {
- minefield[rowOrCol + rowCounter][colOrRow +
- colCounter] = VISIBLE_SAFE;
- }
- }
- colCounter++;
- }
- rowCounter++;
- }
- }
- // Gameplay mode activated
- void gameplay_mode(int minefield[SIZE][SIZE], int gameOver, int gameLost) {
- if (gameLost == 0) {
- printf("..\n");
- printf("\\/\n");
- }
- printf(" 00 01 02 03 04 05 06 07\n");
- printf(" -------------------------\n");
- int i = 0;
- while (i < SIZE) {
- printf("0%d ", i);
- int j = 0;
- while (j < SIZE) {
- int numberOfMines = 0;
- int rowCounter = -1;
- while (rowCounter < 2) { // Checking mechanism
- int colCounter = -1;
- while (colCounter < 2) {
- // Valid input mechanism
- if (i + rowCounter >= 0 && i + rowCounter <= 7) {
- if (j + colCounter >= 0 && j + colCounter <= 7) {
- if (minefield[i + rowCounter][j +
- colCounter] == HIDDEN_MINE){
- numberOfMines++;
- }
- }
- }
- colCounter++;
- }
- rowCounter++;
- }
- if (j == 0) {
- printf("|");
- }
- if (minefield[i][j] == VISIBLE_SAFE && numberOfMines == 0) {
- printf(" ");
- } else if (minefield[i][j] == VISIBLE_SAFE && numberOfMines > 0) {
- printf("%.2d", numberOfMines);
- } else if (minefield[i][j] == HIDDEN_SAFE) {
- printf("##");
- } else if (minefield[i][j] == HIDDEN_MINE && gameLost == 1) {
- printf("()");
- } else if (minefield[i][j] == HIDDEN_MINE) {
- printf("##");
- }
- if (j == 7) {
- printf("|");
- } else {
- printf(" ");
- }
- j++;
- }
- printf("\n");
- i++;
- }
- printf(" -------------------------\n");
- }
- // Mine check
- int check_mines(int minefield[SIZE][SIZE], int rowX, int colY) {
- int numberOfMines = 0;
- int rowCounter = -1;
- while (rowCounter < 2) { // Checking mechanism
- int colCounter = -1;
- while (colCounter < 2) {
- // Valid input mechanism
- if (rowX + rowCounter >= 0 && rowX + rowCounter <= 7) {
- if (colY + colCounter >= 0 && colY + colCounter <= 7) {
- if (minefield[rowX + rowCounter][colY +
- colCounter] == HIDDEN_MINE){
- numberOfMines++;
- }
- }
- }
- colCounter++;
- }
- rowCounter++;
- }
- return numberOfMines;
- }
- // Reveal radial
- void reveal_radial(int minefield[SIZE][SIZE], int rowX, int colY) {
- int numRow = 0;
- while (numRow < SIZE - rowX) { // South
- int numMines = check_mines(minefield, rowX, colY);
- if (numMines == 0) {
- minefield[rowX + numRow][colY] = VISIBLE_SAFE;
- }
- numRow++;
- }
- /*
- int northCounter = 0;
- while (northCounter > -1 * rowX) { // North
- int numCol = 0;
- int numMines = check_mines(minefield, rowX, colY, numRow, numCol);
- if (numMines == 0) {
- if (minefield[rowX + numRow][colY + numCol] != HIDDEN_MINE) {
- minefield[rowX + numRow][colY + numCol] = VISIBLE_SAFE;
- }
- }
- northCounter--;
- }
- while () { // East
- }
- while () { // West
- }
- while () { // North East
- }
- while () { // North West
- }
- while () { // South East
- }
- while () { // South West
- }
- */
- }
- /*
- int rowCounter = -1;
- while (rowCounter < 2) { // Checking mechanism
- int colCounter = -1;
- while (colCounter < 2) {
- // Valid input mechanism
- if (rowOrCol + rowCounter >= 0 && rowOrCol + rowCounter <= 7) {
- if (colOrRow + colCounter >= 0 && colOrRow + colCounter <= 7) {
- if (minefield[rowOrCol + rowCounter][colOrRow +
- colCounter] == HIDDEN_MINE){
- numberOfMines++;
- }
- }
- }
- colCounter++;
- }
- rowCounter++;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement