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 Brian Lee (z5308686)
- // on 13-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]);
- void row_detector(int userInput, int minefield[SIZE][SIZE]);
- void col_detector(int userInput, int minefield[SIZE][SIZE]);
- void square_detector(int rowSquare, int colSquare, int sizeSquare, int minefield[SIZE][SIZE]);
- // Place your function prototyes here.
- int main(void) {
- int minefield[SIZE][SIZE];
- int noMines = 0;
- initialise_field(minefield);
- printf("Welcome to minesweeper!\n");
- printf("How many mines? ");
- scanf("%d", &noMines);
- int row = 0;
- int col = 0;
- int i = 0;
- printf("Enter pairs:\n");
- while(i < noMines) {
- scanf("%d %d", &row, &col);
- if(row >= 0 && col >= 0 && row <=SIZE && col <= SIZE) {
- minefield[row][col] = HIDDEN_MINE;
- }
- i++;
- }
- printf("Game Started\n");
- print_debug_minefield(minefield);
- int command = 0;
- int userInput = 0;
- int rowSquare = 0;
- int colSquare = 0;
- int sizeSquare = 0;
- while(scanf("%d", &command) == 1) {
- if(command == DETECT_ROW) {
- scanf("%d", &userInput);
- row_detector(userInput, minefield);
- }
- if(command == DETECT_COL) {
- scanf("%d", &userInput);
- col_detector(userInput, minefield);
- }
- if(command == DETECT_SQUARE) {
- scanf("%d", &rowSquare);
- scanf("%d", &colSquare);
- scanf("%d", &sizeSquare);
- square_detector(rowSquare, colSquare, sizeSquare, minefield);
- }
- }
- 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 Rows
- void row_detector(int userInput, int minefield[SIZE][SIZE]){
- int rowScanner = 0;
- int mineCounter = 0;
- while(rowScanner < SIZE) {
- if (minefield[userInput][rowScanner] == HIDDEN_MINE) {
- mineCounter++;
- }
- rowScanner++;
- }
- printf("There are %d mine(s) in row %d\n", mineCounter, userInput);
- print_debug_minefield(minefield);
- }
- // Detect Columns
- void col_detector(int userInput, int minefield[SIZE][SIZE]){
- int colScanner = 0;
- int mineCounter = 0;
- while(colScanner < SIZE) {
- if (minefield[colScanner][userInput] == HIDDEN_MINE) {
- mineCounter++;
- }
- colScanner++;
- }
- printf("There are %d mine(s) in column %d\n", mineCounter, userInput);
- print_debug_minefield(minefield);
- }
- // Detect Squares
- void square_detector(int rowSquare, int colSquare, int sizeSquare, int minefield[SIZE][SIZE]) {
- int mineSquareCounter = 0;
- if(rowSquare >= 0 && colSquare >= 0 && rowSquare <=SIZE && colSquare <= SIZE) {
- int i = rowSquare - (sizeSquare / 2);
- while(i < rowSquare + (sizeSquare / 2) + 1) {
- int a = colSquare - (sizeSquare / 2);
- while(a < colSquare + (colSquare / 2) + 1) {
- if(minefield[rowSquare][colSquare] == HIDDEN_MINE) {
- mineSquareCounter++;
- }
- a++;
- }
- i++;
- }
- }
- printf("There are %d mine(s) in the square centred at row %d, column %d of size %d", mineSquareCounter, rowSquare, colSquare, sizeSquare);
- }
- // Reveal Squares
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement