#include <iostream>
#include <ctime>
using namespace std;
#define NOT_SHOW 'X'
/*
//==================class cell====================
//Obsolete, too slow
class cell
{
private:
bool _is_mine;
//bool _is_show;
int _neighbors;
public:
cell(void): _is_mine(0), _neighbors(0) {}
void set_isMine(bool in) {_is_mine = in;}
//void isShow(bool in) {_is_show = in;}
void neighborPlus(void) {_neighbors++;}
bool get_isMine(void) {return _is_mine;}
//bool get_isShow(void) {return _is_show;}
int get_neighbors(void) {return _neighbors;}
virtual ~cell(void);
};
*/
//================class mineGame===================
class mineGame
{
private:
int _width;
int _height;
int _mine_num; // mine#
int _remain; // number of ~mine cell, default=(_width*_height)-_mine_num, user wins when it reaches to 0
int _c_x, _c_y; // user current selected x,y
bool **_show_board; // 0: hide(default), 1: show
int **_mine_board; // cell board: -1:mine, 0:empty , 1~8:amount of neighbors
public:
mineGame(void): _width(0), _height(0), _mine_num(0), _remain(0), _c_x(-1), _c_y(-1), _show_board(NULL),_mine_board(NULL) {}
mineGame(int width, int height, int mine_num);
void createGame(void);
void setGame(void);
void setMine(void);
void playGame(void);
void drawGame(void);
int userSelectEvent(void); //return value: continue=0, quit=1
int currentSelectEvent(void); //return value: continue=0, win=1,lose=-1
void gameOverEvent(int in); //input value: win=1,lose=-1
int revealBoard(const int x, const int y);
void drawCell(const int x, const int y);
int checkNeighbors(const int x, const int y);
virtual ~mineGame(void);
};
//mineGame
mineGame::mineGame(int width, int height, int mine_num)
{
//initial parameters
_width = width;
_height = height;
_mine_num = mine_num;
_remain=(_width*_height)-_mine_num;
//initiallize
createGame();
playGame();
}
//**create game**//
void mineGame::createGame()
{
setGame();
setMine();
}
//set game
void mineGame::setGame()
{
//create board & show board
_mine_board = new int*[_width];
_show_board = new bool*[_width];
for (int i=0;i<_height;i++)
{
_mine_board[i] = new int[_height];
_show_board[i] = new bool[_height];
}
//initialize board & show board
for (int j=0; j<_height; j++)
for (int i=0; i<_width; i++)
{
_mine_board[i][j] = 0;
_show_board[i][j] = false; ////**************
}
}
//set mine
void mineGame::setMine()
{
int set_mine = _mine_num;
int count = 1;
int s_x,s_y;
srand ( time(NULL) );
//set mine to cell
cout << "Set mine.\n";
while (set_mine)
{
s_x = rand() % _width;
s_y = rand() % _height;
if (_mine_board[s_x][s_y]==0)
{
//cout << "Set mine #" << count << "\n";
_mine_board[s_x][s_y]=-1;
set_mine--;
count++;
}
}
cout << "Set mine done.\n";
cout << "Check neighbors.\n";
//check # of mines of neighbors on each cell and fill the # of neighbors
for (int j=0; j<_height; j++)
for (int i=0; i<_width; i++)
checkNeighbors(i,j);
cout << "Check neighbors done.\n";
}
//**play game**//
void mineGame::playGame()
{
int game_over=-2;
drawGame();
while(1)
{
//if userSelectEvent()==-1, user quit game
if (userSelectEvent()==-1)
break;
//user play
//if game_over=0, continue play
if ((game_over=currentSelectEvent())==0)
{
drawGame();
continue;
}
//else user win/lose
else
{
gameOverEvent(game_over);
break;
}
}
}
//draw game
/*
Example: width=15,height=15
then it will diplay a board of size (15+3)*(15+3) like below
012346789ABCDE
/--------------/
0|***************|
1|***************|
2|***************|
3|***************|
4|***************|
5|***************|
6|***************|
7|***************|
8|***************|
9|***************|
A|***************|
B|***************|
C|***************|
D|***************|
E|***************|
/--------------/
*/
void mineGame::drawGame()
{
//draw outter edge
for (int j=-2; j<_height+1; j++)
for (int i=-2; i<_width+1; i++)
{
//draw first line
if (j==-2)
if (i==-2 || i==-1 || i==_width)
printf(" ");
else if (i<10)
printf("%d",i);
//print A~Z if i>9
else printf("%c",i+55);
//draw second line
else if (j==-1 || j==_height)
if (i==-2)
printf(" ");
else if (i==-1 || i==_width)
printf("/");
else printf("-");
else
if (i==-2)
if (j<10)
printf("%d",j);
else printf("%c",j+55);
else if (i==-1 || i==_width)
printf("|");
//else printf("*");
else drawCell(i,j);
if (i==_width)
printf("\n");
}
}
void mineGame::drawCell(const int x, const int y)
{
if (_show_board[x][y]==true)
if (_mine_board[x][y]==-1)
printf("*");
else if (_mine_board[x][y]==0)
printf(" ");
else printf("%d",_mine_board[x][y]);
else printf("%c",NOT_SHOW);
}
//check number of neighbors
int mineGame::checkNeighbors(const int x, const int y)
{
if (_mine_board[x][y]==-1)
return 0;
for (int i=x-1; i<x+2; i++)
for (int j=y-1; j<y+2; j++)
{
if (i<0 || j<0 || i>_width-1 || j>_height-1)
continue;
if (_mine_board[i][j]==-1)
_mine_board[x][y]++;
}
return 0;
}
//check cell
int mineGame::userSelectEvent()
{
char usr_x = 255, usr_y = 255;
char usr_p = 255;
_c_x=-1;
_c_y=-1;
bool have_enter_b4=false;
bool repeat_aixs=false;
bool illegal_axis=false;
while (1)
{
//if have enter before
if (have_enter_b4)
{
//if illegal axis
if (illegal_axis)
{
illegal_axis=false;
cout << "\nQuit the game? (y to quit)\n";
cin >> usr_p;
cin.ignore(256,'\n'); //prevent user enter string
if (usr_p=='y' || usr_p=='Y')
return -1;
}
//if legal axis but have visited before
else if (_show_board[_c_x][_c_y])
{
cout << "\nThis axis have been selected before.";
repeat_aixs=true;
}
//if legal and not been visited
else return 0;
}
//if first enter
else have_enter_b4=true;
//user enter axis
cout << "\nPlease enter cell axis:\n";
cin >> usr_y;
cin.ignore(256,' '); //prevent user enter string
cin >> usr_x;
cin.ignore(256,'\n'); //prevent user enter string
/*
cout << "usr_y\"" << usr_y << "\"\n" ;
cout << "usr_x\"" << usr_x << "\"\n" ;
cout << "usr_p\"" << usr_p << "\"\n" ;
*/
if (usr_y==NULL || usr_x==' ')
{
cout << "here\n";
illegal_axis=true;
usr_y=usr_x=0;
continue;
}
//exam the axis
if (10>_width || 10>_height)
{
if ((47<usr_x && 47<usr_y && 58>usr_x && 58>usr_y))
{
_c_x = usr_x-48;
_c_y = usr_y-48;
continue;
}
illegal_axis=true;
repeat_aixs=false;
cout << "Wrong input.";
continue;
}
else
{
if (47<usr_x && 58>usr_x)
if (47<usr_y && 58>usr_y)
{
_c_x = usr_x-48;
_c_y = usr_y-48;
continue;
}
else if (64<usr_y && 64+_height-9>usr_y)
{
_c_x = usr_x-48;
_c_y = usr_y-55;
continue;
}
else if (96<usr_y && 96+_height-9>usr_y)
{
_c_x = usr_x-48;
_c_y = usr_y-87;
continue;
}
if (64<usr_x && 64+_width-9>usr_x)
if (47<usr_y && 58>usr_y)
{
_c_x = usr_x-55;
_c_y = usr_y-48;
continue;
}
else if (64<usr_y && 64+_height-9>usr_y)
{
_c_x = usr_x-55;
_c_y = usr_y-55;
continue;
}
else if (96<usr_y && 96+_height-9>usr_y)
{
_c_x = usr_x-55;
_c_y = usr_y-87;
continue;
}
if (96<usr_x && 96+_width-9>usr_x )
if (47<usr_y && 58>usr_y)
{
_c_x = usr_x-87;
_c_y = usr_y-48;
continue;
}
else if (64<usr_y && 64+_height-9>usr_y)
{
_c_x = usr_x-87;
_c_y = usr_y-55;
continue;
}
else if (96<usr_y && 96+_height-9>usr_y)
{
_c_x = usr_x-87;
_c_y = usr_y-87;
continue;
}
//user wrong input
illegal_axis=true;
repeat_aixs=false;
cout << "Wrong input.";
continue;
}
}
}
int mineGame::currentSelectEvent()
{
//if current x,y is a mine
//return -1 to over the game
if (_mine_board[_c_x][_c_y]==-1)
return -1;
//if current x,y is # of neighbors
//set show=true, _remain-1
if (_mine_board[_c_x][_c_y])
{
_show_board[_c_x][_c_y]=true;
_remain--;
}
//if current x,y is empty
//reveal its neighbors
if (_mine_board[_c_x][_c_y]==0)
{
revealBoard(_c_x, _c_y);
}
return _remain?0:1;
}
//gameOverEvent
void mineGame::gameOverEvent(int in)
{
for (int j=0; j<_height; j++)
for (int i=0; i<_width; i++)
{
_show_board[i][j] = true; ////**************
}
drawGame();
if (!in)
cout << "You win!\n";
else cout << "You lose! Try again.\n";
}
//reveal board
//If current selected cell is empty(ie. there are no mines in neighbors), set neighbors to show if it's empty too.
int mineGame::revealBoard(const int x, const int y)
{
//If already shown or is mine, retrun
if (_show_board[x][y] || _mine_board[x][y]==-1)
return 0;
//Set show if not show, _remain-1
_show_board[x][y]=true;
_remain--;
//if current cell is empty, show its neighbors if they are empty too
if (_mine_board[x][y]==0)
for (int i=x-1; i<x+2; i++)
for (int j=y-1; j<y+2; j++)
{
if (i<0 || j<0 || i>_width-1 || j>_height-1)
continue;
revealBoard(i,j);
}
return 0;
}
//************** mine.cpp **************
#include <iostream>
#include <string>
#include "mine.h"
using namespace std;
/*
cell::~cell(void)
{
}
*/
mineGame::~mineGame(void)
{
}
void main()
{
int width=0, height=0, mine=0;
char level=0;
int c_x=-1,c_y=-1;
char play='y';
while (play=='y' || play=='Y')
{
//user select play and level
cout << "Play a new game? (Enter y to play, other to exit.)\n";
cin >> play;
cin.ignore(256,'\n'); //prevent user enter string
if (play=='y' || play=='Y')
{
cout << "Please enter game level: (1~3)\n";
cin >> level; //prevent user enter string
cin.ignore(256,'\n');
if (level=='1' || level=='2' || level=='3')
{
switch (level)
{
case '1':
width=9, height=9, mine=10;
break;
case '2':
width=12, height=12, mine=18;
break;
case '3':
width=16, height=16, mine=40;
break;
}
}
else
{
cout << "Level not exist!\n";
continue;
}
}
else break;
//start mine game
mineGame mineGame(width, height, mine);
}
cout << "Thank you for playing.\n";
system("pause");
return;
}