Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // sapper game 1.0
- // author glenpl
- #include <iostream>
- #include <time.h>
- #include <windows.h>
- #include <cstdlib>
- #include <stdio.h>
- #include <conio.h>
- using namespace std;
- typedef unsigned int UI;
- const UI BOARD_SIZE = 9; // board is a grid of x * x fields
- const UI BOMBS = 9; // number of bombs
- const bool SHOW_ZEROES = false; // show all zero-valued fields
- // special values of fields
- const UI CLEAR = 0;
- const UI BOMB = 9;
- const UI MARKED = 10;
- const UI SHOWN = 11;
- const UI HIDDEN = 12;
- // boards
- string board[BOARD_SIZE][BOARD_SIZE]; // board that will be displayed to user
- UI board_int[BOARD_SIZE][BOARD_SIZE]; // board with actual fields' values
- UI shown[BOARD_SIZE][BOARD_SIZE]; // board with values: marked, hidden, shown for each field
- // some global variables... :(
- UI flags = 0;
- bool lose = false;
- int hidden_fields = BOARD_SIZE * BOARD_SIZE;
- // function index
- void update_board();
- UI count_bombs_around(int, int);
- void print_board();
- void init_game();
- void do_action(string, UI, UI);
- string itos(int);
- // convert int to string
- string itos(int n)
- {
- string tmp;
- if(n < 0)
- {
- tmp = "-";
- n = -n;
- }
- if(n > 9)
- tmp += itos(n / 10);
- tmp += n % 10 + 48;
- return tmp;
- }
- void print_board()
- {
- system("CLS");
- printf(" ");
- for(UI i = 0; i < BOARD_SIZE; i++)
- printf("%i ", i+1);
- printf("\n ");
- for(UI i = 0; i < BOARD_SIZE*4-1; i++)
- printf("_");
- printf("\n");
- for(UI i = 0; i < BOARD_SIZE; i++)
- {
- printf(" %i | ", i+1);
- for(UI j = 0; j < BOARD_SIZE; j++)
- cout << board[i][j] << " | ";
- printf("%i\n ", i+1);
- if(i < BOARD_SIZE-1) printf("|");
- if(i < BOARD_SIZE-1)
- {
- for(UI j = 0; j < BOARD_SIZE-1; j++)
- printf("---+");
- printf("---|\n");
- }
- }
- printf(" ");
- for(UI i = 0; i < BOARD_SIZE*4-1; i++)
- printf("-");
- printf("\n");
- }
- void init_game()
- {
- // init formal board with 'X' characters
- // init board with 0 values
- // no val has been shown yet - all fields are hidden
- for(UI i = 0; i < BOARD_SIZE; i++)
- for(UI j = 0; j < BOARD_SIZE; j++)
- {
- board[i][j] = 'X';
- board_int[i][j] = 0;
- shown[i][j] = HIDDEN;
- }
- // sets bombs on the board
- UI dec_bombs = 0;
- while(dec_bombs != BOMBS)
- {
- UI a = rand()%BOARD_SIZE;
- UI b = rand()%BOARD_SIZE;
- if(board_int[a][b] != BOMB)
- {
- board_int[a][b] = BOMB;
- dec_bombs++;
- }
- }
- hidden_fields = BOARD_SIZE * BOARD_SIZE;
- // set values of all other fields
- for(UI i = 0; i < BOARD_SIZE; i++)
- for(UI j = 0; j < BOARD_SIZE; j++)
- if(board_int[i][j] != BOMB)
- {
- board_int[i][j] = count_bombs_around(i, j);
- // if SHOW_ZEROES is true - shows zero-valued fields
- if(SHOW_ZEROES)
- if(board_int[i][j] == 0)
- {
- shown[i][j] = SHOWN;
- --hidden_fields;
- }
- }
- lose = false;
- flags = 0;
- update_board();
- }
- UI count_bombs_around(int x, int y)
- {
- // counts bombs around XY field
- int a, b, num = 0;
- for(int i = x - 1; i <= x + 1; i++)
- {
- a = i;
- if(a < 0)
- continue;
- if(a > (BOARD_SIZE-1))
- continue;
- for(int j = y - 1; j <= y + 1; j++)
- {
- b = j;
- if(b < 0)
- continue;
- if(b > (BOARD_SIZE-1))
- continue;
- if (board_int[a][b] == BOMB)
- num++;
- }
- }
- return num;
- }
- void update_board()
- {
- // update all fields of formal board
- for(UI i = 0; i < BOARD_SIZE; i++)
- for(UI j = 0; j < BOARD_SIZE; j++)
- {
- if(shown[i][j] == HIDDEN)
- board[i][j] = 'X';
- else
- {
- if(shown[i][j] == MARKED)
- board[i][j] = 'M';
- else if(board_int[i][j] == BOMB)
- {
- board[i][j] = 'B';
- lose = true;
- }
- else
- board[i][j] = itos(board_int[i][j]);
- }
- }
- }
- void do_action(string action, UI x, UI y)
- {
- if(action == "mark")
- {
- if(shown[x][y] == HIDDEN)
- {
- if(flags < BOMBS)
- {
- shown[x][y] = MARKED;
- flags++;
- }
- }
- else if(shown[x][y] == MARKED)
- {
- shown[x][y] = HIDDEN;
- flags--;
- }
- }
- if(action == "show")
- if(shown[x][y] == HIDDEN)
- {
- shown[x][y] = SHOWN;
- --hidden_fields;
- }
- }
- int main()
- {
- srand(time(NULL));
- // initialize boards etc
- init_game();
- //print board for the first time
- print_board();
- int x, y;
- string action;
- for(;;)
- {
- while(lose == false)
- {
- cout << "Flags left: " << BOMBS - flags << "\n";
- cout << "Tell me what to do! (action[show/mark], row[1-9] , col[1-9]):\n>";
- cin>>action>>x>>y;
- do_action(action, x-1, y-1);
- update_board();
- print_board();
- if(flags == BOMBS && hidden_fields == BOMBS)
- {
- cout << "\n\nCongratulations! You marked all bombs correctly and saved many human beings!\n";
- break;
- }
- }
- if(lose == true)
- cout << "\n\nWhat a pitty! You stepped on a bomb!\n";
- cout<<"Would you like to play once again? [yes/no]\n";
- string answer;
- while(answer != "yes" && answer != "no")
- {
- cout<<">";
- cin>>answer;
- }
- if(answer == "no")
- {
- cout << "Ok, goodbye! :) [press any button]";
- getch();
- return 0;
- }
- if(answer == "yes")
- {
- cout<<"Here we go! [press any button]";
- init_game();
- getch();
- print_board();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement