📄 chess.cpp
字号:
// Chess.cpp: implementation of the CChess class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Chess.h"
#include "Board.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CChess::CChess()
{
}
CChess::~CChess()
{
}
status CChess::gamestatus()
{
/**************************************检查棋盘当前状况****************************************/
//采取逐点检测法
//在正对角线上检测获胜的状态
for( int r = 0; r < 15; r ++)
{
for(int c = 0; c < 15; c ++)
{
if((r >= 0 && r <= 10) && (c >= 0 && c <= 10))
if(board.stone[r][c] != ' ' && board.stone[r][c] == board.stone[r + 1][c + 1] && board.stone[r][c] == board.stone[r + 2][c + 2]
&& board.stone[r][c] == board.stone[r + 3][c + 3] && board.stone[r][c] == board.stone[r + 4][c + 4])
return WIN;
else
continue;
//else
// continue;
}
}
//在负对角线上检测获胜的状态
for(r = 0; r < 15; r ++)
{
for(int c = 0; c < 15; c ++)
{
if((r >= 0 && r <= 11) && (c >= 4 && c <= 14))
if(board.stone[r][c] != ' ' && board.stone[r][c] == board.stone[r + 1][c - 1] && board.stone[r][c] == board.stone[r + 2][c - 2]
&& board.stone[r][c] == board.stone[r + 3][c - 3] && board.stone[r][c] == board.stone[r + 4][c - 4])
return CONTINUE;
else
continue;
else
continue;
}
}
//在行上检测获胜的状态
for( r = 0; r < 15; r ++)
{
for(int c = 0; c < 15; c ++)
{
if(c <= 10)
if(board.stone[r][c] != ' ' && board.stone[r][c] == board.stone[r][c + 1] && board.stone[r][c] == board.stone[r][c + 2]
&& board.stone[r][c] == board.stone[r][c + 3] && board.stone[r][c] == board.stone[r][c + 4])
return WIN;
else
continue;
else
continue;
}
}
//在列上检测获胜的状态
for( r = 0; r < 15; r ++)
{
for(int c = 0; c < 15; c ++)
{
if(r <= 10)
if(board.stone[r][c] != ' ' && board.stone[r][c] == board.stone[r + 1][c] && board.stone[r][c] == board.stone[r + 2][c]
&& board.stone[r][c] == board.stone[r + 3][c] && board.stone[r][c] == board.stone[r + 4][c])
return WIN;
else
continue;
else
continue;
}
}
//最后检测棋盘上是否还有位置可走
for( r = 0; r < 15; r ++)
{
for(int c = 0; c < 15; c ++)
{
if(board.stone[r][c] == ' ')
return CONTINUE;
else
continue;
}
}
//排除所有情况即为平局状态
return DRAW;
}
int CChess::validmove(int r, int c) // 确保输入的数组位置不越界且为空
{
if(r >= 0 && r < 15 && c >= 0 && c < 15 && board.stone[ r ][ c ] == ' ')
return 1 ;
else
return 0 ;
}
bool CChess::printresult(status sta,player ply)
{ if ( sta == WIN )
{
cout << "玩家" << ply.name << "获胜!" << '\n';
return true;
}
else if ( sta == DRAW )
{
cout << "本次游戏平局.\n";
return true;
}
else
return false;
}
bool CChess::move( player ply )
{
int x, y;
do {
cout << "玩家" << ply.name << " 请走棋: ";
cin >> x >> y;
cout << '\n';
} while ( !validmove( x, y ) );//do{}while()语句检测输入值的有效性,若无效则一直让输入
board.stone[ x ][ y ] = (int)ply.symbol;
board.drawboard();
enum status pstatus = gamestatus(); //自定义的枚举类型数据
return printresult(pstatus,ply);
}
void CChess::twoplayers(void)
{/*************************双人游戏***************************/
board.drawboard();
while ( true ) {
if ( move( board.player1 ) ) //一旦返回true则非胜败已分即平局,故结束while(true)
break;
else if ( move( board.player2 ))
break;
}
}
void CChess::playercomputer(int firsthand)
{/*****************人机对奕函数,firsthand为确定人机顺序******************/
board.drawboard();
if(firsthand == 2)
while( true ) {
if(computermove())
break;
else if(playermove())
break;
}
else
while( true ) {
if( playermove())
break;
else if( computermove())
break;
}
}
/********************计算所有获胜组合(572种),初始化获胜表***********************************/
void CChess::iniwintable()
{
int i,j,k;
int count=0;
//over = false;
//num[0] = num[1] = 0;
for(i=0;i<15;i++)
for(j=0;j<15;j++)
{
temp[i][j]=(int)board.stone[i][j];
}
//设定玩家与计算机在各个获胜组合中的棋子数
for(i=0;i<572;i++)
{
tendence[0][i] = 0;
tendence[1][i] = 0;
}
//设定水平方向的获胜组合
for(i=0;i<15;i++)
for(j=0;j<15;j++)
for(k=0;k<572;k++)
{
computertable[i][j][k] = false;
playertable[i][j][k] = false;
}
for(i=0;i<15;i++)
for(j=0;j<11;j++)
{
for(k=0;k<5;k++)
{
computertable[i][j+k][count] = true;
playertable[i][j+k][count] = true;
}
count++;
}
//垂直方向获胜组合
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -