Minsweeper text game problem
May 1, 2010 at 12:06pm UTC
today i started programming a small text version of minesweeper, and even though i'm going to embarass myself as being a noob..again.. but whatever, the program seems to crash whenever i call the hField::Open(int,int) function so here is the code:
this is the header file called mine.h:
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
class Field
{
public :
Field();
~Field(){};
protected :
char field[8][8];
};
class hField : public Field
{
public :
hField();
~hField(){};
void Open(int ,int );
void drawField();
bool ifDeath();
private :
char cField[8][8];
bool fDeath;
};
Field::Field()
{
int x,y;
for (int i = 0;i < 8;i++)
{
x = rand() % 8;
srand(time(NULL));
y = rand() % 8;
if (field[x][y] == 'M' ){
i--;
continue ;
}
else field[x][y] = 'M' ;
cout << x + 1 << " " << y + 1 << endl;
}
for (int i = 0;i < 8;i++)
{
for (int j = 0;j < 8;j++)
{
if (field[i][j] == 'M' ) field[i][j] = 'M' ;
else field[i][j] = '.' ;
}
}
}
hField::hField()
{
for (int i = 0;i < 8;i++)
{
for (int j = 0;j < 8;j++) cField[i][j] = '#' ;
}
fDeath = false ;
}
void hField::Open(int x,int y)
{
cField[x][y] = field[x][y];
if (cField[x][y] == '.' ){
Open(x-1,y-1);
Open(x,y-1);
Open(x+1,y-1);
Open(x-1,y);
Open(x+1,y);
Open(x-1,y+1);
Open(x,y+1);
Open(x+1,y+1);
}
if (cField[x][y] == 'M' ) fDeath = true ;
}
void hField::drawField()
{
for (int i = 0;i < 8;i++)
{
cout << endl << i + 1 << " " ;
for (int j = 0;j < 8;j++) cout << cField[i][j] << " " ;
}
cout << "\n 1 2 3 4 5 6 7 8\n" ;
}
bool hField::ifDeath()
{
if (fDeath == true ) return true ;
else return false ;
}
and here is the main program:
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 33 34
#include <iostream>
#include <time.h>
using namespace std;
#include "mine.h"
int main()
{
cout << "Initalizing game, this may take couple seconds\n" ;
char f;
int x,y;
hField game;
while (1)
{
game.drawField();
cin >> f;
if (f == 'o' || f == 'O' ){
cout << "Enter x:" ;
cin >> x;
cout << "Enter y:" ;
cin >> y;
game.Open(x,y);
}
if (game.ifDeath()){
cout << "You died :(\n" ;
cout << "Do you want to play again?(y/n)" ;
cin >> f;
if (f == 'n' || f == 'N' ) break ;
else hField game;
}
}
cin.get();
return 0;
}
and please just don't make fun of me b/c i know i am a noob :D:D:D::D:D:D:D
May 1, 2010 at 12:40pm UTC
ok i think i found how to solve it i'm going to write it here if i have any more problems
edit. still no solution
Last edited on May 1, 2010 at 12:46pm UTC
Topic archived. No new replies allowed.