ubuntu14.04
g++版本号: 4:4.8.2-1ubuntu6
1.[文件] ChessBoard.h ~ 248B 下载(89)
#ifndef _CHESSBOARD_H_
#define _CHESSBOARD_H_
#include
using namespace std;
class ChessBoard//棋盘类
{
public:
enum{ROW=31,COL=31};
char cSquare[ROW][COL];
public:
ChessBoard();
void show();
~ChessBoard(){}
};
#endif
2.[文件] ChessBoard.cpp ~ 450B 下载(65)
#include"ChessBoard.h"
#include
ChessBoard::ChessBoard()
{
for(int i=1;i
for(int j=1;j
cSquare[i][j]=' ';
for(int j=0;j
cSquare[0][j]=cSquare[ROW-1][j]='-';
for(int i=1;i
cSquare[i][0]=cSquare[i][COL-1]='|';
}
void ChessBoard::show()
{
system("clear");//DOS下调用system("cls")
for(int i=0;i
{
for(int j=0;j
cout<
cout<
}
}
3.[文件] Player.h ~ 648B 下载(63)
#ifndef _PLAYER_H_
#define _PLAYER_H_
#include"ChessBoard.h"
#include
class Player
{
string m_name;
char m_ChessType;
int m_x;
int m_y;
ChessBoard* m_ptBoard;
public:
enum{ROW=31,COL=31};
Player():m_name("no name"),m_ChessType('?'),m_x(0),m_y(0),m_ptBoard(NULL){}
void attachToBoard(ChessBoard* ptBoard){m_ptBoard=ptBoard;}
bool isInChessBoard(int x,int y)const;
bool HisLine(int x,int y)const;
bool VisLine(int x,int y)const;
bool LtoBottomisLine(int x,int y)const;
bool LtoTopisLine(int x,int y)const;
bool isWin()const;
string WinName()const { return m_name;}
void SetInfo();
void SetChess();
~Player(){}
};
#endif
4.[文件] Player.cpp ~ 3KB 下载(62)
#include"Player.h"
#include
bool Player::isInChessBoard(int x,int y)const
{
if(x0 && y0)
return true;
else
return false;
}
void Player::SetInfo()
{
cout<
getline(cin,m_name);
if("q" == m_name) { cout<
while(!cin)
{
cin.clear();//清楚fail状态
cin.ignore(1024,'\n');//清除缓冲区
cout<
getline(cin,m_name);
if("q" == m_name) { cout<
}
cout<m_name<
cin>>m_ChessType;
cin.get();//read Enter key
if('q' == m_ChessType) { cout<
while(!cin||(m_ChessType!='*'&&m_ChessType!='#'))
{
cin.clear();
cin.sync();
//cin.ignore(1024,'\n');//因Enter key已被读取所以此处不宜用ignore
cout<m_name<
cin>>m_ChessType;
cin.get();//read Enter key.ignore将Enter key清除了而cin.sync()不清Enter key。
if('q' == m_ChessType) { cout<
}
}
void Player::SetChess()
{
cout<m_name<
cin>>m_x>>m_y;//x是行标,y是列标
if(-1 == m_x &&-1 == m_y) {cout<
while(!cin||m_ptBoard->cSquare[m_x][m_y]!=' ')//输入此位置已有棋子
{
cin.clear();
cin.ignore(1024,'\n');
cout<m_name<
cin>>m_x>>m_y;
if(-1 == m_x && -1 == m_y) {cout<
}
if(isInChessBoard(m_x,m_y))
m_ptBoard->cSquare[m_x][m_y]=m_ChessType;
}
bool Player::HisLine(int x,int y)const
{
int num=0;
for(int i=-4;i<=4;i++)
{
if(isInChessBoard(x,y+i)&&m_ptBoard->cSquare[x][y+i]==m_ChessType)
{
num++;
if(5 == num)
return true;
}
else
num=0;
}
return false;
}
bool Player::VisLine(int x,int y)const
{
int num=0;
for(int i=-4;i<=4;i++)
{
if(isInChessBoard(x+i,y)&&m_ptBoard->cSquare[x+i][y]==m_ChessType)
{
num++;
if(5 == num)
return true;
}
else
num=0;
}
return false;
}
bool Player::LtoBottomisLine(int x,int y)const
{
int num=0;
for(int i=-4;i<=4;i++)
{
if(isInChessBoard(x+i,y+i)&&m_ptBoard->cSquare[x+i][y+i]==m_ChessType)
{
num++;
if(5 == num)
return true;
}
else
num=0;
}
return false;
}
bool Player::LtoTopisLine(int x,int y)const
{
int num=0;
for(int i=-4;i<=4;i++)
{
if(isInChessBoard(x-i,y+i)&&m_ptBoard->cSquare[x-i][y+i]==m_ChessType)
{
num++;
if(5 == num)
return true;
}
else
num=0;
}
return false;
}
bool Player::isWin()const
{
return (HisLine(m_x,m_y)||VisLine(m_x,m_y)||LtoBottomisLine(m_x,m_y)||LtoTopisLine(m_x,m_y))?true:false;
}
5.[文件] FiveChessMain.cpp ~ 645B 下载(63)
#include"Player.h"
int main()
{
ChessBoard board;
Player playerA;
playerA.SetInfo();
playerA.attachToBoard(&board);
Player playerB;
playerB.SetInfo();
playerB.attachToBoard(&board);
board.show();
while(1)
{
playerA.SetChess();//玩家A放下一个棋子
if(playerA.isWin())
{ cout<
board.show();
playerB.SetChess();//玩家B放下一个棋子
if(playerB.isWin())
{ cout<
board.show();
}
return 1;//运行出错
}