Hi,
I have to write game Minesweeper in OOP way. I have classes:
Square which inherits to mineSquare, emptySquare, digitSquare and class Board which is responsible for initialize and control the game.
I have such code:
#include <iostream>
#include <typeinfo>
#include "time.h"
using namespace std;
class Square{
public:
Square(){
this->isShown = false;
this->isFlagged = false;
}
bool getIsShown(){
return this->isShown;
}
bool getIsFlagged(){
return this->isFlagged;
}
void makeItShown(){
this->isShown = true;
}
void makeItFlagged(){
this->isFlagged = true;
}
void makeItUnflagged(){
this->isFlagged = false;
}
private:
bool isShown;
bool isFlagged;
};
class digitSquare : Square{
public:
digitSquare(){
value = 0;
}
int getValue(){
return value;
}
void setValue(int value){
this->value = value;
}
private:
int value;
};
class emptySquare : Square{
};
class mineSquare : public Square{
};
mineSquare type_mineSquare;
emptySquare type_emptySquare;
digitSquare type_digitSquare;
class Board{
public:
Board(int xMax=10, int yMax=10, int amountOfMines=10){
gameOver = false;
this->xMax = xMax;
this->yMax = yMax;
this->amountOfMines = amountOfMines;
Square*** tbl = new Square**[xMax];
for(int i=0; i<xMax; i++){
tbl[i] = new Square*[yMax];
}
}
void init();
void placeMines();
bool isGameOver(){
if(gameOver == true){
unhideTable();
}
else{
}
return gameOver;
}
private:
void unhideNeighboringEmptySquares(int x, int y){
}
void unhideTable(){
}
Square ***tbl;
int xMax;
int yMax;
int amountOfMines;
bool isMine(int x, int y);
int countNeighboringMines(int x, int y);
void showSquare(int x, int y);
void flagSquare(int x, int y);
bool gameOver;
};
void Board::init(){
for(int i=0; i<xMax; i++){
for(int j=0; j<yMax; j++){
tbl[i][j]->makeItFlagged();
}
}
}
void Board::placeMines(){
int amountOfMines = this->amountOfMines;
for(amountOfMines; amountOfMines>0; amountOfMines--){
srand((unsigned int)time(NULL));
int xRandom = rand()%xMax;
int yRandom = rand()%yMax;
if(!isMine(xRandom,yRandom)){
delete tbl[xRandom][yRandom];
tbl[xRandom][yRandom] = new mineSquare();
}
else amountOfMines++;
}
}
bool Board::isMine(int x, int y){
if(typeid(*tbl[x][y]) == typeid(type_mineSquare)) return true;
else false;
}
int Board::countNeighboringMines(int x, int y){
int amountOfNeighboringMines = 0;
if(x > 0){
if(isMine(x-1, y)) amountOfNeighboringMines++;
if(y > 0 && isMine(x-1, y-1)) amountOfNeighboringMines++;
if((y < this->yMax-1) && isMine(x-1, y+1)) amountOfNeighboringMines++;
}
if(x < this->xMax-1){
if(isMine(x+1, y)) amountOfNeighboringMines++;
if(y > 0 && isMine(x+1, y-1)) amountOfNeighboringMines++;
if((y < this->yMax-1) && isMine(x+1, y+1)) amountOfNeighboringMines++;
}
if(y > 0 && isMine(x, y-1)) amountOfNeighboringMines++;
if((y < this->yMax-1) && isMine(x, y+1)) amountOfNeighboringMines++;
return amountOfNeighboringMines;
}
void Board::showSquare(int x, int y){
if(tbl[x][y]->getIsShown() == false) tbl[x][y]->makeItShown();
if(isMine(x,y)) this->gameOver = true;
}
void Board::flagSquare(int x, int y){
if(tbl[x][y]->getIsShown() == false){
if(tbl[x][y]->getIsFlagged() == false){
tbl[x][y]->makeItFlagged();
}
else{
tbl[x][y]->makeItUnflagged();
}
}
}
int main(){
Board* Game = new Board();
Game->init(); // error
Game->placeMines(); // i suppose i would get error here also
system("PAUSE");
return 0;
}
It's compiling without any problems. I get error (http://img218.imageshack.us/gal.php?g=pppml.jpg) when program try run method init().
Can anyone help me?
I use VC++ 2008 ;)