Skip to content

Commit f8b09a2

Browse files
authored
Add files via upload
1 parent 3592e75 commit f8b09a2

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed

minesweeper_game.cpp

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#include<iostream>
2+
#include<cstdlib>
3+
#include<ctime>
4+
using namespace std;
5+
6+
void print_Board(char board[7][7]);
7+
void boardm(char board[7][7]);
8+
void mines(char board[7][7]);
9+
void get_Input(int *x,int *y,char board1[7][7]);
10+
11+
int main()
12+
{
13+
cout<<"Minesweeper is a deceptively simple test of memory and reasoning and one of the most popular Windows games of all time."<<endl<<endl <<"THE GOAL : find the empty squares and avoid the mines."<<endl<<endl;
14+
cout<<"THE RULES IN MINESWEEPER ARE SIMPLE : "<<endl<<endl<<"1. Uncover a mine, and the game ends."<<endl<<endl<<"2. Uncover a number, and it tells you how many mines lay hidden in the eight surrounding squares."<<endl<<endl<<"3. Use the information gathered from the number to unravel the position of mine";
15+
cout<<endl<<endl<<endl<<"NOTE : you have to enter the position of numbers (NOT MINES)"<<endl<<endl<<endl;
16+
17+
//starting the game
18+
char board1[7][7];//to be seen by the user
19+
char board[7][7];//backend board
20+
boardm(board1);
21+
cout<<"LET START TEH GAME...."<<endl<<" this is your board : "<<endl;
22+
print_Board(board1);
23+
24+
25+
//filling data in backend board
26+
mines(board);
27+
print_Board(board);
28+
29+
//PLAYING THE GAME
30+
31+
for(;;)
32+
{
33+
34+
//TAKING VALID INPUT FROM THE USER AND PRINTING THE CURRENT SITUATION OF THE BOARD
35+
36+
int x,y;
37+
get_Input(&x,&y,board1);
38+
board1[x][y]=board[x][y];
39+
print_Board(board1);
40+
41+
//CHECKING IF THE USER LOST THE GAME
42+
43+
if(board1[x][y]=='*')
44+
{
45+
cout<<"THE MINE BLASTED....."<<endl<<"YOU LOST THE GAME....."<<endl<<"BETTER LUCK NEXT TIME....";
46+
return 0;
47+
}
48+
49+
int count1=0,count2=0;
50+
51+
//CHECKING IF THE USER WON THE GAME....
52+
53+
for(int i=1;i<6;i++)
54+
{
55+
for(int j=1;j<6;j++)
56+
{
57+
if(board1[i][j]=='#')
58+
{
59+
count1++;
60+
}
61+
if(board[i][j]=='*')
62+
{
63+
count2++;
64+
}
65+
}
66+
}
67+
cout<<endl<<count2<<" mines left out of remaining "<<count1<<" positionns"<<endl<<endl;
68+
69+
if(count1==count2)
70+
{
71+
cout<<"CONGRATS....."<<endl<<"YOU FOUND ALL THE MINES...."<<endl<<"YOU WON THE GAME....";
72+
return 0;
73+
}
74+
75+
}
76+
77+
return 0;
78+
}
79+
80+
81+
//printing the current situation of the board
82+
83+
void print_Board(char board[7][7])
84+
{
85+
for(int i=1;i<6;i++)
86+
{
87+
for(int j=1;j<6;j++)
88+
{
89+
cout<<board[i][j]<<" ";
90+
}
91+
cout<<endl;
92+
}
93+
cout<<endl;
94+
}
95+
96+
//initialising board
97+
98+
void boardm(char board[7][7])
99+
{
100+
101+
for(int i=0;i<7;i++)
102+
{
103+
for(int j=0;j<7;j++)
104+
{
105+
board[i][j]='#';
106+
}
107+
}
108+
}
109+
110+
//SETTING UP MINES AND NUMBERS IN THE GAME DATABASE
111+
112+
void mines(char board[7][7])
113+
{
114+
for(int i=1;i<6;i++)
115+
{
116+
for(int j=1;j<6;j++)
117+
{
118+
board[i][j]='0';
119+
}
120+
}
121+
srand(time(0));
122+
for(int k=1;k<7;k++)
123+
{
124+
int x=(rand()%5)+1;
125+
int y=(rand()%5)+1;
126+
if(board[x][y]=='*')
127+
{
128+
continue;
129+
}
130+
else
131+
{
132+
board[x][y]='*';
133+
for(int i=x-1;i<x+2;i++)
134+
{
135+
for(int j=y-1;j<y+2;j++)
136+
{
137+
if(board[i][j]=='*')
138+
continue;
139+
else
140+
{
141+
int temp=(int)board[i][j]+1;
142+
board[i][j]=(char)temp;
143+
}
144+
}
145+
}
146+
}
147+
}
148+
}
149+
150+
//TAKING VALID INPUT FROM THE USER
151+
152+
void get_Input(int *x,int *y,char board1[7][7])
153+
{
154+
cout<<"enter the row position : ";
155+
cin>>*x;
156+
while(*x<1||*x>5)
157+
{
158+
cout<<"INVALID POSITION.... enter row between 1-10 : ";
159+
cin>>*x;
160+
}
161+
cout<<"enter the coloumn position : ";
162+
cin>>*y;
163+
while(*y<1||*y>5)
164+
{
165+
cout<<"INVALID POSITION.... enter coloumn between 1-10 : ";
166+
cin>>*y;
167+
}
168+
while(board1[*x][*y]!='#')
169+
{
170+
cout<<endl<<"THIS POSITION IS ALREADY ENTERED....TRY DIFFERNT ONE......"<<endl;
171+
cout<<"enter the row position : ";
172+
cin>>*x;
173+
while(*x<1||*x>5)
174+
{
175+
cout<<"INVALID POSITION.... enter row between 1-10 : ";
176+
cin>>*x;
177+
}
178+
cout<<"enter the coloumn position : ";
179+
cin>>*y;
180+
while(*y<1||*y>5)
181+
{
182+
cout<<"INVALID POSITION.... enter coloumn between 1-10 : ";
183+
cin>>*y;
184+
}
185+
}
186+
cout<<endl;
187+
}

0 commit comments

Comments
 (0)