网络五子棋游戏QT类的设计与实现 (2)
第一版的五子棋游戏实在是太简陋了!谁愿意玩这样的游戏?连我自己都觉得没劲,赶快给它增加一个友好一点的界面吧!但是这时QT都没有学呀,图形界面不会做,那就搞一个字符的界面:
1. 我给棋盘类增加了一个用于显示的成员函数void dispChessBoard():
class ChessBoard
{
public:
void setChessBoard();
void dispChessBoard(); // 增加的专门用于显示的成员函数
int setChess(int x,int y,int chs);
int judgeVictory(int x,int y);
private:
int board[19][19];
};
void dispChessBoard()的实现如下:
void ChessBoard::dispChessBoard()
{
cout<<" ";
for(int j=0; j<19;j++)
{
printf(" %02d",j); // 首行显示00-18
}
cout<<endl<<endl;
for(int i=0; i<19;i++) // 有19行
{
printf("%02d",i); // 显示行号
for(int j=0; j<19;j++) // 有19列
{
if(j==0)
printf(" %2c",board[i][j]); // 显示棋子, 在board[19][19]中 'W'表示白棋,'B'表示黑棋 , ' '(空格)没有棋子
else
printf("-%2c",board[i][j]); // 显示横线和棋子
}
printf(" %02d",i);
// 显示行号
cout<<endl;
cout<<" ";
for(int j=0; j<19;j++)
{
if(i!=18)
printf(" |"); // 显示竖线
}
cout<<endl;
}
cout<<" ";
for(int j=0; j<19;j++)
{
printf(" %02d",j);
// 最后一行显示00-18
}
cout<<endl<<endl;
}
2. 增加了显示函数,还将它“汉化”了,main.cpp 如下所示:
int main()
{
int ret;
ChessBoard ourboard;
MyChess mychs;
ourboard.setChessBoard();//clear the board
ourboard.dispChessBoard();//display the board
while(1)
{
do{
cout<<"白棋,轮到你下了!"<<endl<<endl;
mychs.readInfo('W');// white chess thinking
ret=mychs.setChess(&ourboard);// put the white chess
ourboard.dispChessBoard();//display the board
}while(ret==1);//something wrong,try again!
if(ret==2)
{
cout<<"恭喜白棋!你赢了。"<<endl<<endl;
break;
}
do{
cout<<"黑棋,轮到你下了!"<<endl<<endl;
mychs.readInfo('B');//black chess thinking
ret = mychs.setChess(&ourboard);// put the black chess
ourboard.dispChessBoard();//display the board
}while(ret==1);//something wrong,try again!
if(ret==3)
{
cout<<"恭喜黑棋!你赢了。"<<endl<<endl;
break;
}
}
cout<<"再见!"<<endl<<endl;
}
3. 在上面的程序中,连C++的构造函数都还没有用到,不好意思我们还没学。 好的,看看我的第二版五子棋游戏吧: