Minesweeper Project

Hey guys i have almost done my minesweeper game can anyone help me on this final part for the player input thanks in advance.


#include <iostream>
#include <windows.h>
#define _WIN32_WINNT 0x0500
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <dos.h>
#include <fstream>
#include <sstream>
#include <time.h>
#include <string>
#include <conio.h>
#include <ccomplex>
#include <zmouse.h>
using namespace std;

void resize_windows (int height, int width);
void resize_windows();
void Play_game();
void High_score();
void Help();
struct Score {
int time[4];
char name[8];
};
int main ()
{
resize_windows(600,600); //to resize the console window//
int choice = 0;
while (choice!= 5)
{

cout << " ____________________________________________ " << endl;
cout << " [ Manic Mines ]" << endl;
cout << " [ Press 2 to play game ]" << endl;
cout << " [ Press 3 for highscores ]" << endl;
cout << " [ Press 4 for help ]" << endl;
cout << " [ Press 5 to quit game ]" << endl;
cout << " [ ]" << endl;
cout << " [__________________________________________]" << endl;

{
cin >> choice;
switch (choice) //Switch used to decide which menu option you will choose//
{
case 2:
Play_game();
system ("CLS");
break;

case 3:
High_score();
system ("CLS");
break;

case 4:
Help();
system ("CLS");
break;
}
}
}
system("PAUSE");
return 0;
}

//***********ResizeWindowSubroutine*************************//
void resize_windows (int height, int breadth)
{
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect (console, &r);

MoveWindow(console, r.left, r.top, height, breadth, TRUE);
}

//**********************Play_gameSubroutine******************//
void Play_game()
{
system ("CLS");
int minegrid [9][9] = {0};// grid to check values against
int minecount = 0;
int xnum;
int ynum;
char blankx;
char blanky;
int i;
int j;
int gridx;
int gridy;
char playgrid [9][9];




//Visual grid//

/*for ( i=0;i<9; i++)
{
for ( j=0;j<9; j++)
{
if(playgrid[i][j] >= 9){
cout << " " << "#" << " ";
}else{
cout << " " << playgrid[i][j] << " ";
}
}
cout <<"\n" << endl;

}*/


//Randomseed//
srand(time(NULL));
//IGNORE FOR NOW Used to put mines into the real grid //
do
{
xnum = rand () % 9 + 1;
ynum = rand () % 9 + 1;

if
(minegrid[xnum][ynum]!=9)

{
minegrid[xnum][ynum] = 9;

minecount ++;
}
if (xnum !=8)
{
minegrid[xnum + 1][ynum]++;
}
if ((xnum !=8) && (ynum !=8))
{
minegrid[xnum + 1][ynum + 1]++;
}
if (ynum !=8)
{
minegrid[xnum][ynum + 1]++;
}
if ((xnum !=0) && (ynum !=8))
{
minegrid[xnum - 1][ynum + 1]++;
}
if (xnum !=0)
{
minegrid[xnum - 1][ynum]++;
}
if ((xnum !=0) && (ynum !=0))
{
minegrid[xnum- 1][ynum - 1]++;
}
if (ynum !=0)
{
minegrid[xnum][ynum - 1]++;
}
if ((xnum !=8) && (ynum !=0))
{
minegrid[xnum + 1][ynum - 1]++;
}


} while (minecount < 10);

// Used to loop out the grid//

for ( i=0;i<9; i++)
{
for ( j=0;j<9; j++)
{
if(minegrid[i][j] >= 9){
cout << " " << "#" << " ";
}else{
cout << " " << minegrid[i][j] << " ";
}
}
cout <<"\n" << endl;

}

//*************Comparingcode************/

cout << endl;
cout << endl;
cout << "Input the grid co-ordinates you wish to choose" << endl;
cin >> gridx;
cin >> gridy;

if (minegrid [gridx][gridy] == 9)
{
system ("CLS");
cout << "GameOver"<< endl;
}

else
{
if (minegrid [gridx][gridy] == 0)





system("PAUSE");
}

//************************High_scoreSubroutine****************//

void High_score()
{
system ("CLS");

system("PAUSE");
}

//************************HelpSubroutine************************//

void Help()
{
int goback;
system ("CLS");

cout <<"How to play:" << endl;
cout << endl;
cout <<"Manic Mines is a game about finding mines in the game board." << endl;
cout << endl;
cout <<"Type in the co-ordinates of the square you wish to manipulate." << endl;
cout << endl;
cout <<"If you hit a mine then the game will end." << endl;
cout << endl;
cout <<"There will be 10 mines in the grid to find." << endl;
cout << endl;
cout <<"Be careful you can detonate a mine on your first turn." << endl;
cout << endl;
cout <<"When you select a square and it is not a mine," << endl;
cout << endl;
cout <<"Then the areas around it will be uncovered." << endl;
cout << endl;
cout <<"Your highscore will be based on time completed." << endl;
cout << endl;
cout <<"A score will not be saved if you fail to clear the board." << endl;
cout << endl;
cout <<"Good Luck!!!" << endl;
cout << endl;
cout <<"Happy Manic Mining..." << endl;
cout << endl;
system("PAUSE");
}

#Brandald

You're going to run into problems, as you will be out of bounds at some point, in your arrays.
You created this:
int minegrid [9][9]
Arrays start with 0, so the array is minegrid[0] to minegrid[8]
But try filling, with this:
1
2
3
4
5
6
xnum = rand () % 9 + 1;
ynum = rand () % 9 + 1;

if
(minegrid[xnum][ynum]!=9)
// rest of code 


What if xnum or ynum, IS 9? You'll be out of bounds. Remove the '+1' from the rand generator, so it returns a 0 thru 8, which is what you're needing.
#whitenite1

Thank you i will try puting that in it has been as you have said going out of bounds. My main problem though is i have no idea what so ever on how to do the user input part of the game its annoying coming so close but not being able to complete it. Do you have any idea on the user input side of the game?
@Brandalf

When I wrote a 'Mine Sweeper' game for fun, I used the following...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout << "Total of " << mines << " mines are left. "<< endl; //Show mines left
		do
		{
			gotoXY(1,30);
			cout << "Where in the grid do you wish to check for a mine? :     \b\b\b\b";
			cin >> check; // declared in beginning as a string

			Y = toupper(check[0])-65; // Use first part of input for columns A to X. Grid was 24x24
			X = ((check[1]-48)*10)+(check[2]-49); // Second part of input for rows 1 to 24, 
		}while (check.length()<3 || (X<0 || X>23) || (Y<0 || Y >23));
		check[3]=toupper(check[3]); // Third part, if it's 'F', only put a flag in location, otherwise uncover
		if(check[3] == 'F')
			game_over = Flag_Mine( boardm, check_board, X, Y, mines); // Only flag the area
		if(check[3] != 'F')
			game_over = Mine_Check(boardm, check_board ,X, Y);// uncover area. Game over if mine at 
                           // that location 


Instructions on screen, were...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
gotoXY(55,5); // Function to place text at specified location
	cout << "To open up a space to reveal";
	gotoXY(57,6); 
	cout << "possible mine locations,";
	gotoXY(56,7);
	cout << "use column letter and two";
	gotoXY(61,8);
	cout << "digits for row.";
	gotoXY(59,9);
	cout << "Example : H07 [enter]";
	gotoXY(56,12);
	cout << "To flag a space as a mine,";
	gotoXY(56,13);
	cout << "use the column letter, two";
	gotoXY(58,14);
	cout << "digits for row and the ";
	gotoXY(56,15);
	cout << "letter 'F', for FLAG MINE";
	gotoXY(57,16);
	cout << "Example : H07F [enter]";
	gotoXY(63,19);
	cout << ": WARNING :";
	gotoXY(61,20);
	cout << "Game is over if";
	gotoXY(61,21);
	cout << "no mine present";
	gotoXY(58,22);
	cout << "at selected location.";
	gotoXY(56,25);
	cout << "NOTE : You may use lower,";
	gotoXY(61,26);
	cout << "or upper case, letters";


Hope this gives you ideas on how to get user input.

My only disappointment with my program, I couldn't figure out how to use recursion to open up all the open areas at the chosen location. So I settled on a 3x3 area only. Hopefully, you;ll have better luck.
Last edited on
@ Whitenite1

Thank you, it has given me a good idea on how i can do this, I stupidly thought this would be an easy project haha. I will post up my solution when i get it done so you guys can see if i can improve it any way.

thanks again :)
Looking forward to seeing your solution, Brandalf
Topic archived. No new replies allowed.