#include #include #include #include const int MAXROWS = 15; const int MAXCOLS = 20; const int BOMBS = 50; enum SquareType {EMPTY, BOMB, BOMBMARKED, EMPTYMARKED, STEPPEDON,EXPLODED}; using namespace std; void init_board(SquareType board[][MAXCOLS]); void play_game(SquareType board[][MAXCOLS]); int main () { SquareType board[MAXROWS][MAXCOLS]; srand(time(NULL)); init_board(board); play_game(board); } void place_bombs(SquareType board[][MAXCOLS], int bombs){ int x, y; if (bombs != 0) { x = rand() % MAXROWS; y = rand() % MAXCOLS; if (board[x][y] == 0) { board[x][y] = BOMB; place_bombs(board, bombs-1); } else { place_bombs(board,bombs); } } } void init_board(SquareType board[][MAXCOLS]){ int i,j; for(i=0;i=0) && (x < MAXROWS) && (y >= 0) && (y < MAXCOLS)) ; } int mt_surround(SquareType board[][MAXCOLS],int x, int y) { int i,j; int things = 0; for(i=-1;i<2;i++) { for(j=-1;j<2;j++) { if (valid_position(x+i,y+j)) { if (board[x+i][y+j] == STEPPEDON) { things++; } } } } return(things); } int bomb_surround(SquareType board[][MAXCOLS],int x, int y) { int i,j; int things = 0; for(i=-1;i<2;i++) { for(j=-1;j<2;j++) { if (valid_position(x+i,y+j)) { if ((board[x+i][y+j] == BOMB) || (board[x+i][y+j] == BOMBMARKED)){ things++; } } } } return(things); } void print_square(SquareType board[][MAXCOLS], int x, int y) { switch(board[x][y]) { case EMPTY: if (mt_surround(board,x,y) == 0) { cout << "|??"; break; } else { cout << "|" << setw(2) << bomb_surround(board,x,y); } break; case BOMB: if (mt_surround(board,x,y) == 0) { cout << "| B"; break; } else { cout << "|" << setw(2) << bomb_surround(board,x,y); } break; case BOMBMARKED: cout << "| F"; break; case EMPTYMARKED: cout << "| F"; break; case STEPPEDON: cout << "|__"; break; case EXPLODED: cout << "|XX"; break; } } void print_board(SquareType board[][MAXCOLS]){ int i,j; cout << " "; for(j=0;j "; cin >> tmp; cout << endl; if ((tmp == 'M') || (tmp == 'S')) { return(tmp); } else { return(get_move()); } } void get_coord(int & x , int & y) { cout << "Enter the row (0-" << MAXROWS-1 << ") "; cin >> x; cout << endl; cout << "Enter the column (0-" << MAXCOLS-1 << ") "; cin >> y; cout << endl; if (! valid_position(x,y) ) { cout << "Invalid position (" << x << ", " << y << ")" << endl; get_coord(x,y); } } void get_action(char & resp, int & x, int & y) { resp = get_move(); get_coord(x,y); } bool all_empty(SquareType board[][MAXCOLS],int x,int y) { int i,j; bool rv = true; for(i=-1;i<2;i++) { for(j=-1;j<2;j++) { if (valid_position(x+i,y+j)) { if ((board[x+i][y+j] != EMPTY)&&(board[x+i][y+j] != STEPPEDON)) { rv = false; } } } } return(rv); } void extend_step(SquareType board[][MAXCOLS],int x, int y) { if (!valid_position(x,y) ) { return; } if ((board[x][y] == EMPTY) && all_empty(board,x,y)) { cout << "Extending to (" << x << ", " << y << ")" << endl; board[x][y] = STEPPEDON; // extend_step(board,x-1,y-1); extend_step(board,x-1,y); // extend_step(board,x-1,y+1); extend_step(board,x,y-1); extend_step(board,x,y+1); // extend_step(board,x+1,y-1); extend_step(board,x+1,y); // extend_step(board,x+1,y+1); } } bool take_step(SquareType board[][MAXCOLS],int x,int y){ if ((board[x][y] ==BOMB ) || (board[x][y] == BOMBMARKED)) { cout << "Boom " << endl; board[x][y] = EXPLODED; return(true); } else { extend_step(board,x,y); board[x][y] = STEPPEDON; } return(false); } void mark_board(SquareType board[][MAXCOLS], int x, int y) { if (board[x][y] == BOMB) { board[x][y] = BOMBMARKED; } else if (board[x][y] == EMPTY) { board[x][y] = EMPTYMARKED; } } bool take_action(SquareType board[][MAXCOLS],char resp,int x,int y){ switch(resp) { case 'M': mark_board(board,x,y); break; case 'S': return(take_step(board,x,y)); } return(false); } void play_game(SquareType board[][MAXCOLS]){ char resp; int x, y; bool dead; dead= false; while (! dead) { print_board(board); get_action(resp,x,y); dead = take_action(board,resp,x,y); } print_board(board); }