Untitled
unknown
c_cpp
4 years ago
4.9 kB
6
Indexable
#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_SQUARE 2 #define REVEAL_CROSS 3 #define GAME_MODE 4 #define FLAG_MINE 5 #define DEFUSE 6 // Add any extra #defines here. void initialise_field(int minefield[SIZE][SIZE]); void print_debug_minefield(int minefield[SIZE][SIZE]); // Place your function prototyes here. int main(void) { int minefield[SIZE][SIZE]; int mine_num; initialise_field(minefield); printf("Welcome to minesweeper!\n"); printf("How many mines? "); scanf("%d", &mine_num); // TODO: Scan in the number of pairs of mines. printf("Enter pairs:\n"); for(int i=0; i<mine_num; i++) { int a,b; scanf("%d %d",&a,&b); if (a > SIZE-1 || b > SIZE-1 || a < 0 || b < 0) continue; minefield[a][b] = HIDDEN_MINE; } // TODO: Scan in the pairs of mines and place them on the grid. printf("Game Started\n"); print_debug_minefield(minefield); int command = 0; while(1) { scanf("%d",&command); if(command==1) { //Command 1 code here int r,c,leng; int count = 0; scanf("%d %d %d",&r,&c,&leng); if (r > SIZE-1 || c > SIZE-1 || r < 0 || c < 0){ printf("Coordinates not on map\n"); print_debug_minefield(minefield); } else{ for(int j=c; j<=(c+leng-1); j++) { if(minefield[r][j]==HIDDEN_MINE) { count++; } } printf("There are %d mine(s) in row %d, from column %d to %d\n",count, r, c, (c+leng-1)); print_debug_minefield(minefield); } } else if(command==2) { //command 2 code here int r,c,s; int count = 0; scanf("%d %d %d",&r,&c,&s); for(int i=(r-(s/2)); i<=(r+(s/2)); i++) { for(int j=(c-(s/2)); j<=(c+(s/2)); j++) { if(minefield[i][j]==HIDDEN_MINE) { count++; } } } printf("\n%d\n",count); } else if(command==3) { printf("Test command 3"); //command 3 code here int r,c; scanf("%d %d ",&r,&c); if(minefield[r][c]==HIDDEN_MINE){ printf("Game Over"); } if(checkAdjacentMines(minefield,r-1,c)==0){ minefield[r-1][c] = VISIBLE_SAFE; } if(checkAdjacentMines(minefield,r+1,c)==0){ minefield[r+1][c] = VISIBLE_SAFE; } if(checkAdjacentMines(minefield,r,c-1)==0){ minefield[r][c-1] = VISIBLE_SAFE; } if(checkAdjacentMines(minefield,r,c+1)==0){ minefield[r][c+1] = VISIBLE_SAFE; } print_debug_minefield(minefield); } else if(command==4) { //command 4 code here } else if(command==5) { //command 5 code here } else if(command==6) { //command 6 code here } } // TODO: Scan in commands to play the game until the game ends. // 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++; } } int checkAdjacentMines(int minefield[SIZE][SIZE],int r, int c) { for(int i = r-1; i<r+1; i++) { for(int j=c-1; j<c+1; j++) { if(minefield[i][j]==HIDDEN_MINE) { return 1; } } } return 0; }
Editor is loading...