Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <conio.h>
- #include <windows.h>
- #include <ctime>
- #include <iomanip>
- void game();
- void settings();
- void flagging();
- void click();
- void settings();
- void board();
- bool ifend();
- void counter(int lin, int col);
- struct square
- {
- char view=char(254u);
- bool flags=0;
- bool bombs=0;
- bool if_checked=0;
- };
- square tab[9][9];
- int flagsleft=10;
- int main()
- {
- std::cout << "====================" << std::endl;
- std::cout << "Minesweeper v.2.0" << std::endl;
- std::cout << "by Bartek Mekarski" << std::endl;
- std::cout << "====================" << std::endl;
- std::cout << "\n";
- std::cout << "1. New game" << std::endl;
- std::cout << "2. Exit" << std::endl;
- std::cout << "Press the key to select an option" << std::endl;
- char pick;
- do
- {
- pick=getch();
- switch (pick)
- {
- case '1': game();
- break;
- case '2': return 0;
- break;
- default:
- {
- system("cls");
- std::cout << "Press the correct key!" << std::endl;
- std::cout << "1. New game" << std::endl;
- std::cout << "2. Exit" << std::endl;
- }
- }
- }while (pick!='1'&&pick!='2');
- return 0;
- }
- void game()
- {
- system ("cls");
- srand(time(NULL));
- int i=0;
- while (i<10)
- {
- int wherebombx=rand()%9, wherebomby=rand()%9;
- if (tab[wherebombx][wherebomby].bombs==0)
- {
- tab[wherebombx][wherebomby].bombs=1;
- i++;
- }
- }
- clock_t start = clock();
- while (ifend()==0)
- {
- board();
- std::cout << "\nTo put/remove a flag, press F!" << std::endl;
- std::cout << "To 'click' on a square, press J!" << std::endl;
- std::cout << "\n\nTime: " << (clock()-start)/CLOCKS_PER_SEC << " s" << std::endl;
- std::cout << "Flags left: " << flagsleft << std::endl;
- char ans;
- ans = getch();
- switch(ans)
- {
- case 70: flagging();
- break;
- case 102: flagging();
- break;
- case 74: click();
- break;
- case 106: click();
- break;
- default: std::cout << "Press the correct key!" << std::endl;
- break;
- }
- }
- board();
- std::cout << "\nYOU WON!!!\n\n";
- std::cout << "Your time was: " << (clock()-start)/CLOCKS_PER_SEC << " s" << std::endl;
- std::cout << "Press any key to continue!" << std::endl;
- getch();
- }
- void board()
- {
- system("cls");
- std::cout << "1 2 3 4 5 6 7 8 9 " << std::endl;
- std::cout << "------------------" << std::endl;
- for (int i=0; i<9; i++)
- {
- for (int j=0; j<9; j++)
- std::cout << tab[i][j].view << " ";
- std::cout << "|" << i+1 << std::endl;
- }
- }
- bool ifend()
- {
- int c=0;
- for (int i=0; i<9; i++)
- for (int j=0; j<9; j++)
- {
- if (tab[i][j].view == char(254u) || tab[i][j].view == 'F')
- c++;
- }
- int d=0;
- if (c==10)
- {
- for (int i=0; i<9; i++)
- for (int j=0; j<9; j++)
- {
- if (tab[i][j].bombs == 1 && (tab[i][j].view == char(254u) || tab[i][j].view == 'F'))
- d++;
- }
- }
- if (d==10)
- return 1;
- else
- return 0;
- }
- void flagging()
- {
- int lin,col;
- std::cout << "Enter coordinates (line and column) of the flag!" << std::endl;
- std::cin >> lin >> col;
- lin--;
- col--;
- if (lin>=0 && lin<=8 && col>=0 && col<=8 && (tab[lin][col].view==char(254u) || tab[lin][col].view=='F'))
- {
- if (tab[lin][col].flags==0)
- {
- tab[lin][col].flags=1;
- tab[lin][col].view='F';
- flagsleft--;
- }
- else
- {
- tab[lin][col].flags=0;
- tab[lin][col].view=char(254u);
- flagsleft++;
- }
- }
- else
- std::cout << "Incorrect coordinates!" << std::endl;
- }
- void click()
- {
- int lin,col;
- std::cout << "Enter coordinates (line and column) of the square!" << std::endl;
- std::cin >> lin >> col;
- lin--;
- col--;
- std::cout << "\n";
- if (lin>=0 && lin<=8 && col>=0 && col<=8)
- {
- if (tab[lin][col].flags==1)
- std::cout << "There is a flag on this square!" << std::endl;
- else if (tab[lin][col].bombs==1)
- {
- system("cls");
- tab[lin][col].view='X';
- board();
- //prints board with marked bombs
- std::cout << "\nYOU LOST!!!\n\n";
- std::cout << "1 2 3 4 5 6 7 8 9 " << std::endl;
- std::cout << "------------------" << std::endl;
- for (int i=0; i<9; i++)
- {
- for (int j=0; j<9; j++)
- {
- if (tab[i][j].bombs==1)
- std::cout << "B ";
- else
- std::cout << char(254u) <<" ";
- }
- std::cout << "| " << i+1 << std::endl;
- }
- std::cout << "Press any key to continue!" << std::endl;
- getch();
- exit(0);
- }
- else
- counter(lin,col);
- }
- else
- {
- std::cout << "Incorrect coordinates!" << std::endl;
- }
- }
- void counter (int lin, int col)
- {
- if (lin>=0 && lin<=8 && col>=0 && col<=8)
- {
- tab[lin][col].if_checked = 1;
- char c=48;
- if (tab[lin-1][col-1].bombs==1 && lin-1>=0 && col-1>=0)
- c++;
- if (tab[lin-1][col].bombs==1 && lin-1>=0)
- c++;
- if (tab[lin-1][col+1].bombs==1 && lin-1>=0 && col+1<=8)
- c++;
- if (tab[lin][col+1].bombs==1 && col+1<=8)
- c++;
- if (tab[lin+1][col+1].bombs==1 && lin+1<=8 && col+1<=8)
- c++;
- if (tab[lin+1][col].bombs==1 && lin+1<=8)
- c++;
- if (tab[lin+1][col-1].bombs==1 && lin+1<=8 && col-1>=0)
- c++;
- if (tab[lin][col-1].bombs==1 && col-1>=0)
- c++;
- if (c==48)
- {
- tab[lin][col].view=32;//space
- if (tab[lin-1][col-1].flags==0 && lin-1>=0 && col-1>=0 && tab[lin-1][col-1].if_checked==0)
- counter(lin-1,col-1);
- if (tab[lin-1][col].flags==0 && lin-1>=0 && tab[lin-1][col].if_checked==0)
- counter(lin-1,col);
- if (tab[lin-1][col+1].flags==0 && lin-1>=0 && col+1<=8 && tab[lin-1][col+1].if_checked==0)
- counter(lin-1,col+1);
- if (tab[lin][col+1].flags==0 && col+1<=8 && tab[lin][col+1].if_checked==0)
- counter(lin,col+1);
- if (tab[lin+1][col+1].flags==0 && lin+1<=8 && col+1<=8 && tab[lin+1][col+1].if_checked==0)
- counter(lin+1,col+1);
- if (tab[lin+1][col].flags==0 && lin+1<=8 && tab[lin+1][col].if_checked==0)
- counter(lin+1,col);
- if (tab[lin+1][col-1].flags==0 && lin+1<=8 && col-1>=0 && tab[lin+1][col-1].if_checked==0)
- counter(lin+1,col-1);
- if (tab[lin][col-1].flags==0 && col-1>=0 && tab[lin][col-1].if_checked==0)
- counter(lin,col-1);
- }
- else
- tab[lin][col].view = c;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement