Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <ctime>
- #include <random>
- #include <iomanip>
- using namespace std;
- int x,y,r,t;
- int mine;
- int** arraymine;
- char** arrayboard;
- int counter;
- void adjminethingy()
- {
- counter=48;
- if (arraymine[x-1][y-2] == 0 && y>=1)//north
- {
- counter++;
- arrayboard[x-1][y-1]=counter;
- }
- if (arraymine[x-1][y] == 0 && y<r)//south
- {
- counter++;
- arrayboard[x-1][y-1]=counter;
- }
- if (arraymine[x][y-1] == 0 && x<r)//east
- {
- counter++;
- arrayboard[x-1][y-1]=counter;
- }
- if (arraymine[x-2][y-1] == 0 && x>1)//west
- {
- counter++;
- arrayboard[x-1][y-1]=counter;
- }
- if (arraymine[x][y-2] == 0 && x>r && y>1)//north-east
- {
- counter++;
- arrayboard[x-1][y-1]=counter;
- }
- if (arraymine[x][y] == 0 && x<r && y<r)//south-east
- {
- counter++;
- arrayboard[x-1][y-1]=counter;
- }
- if (arraymine[x-2][y] == 0 && x>1 && y<r)//south-west
- {
- counter++;
- arrayboard[x-1][y-1]=counter;
- }
- if (arraymine[x-2][y-2] == 0 && x>1 && y>1)//north-west
- {
- counter++;
- arrayboard[x-1][y-1]=counter;
- }
- }
- void boarddraw()
- {
- for(int i=0;i<r;i++)
- {
- for(int j=0;j<t;j++)
- {
- cout<<"["<<arrayboard[i][j]<<"]";
- }
- cout<<endl;
- }
- }
- void makemove()
- {
- boarddraw();
- cout<<"enter x:";
- cin>>x;
- cout<<"enter y:";
- cin>>y;
- if(arraymine[x-1][y-1]==0)
- {
- cout<<"you have hit a mine";
- }
- else
- cout<<"mined your own business \n";
- adjminethingy();
- cout<<endl<<counter<<endl;
- makemove();
- }
- //This function randomises mines in the board
- void randomise()
- {
- srand(time(0));
- for (int i=0; i<r; i++)
- {
- for (int j=0; j<r; j++)
- {
- int index1=rand()%r;
- int index2=rand()%r;
- int temp=arraymine[i][j];
- arraymine[i][j]=arraymine[index1][index2];
- arraymine[index1][index2]=temp;
- }
- }
- }
- //This function sets up the board
- void mine_board() // Creating mines
- {
- for(int i=0;i<r;i++)
- {
- for(int j=0;j<t;j++)
- {
- if(mine>0)
- {
- arraymine[i][j]=0;
- mine--;
- }
- else
- arraymine[i][j]=1;
- }
- }
- // Randomises mines
- randomise();
- for(int i=0;i<r;i++)
- {
- for(int j=0;j<t;j++)
- {
- arrayboard[i][j]=2;
- }
- }
- for(int i=0;i<r;i++)
- {
- for(int j=0;j<t;j++)
- {
- cout << "[" << arraymine[i][j] << "]";
- }
- cout << endl;
- }
- }
- /*void mine_board()
- {
- for(int i=0;i<r;i++)
- {
- for(int j=0;j<t;j++)
- {
- if(mine>0)
- {
- arraymine[i][j]=0;
- mine--;
- }
- else
- arraymine[i][j]=1;
- }
- }
- //this randomises the mines
- randomise();
- for(int i=0;i<r;i++)
- {
- for(int j=0;j<t;j++)
- {
- cout<<"["<<arrayboard[i][j]<<"]";
- }
- cout<<endl;
- }
- //this draws a physical board made up of ASCII Smiley Faces
- for(int i=0;i<r;i++)
- {
- for(int j=0;j<t;j++)
- {
- arrayboard[i][j]=2;
- }
- }
- for(int i=0;i<r;i++)
- {
- for(int j=0;j<t;j++)
- {
- cout<<arrayboard[i][j];
- }
- cout<<endl;
- }
- }*/
- //void menu draws the board and allows the user to input the game they wish to play
- //based on this it then sets the values of r & t (board size) and mine (number of mines)
- void menu()
- {
- int selection;
- cout<<setw(60)<<"MINESWEEPER\n\n";
- cout << "1. Play Beginner Game\n" << "2. Play Intermediate Game\n" << "3. Play Advanced Game\n" << "4. Show Score Board\n" << "5. Quit Game\n\n\n";
- cout << "Please enter your selection: ";
- cin >> selection;
- cout << endl;
- switch (selection)
- {
- case 1:
- cout << "Play Beginner Game\n";
- cout << "\n";
- system("cls"); //hides above text and leaves only the board
- r=8;
- t=8;
- mine=10;
- break;
- case 2:
- cout << "Play Intermediate Game\n";
- cout << "\n";
- system("cls"); //hides above text and leaves only the board
- r=16;
- t=16;
- mine=40;
- break;
- case 3:
- cout << "Play Advanced Game\n";
- cout << "\n";
- system("cls"); //hides above text and leaves only the board
- r=24;
- t=24;
- mine=99;
- break;
- case 4:
- cout << "Show Score Board\n";
- cout << "\n";
- break;
- case 5:
- cout << "Goodbye.\n";
- break;
- //if not 1-5, this brings up an error and restarts the menu
- default: ;
- system("cls");
- cout<< selection << " is not a valid menu item, please try again.\n";
- menu();
- cout << endl;
- }
- return;
- }
- //based on the input in menu, this generates the board and mines
- void array_board()
- {
- arraymine = new int*[r-1];
- for(int k=0;k<r;k++)
- arraymine[k] = new int[t-1];
- arrayboard = new char*[r-1];
- for(int k=0;k<r;k++)
- arrayboard[k] = new char[t-1];
- }
- int main()
- {
- menu();
- array_board();
- mine_board();
- makemove();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement