www.pudn.com > wuziqi.rar > 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++;
}
//垂直方向获胜组合
for(i=0;i<15;i++)
for(j=0;j<11;j++)
{
for(k=0;k<5;k++)
{
computertable[j+k][i][count] = true;
playertable[j+k][i][count] = true;
}
count++;
}
//正对角线方向的获胜组合
for(i=0;i<11;i++)
for(j=0;j<11;j++)
{
for(k=0;k<5;k++)
{
computertable[j+k][i+k][count] = true;
playertable[j+k][i+k][count] = true;
}
count++;
}
//反对角线方向的获胜组合
for(i=0;i<11;i++)
for(j=14;j>=4;j--)
{
for(k=0;k<5;k++)
{
computertable[j-k][i+k][count] = true;
playertable[j-k][i+k][count] = true;
}
count++;
}
}
//****计算机下子函数***********************************
// 1.计算机获胜分数
// 2.选择最佳位置下子
bool CChess::computermove()
{
int grades[2][15][15];
int m,n,i,max=0;
int u,v;
for(m=0;m<15;m++)
for(n=0;n<15;n++)
{
grades[0][m][n] = 0;
grades[1][m][n] = 0;
if(board.stone[m][n] == ' ')
{
for(i=0;i<572;i++)
{
//计算玩家在空格上的获胜分数
if(playertable[m][n][i] && tendence[0][i] != 7)
{
switch(tendence[0][i])
{
case 0:
grades[0][m][n]+=1;
break;
case 1:
grades[0][m][n]+=200;
break;
case 2:
grades[0][m][n]+=400;
break;
case 3:
grades[0][m][n]+=2000;
break;
case 4:
grades[0][m][n]+=10000;
break;
}
}
//计算计算机在空格上的获胜分数
if(computertable[m][n][i] && tendence[1][i] != 7)
{
switch(tendence[1][i])
{
case 0:
grades[1][m][n]+=1;
break;
case 1:
grades[1][m][n]+=220;
break;
case 2:
grades[1][m][n]+=420;
break;
case 3:
grades[1][m][n]+=2100;
break;
case 4:
grades[1][m][n]+=20000;
break;
}
}
}
if(max == 0)
{
u = m;
v = n;
}
if(grades[0][m][n] > max)
{
max = grades[0][m][n];
u = m;
v = n;
}
else if(grades[0][m][n] == max)
{
if(grades[1][m][n] > grades[1][u][v])
{
u = m;
v = n;
}
}
if(grades[1][m][n] > max)
{
max = grades[1][m][n];
u = m;
v = n;
}
else if(grades[1][m][n] == max)
{
if(grades[0][m][n] > grades[0][u][v])
{
u = m;
v = n;
}
}
}
}
board.stone[u][v] = 'X'; //设定为计算机的棋子
temp[u][v]=board.stone[u][v];
for(i=0;i<572;i++)
{
if(computertable[u][v][i])
{
tendence[1][i]++;
playertable[u][v][i] = false;
tendence[0][i] = 7;
}
}
board.drawboard();
cout << "计算机走棋: " << u << ", " << v << '\n';
/* for(i=0;i<572;i++)
{ if(i%9==0)
cout<<'/n';
cout<> x >> y;
cout << '\n';
} while ( !validmove( x, y ) );//do{}while()语句检测输入值的有效性,若无效则一直让输入
temp[x][y]=board.stone[x][y];
board.stone[ x ][ y ] = 'O';
for(i=0;i<572;i++)
{
if(playertable[x][y][i])
{
tendence[0][i]++;
computertable[x][y][i] = false;
tendence[1][i] = 7;
}
}
board.drawboard();
cout << "想悔棋吗?Y/N \n";
do
{
cout << "请输入你的选择!\n ";
cin >> tem;
cout << '\n';
//getchar();
}while(tem!='Y'&&tem!='y'&&tem!='N'&&tem!='n');
if((tem=='Y')||(tem=='y'))
{
for(i=0;i<15;i++)
for(j=0;j<15;j++)
{
board.stone[i][j]=temp[i][j];
}
board.drawboard();
playermove();
}
enum status pstatus = gamestatus(); //自定义的枚举类型数据
if ( pstatus == WIN )
{
cout << "玩家获胜!" << '\n';
return true;
}
else if ( pstatus == DRAW )
{
cout << "本次游戏平局.\n";
return true;
}
else
return false;
}