Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <conio.h>
- #define ii pair<int,int>
- #define fi first
- #define se second
- using namespace std;
- //----Variables----//
- int level;
- int bombs_level[5] = {0, 10 , 40 , 99 , -1 };
- ii grid_level[5] = {ii(1,1), ii(9,9) , ii(16,16) , ii(16,30), ii(-1,-1) };
- bool is_bomb[35*35];//
- char board[35][35]; //real board state
- char cur_board[35][35]; //display board state
- int dx[8] = {1,-1,0,0,1,1,-1,-1}; // direction
- int dy[8] = {0,0,1,-1,1,-1,1,-1};
- ii DIR[260];
- int cur_x = 1, cur_y = 1;
- int flag = 0;
- //----------------Clear the console screen-----------------------------//
- void Clear(){
- #if defined _WIN32
- system("cls");
- #elif defined (__LINUX__) || defined(__gnu_linux__) || defined(__linux__)
- system("clear");\
- #elif defined (__APPLE__)
- system("clear");
- #endif
- }
- //-----------Welcome the player-------------------------//
- void Welcome(){
- cout << "--------Welcome to the Minesweeper game!-------";
- cout << endl;
- cout << endl;
- cout << endl;
- cout << " ------------------------- "; cout << endl;
- cout << " |Press enter key to play| "; cout << endl;
- cout << " ------------------------- ";
- cin.ignore();
- Clear();
- }
- //---Chosing level---//
- void Chosing_level(){
- cout << "Chose your level:" << endl;
- cout << "1 is for begginer \n";
- cout << "2 is for intermediate \n";
- cout << "3 is for expert \n";
- cout << "4 is custom game \n";
- cout << "Which level do you want to play? \n";
- cin >> level;
- Clear();
- }
- //---Randomize---//
- int Rand(int l, int r){
- return rand()%(r-l+1);
- }
- //---Convert 2D to 1D and vice versa---//
- int Convert_to_1D(int x, int y){
- return grid_level[level].se*(x-1) + y;
- }
- ii Convert_to_2D(int val){
- int x, y;
- if (val%grid_level[level].se==0){
- y = grid_level[level].se;
- x = val/grid_level[level].se;
- }
- else{
- x = val/grid_level[level].se+1;
- y = val%grid_level[level].se;
- }
- return ii(x,y);
- }
- //---Display the board---//
- void Display_the_board(){
- for(int i=1; i<=grid_level[level].fi; i++){
- if (cur_x==i && cur_y==1) cout << "[";
- else cout << " ";
- for(int j=1; j<=grid_level[level].se; j++){
- cout << cur_board[i][j];
- if (cur_x==i && cur_y==j) cout << "]";
- else if (cur_x==i && j==cur_y-1) cout << "[";
- else cout << " ";
- }
- cout << endl;
- }
- cout << endl;
- cout << "Flag left: " << bombs_level[level] - flag << '\n';
- cout << endl << endl << endl;
- system("Color E4");
- cout << "------------Basic movement keys------------" << endl;
- cout << "Use the arrow keys to move around the board" << endl;
- cout << "'Enter' to open a cell" << endl;
- cout << "'Space' to flag a cell" << endl;
- cout << "'Z' to open every cell around the number cell that all bombs are flagged" << endl;
- cout << "'ESC' to exit the game";
- }
- //---checking border---//
- bool ok(int x, int y){
- return 1 <= x && x <= grid_level[level].fi && 1 <= y && y <= grid_level[level].se;
- }
- //---Preprocess---//
- void Preprocess_the_board(){
- //Preprocess the arrow keys
- DIR[75] = ii(0,-1);
- DIR[77] = ii(0,1);
- DIR[80] = ii(1,0);
- DIR[72] = ii(-1,0);
- //Input custom level
- if (level==4){
- int grid_x, grid_y, bombs;
- cout << "Input your width: "; cin >> grid_x;
- cout << "Input your length: "; cin >> grid_y;
- cout << "Input your number of bombs: "; cin >> bombs;
- Clear();
- bombs_level[4] = bombs;
- grid_level[4] = ii(grid_x,grid_y);
- }
- //generate a board with no bomb
- int wid = grid_level[level].fi;
- int len = grid_level[level].se;
- int num_bomb = bombs_level[level];
- for(int i=1; i<=wid; i++){
- for(int j=1; j<=len; j++){
- board[i][j] = 'O';
- }
- }
- //reset bombs state
- for(int i=1; i <= wid*len; i++) is_bomb[i] = false;
- //randomize bombs
- srand(time(NULL));
- for(int i=1; i<=num_bomb; i++){
- while (1){
- int Cur_bomb = Rand(1,wid*len);
- if (is_bomb[Cur_bomb]) continue;
- else{
- is_bomb[Cur_bomb] = true;
- ii pos = Convert_to_2D(Cur_bomb);
- board[pos.fi][pos.se] = '*';
- break;
- }
- }
- }
- //"Number" the board
- for(int i=1; i<=wid; i++){
- for(int j=1; j<=len; j++){
- //init the current gameplay board
- cur_board[i][j] = '.';
- if (board[i][j]!='*'){
- int num = 0;
- for(int k=0; k<8; k++){
- int x = i + dx[k];
- int y = j + dy[k];
- if (ok(x,y) && board[x][y]=='*') num++;
- }
- if (num!=0) board[i][j] = (char)(num+'0');
- }
- }
- }
- }
- //---Move---//
- void Move(ii add){
- int x = cur_x + add.fi;
- int y = cur_y + add.se;
- if (ok(x,y)){
- cur_x = x;
- cur_y = y;
- }
- }
- //---BFS---//
- void bfs(){
- queue<ii> q;
- q.push(ii(cur_x,cur_y));
- cur_board[cur_x][cur_y] = board[cur_x][cur_y];
- while (q.size()){
- int u = q.front().fi;
- int v = q.front().se;
- q.pop();
- if (board[u][v]!='O') continue;
- for(int k=0; k<8; k++){
- int x = u + dx[k];
- int y = v + dy[k];
- if (ok(x,y) && cur_board[x][y]=='.'){
- cur_board[x][y] = board[x][y];
- if (board[x][y]=='O') q.push(ii(x,y));
- }
- }
- }
- }
- //---End game---//
- void End_game(){
- Clear();
- for(int i=1; i<=grid_level[level].fi; i++){
- if (cur_x==i && cur_y==1) cout << "[";
- else cout << " ";
- for(int j=1; j<=grid_level[level].se; j++){
- cout << cur_board[i][j];
- if (cur_x==i && cur_y==j) cout << "]";
- else if (cur_x==i && j==cur_y-1) cout << "[";
- else cout << " ";
- }
- cout << endl;
- }
- cout << endl << "YOU LOSE \n";
- cin.ignore();
- exit(0);
- }
- //---process enter query---//
- void Process_Enter(){
- if (board[cur_x][cur_y]=='O'){
- bfs();
- }
- else if (board[cur_x][cur_y]=='*'){
- cur_board[cur_x][cur_y] = board[cur_x][cur_y];
- End_game();
- }
- else{
- cur_board[cur_x][cur_y] = board[cur_x][cur_y];
- }
- }
- //---process space query---//
- void Process_Space(){
- if (cur_board[cur_x][cur_y]!='.' && cur_board[cur_x][cur_y]!='F') return;
- if (cur_board[cur_x][cur_y] == '.') cur_board[cur_x][cur_y] = 'F', flag++;
- else cur_board[cur_x][cur_y] = '.', flag--;
- }
- //---process Z query---//
- void Process_Z(){
- if (cur_board[cur_x][cur_y]=='P' || cur_board[cur_x][cur_y]=='O') return; //If not number cell then do nothing
- int num_flag = 0;
- queue<ii> q;
- for(int k=0; k<8; k++){
- int x = cur_x + dx[k];
- int y = cur_y + dy[k];
- if (ok(x,y) && cur_board[x][y]=='F') num_flag++;
- if (ok(x,y) && cur_board[x][y]=='.'){
- cur_board[x][y] = board[x][y];
- if (board[x][y]=='*') End_game();
- if (board[x][y]=='O') q.push(ii(x,y));
- }
- }
- if (num_flag != cur_board[cur_x][cur_y]-'0') return; //If not enough flag around then do nothing
- while (q.size()){
- int u = q.front().fi;
- int v = q.front().se;
- q.pop();
- for(int k=0; k<8; k++){
- int x = u + dx[k];
- int y = v + dy[k];
- if (ok(x,y) && cur_board[x][y]=='.' && board[x][y]!='*'){
- cur_board[x][y] = board[x][y];
- if (board[x][y]=='O') q.push(ii(x,y));
- }
- }
- }
- }
- //---Check winning state---//
- bool Check_win(){
- bool ok = true;
- for(int i=1; i<=grid_level[level].fi; i++){
- for(int j=1; j<=grid_level[level].se; j++){
- if (cur_board[i][j]=='F' && board[i][j]=='*') continue;
- if (cur_board[i][j]!=board[i][j]) ok = false;
- }
- }
- return ok;
- }
- //----Main Game Structure----//
- void Play(){
- while(1){
- Display_the_board();
- if (Check_win()){
- Clear();
- system("Color E1");
- cout << "----Congratulations----" << endl;
- cout << " You have win the game " << endl;
- cout << "Number of bombs defused: " << bombs_level[level] << endl;
- cin.ignore();
- exit(0);
- }
- char key_pressed = getch();
- int ascii = key_pressed;
- if (ascii==75 || ascii==80 || ascii==77 || ascii==72){
- Move(DIR[ascii]);
- }
- else if (ascii==13){
- Process_Enter();
- }
- else if (ascii==32){
- Process_Space();
- }
- else if (ascii==122){
- Process_Z();
- }
- Clear();
- if (ascii==27) break;
- }
- }
- int main()
- {
- Welcome();
- Chosing_level();
- Preprocess_the_board();
- Play();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment