#include "stdafx.h"
#include "CGoBeng.h"
//----------------------------------------------------------------------
//构造/析构函数
CGoBeng::CGoBeng():m_bAiWorking(false),m_bGamePlaying(false),
m_iHumanChessManType(CHESS_BLACK),
m_iRecursionCount(CHESSGAME_AI_RecursionCountMax/2),
m_iHumanWinCount(0),
m_iAiWinCount(0),
m_iHumanWillWinCount(0),
m_iAiWillWinCount(0),
m_iHumanUndoCount(0),
m_bRunEnd(false),
m_iRunResult(0)
{
ZeroMemory(m_chessBoard.iChess ,sizeof(byte)*CHESSBOARDSIZE*CHESSBOARDSIZE);
ZeroMemory(m_undoCache ,sizeof(CHESS)*2);
}
CGoBeng::~CGoBeng()
{
}
//----------------------------------------------------------------------
//共有函数
//开始新游戏
bool CGoBeng::NewGame()
{
m_bGamePlaying = true;
m_iRecursionCount = CHESSGAME_AI_RecursionCountMax/2;
ZeroMemory(m_chessBoard.iChess ,sizeof(byte)*CHESSBOARDSIZE*CHESSBOARDSIZE);
ZeroMemory(m_undoCache ,sizeof(CHESS)*2);
//
m_iHumanWillWinCount = 0;
m_iAiWillWinCount = 0;
m_iHumanUndoCount = 0;
//如果玩家是白子后手则
if( m_iHumanChessManType == CHESS_WHITE )
{
srand(int(timeGetTime()));
char tmp_iPosX = char(rand()%6+6);
char tmp_iPosY = char(rand()%6+6);
m_chessBoard.iChess[tmp_iPosX][tmp_iPosY] = CHESS_BLACK;
UpdateUndoCache(tmp_iPosX,tmp_iPosY,CHESS_BLACK);
}
return true;
}
//人类玩家下子
int CGoBeng::Run(char iPosX , char iPosY )
{
if( !m_bGamePlaying )return CHESSGAME_RUNRESULT_GameNotStarted;
if( iPosX < 0 )return CHESSGAME_RUNRESULT_Define;
if( iPosY < 0 )return CHESSGAME_RUNRESULT_Define;
if( iPosX > 18 )return CHESSGAME_RUNRESULT_Define;
if( iPosY > 18 )return CHESSGAME_RUNRESULT_Define;
int tmp_iResult = CHESSGAME_RUNRESULT_Define;
//检查此处是否已存在棋子
if( !checkEmptyPoint(m_chessBoard,iPosX,iPosY) )
{
return CHESSGAME_RUNRESULT_ExistChess;
}
//先检测是否禁手
if( checkForbiddenHand(m_chessBoard,iPosX,iPosY,m_iHumanChessManType) )
{
return CHESSGAME_RUNRESULT_ForbiddenHand;
}
//人类玩家下子
m_chessBoard.iChess[iPosX][iPosY] = m_iHumanChessManType;
//更新悔棋缓冲
UpdateUndoCache(iPosX,iPosY,m_iHumanChessManType);
//检查胜利
if( checkMatchState(m_chessBoard,iPosX,iPosY,m_iHumanChessManType,4) )
{
m_bGamePlaying = false;
++m_iHumanWinCount;
return (m_iHumanChessManType==CHESS_WHITE)?CHESSGAME_RUNRESULT_WhiteManWin:CHESSGAME_RUNRESULT_BlackManWin;
}
if( checkMatchState(m_chessBoard,iPosX,iPosY,m_iHumanChessManType,3) )
{
++m_iHumanWillWinCount;
tmp_iResult = (m_iHumanChessManType==CHESS_WHITE)?CHESSGAME_RUNRESULT_WhiteManWillWin:CHESSGAME_RUNRESULT_BlackManWillWin;
}
//---------------------------------------
//电脑棋手下棋
byte iAIChessMan = (m_iHumanChessManType==CHESS_WHITE)?CHESS_BLACK:CHESS_WHITE;
//先保存当前时间
DWORD tmp_dwTimeStart = ::timeGetTime();
//电脑棋手思考
CHESS2 tmp_AIResult = artificialIntelligence(m_chessBoard,iAIChessMan,m_iRecursionCount );
if( tmp_AIResult.iPosX < 0 || tmp_AIResult.iPosY < 0 )
{
//如果电脑想不出可下点,则投降
m_bGamePlaying = false;
return (m_iHumanChessManType==CHESS_WHITE)?CHESSGAME_RUNRESULT_BlackManSurrender:CHESSGAME_RUNRESULT_WhiteManSurrender;
}
//电脑棋手下子
m_chessBoard.iChess[tmp_AIResult.iPosX][tmp_AIResult.iPosY] = iAIChessMan;
//更新悔棋缓冲
UpdateUndoCache(tmp_AIResult.iPosX,tmp_AIResult.iPosY,iAIChessMan);
//检查胜利
if( checkMatchState(m_chessBoard,tmp_AIResult.iPosX,tmp_AIResult.iPosY,iAIChessMan,4) )
{
m_bGamePlaying = false;
++m_iAiWinCount;
return (iAIChessMan==CHESS_WHITE)?CHESSGAME_RUNRESULT_WhiteManWin:CHESSGAME_RUNRESULT_BlackManWin;
}
if( checkMatchState(m_chessBoard,tmp_AIResult.iPosX,tmp_AIResult.iPosY,iAIChessMan,3) )
{
++m_iAiWillWinCount;
tmp_iResult = (iAIChessMan==CHESS_WHITE)?CHESSGAME_RUNRESULT_WhiteManWillWin:CHESSGAME_RUNRESULT_BlackManWillWin;
}
//---------------------------------------
//调节电脑棋手的思考步数
tmp_dwTimeStart = ::timeGetTime() - tmp_dwTimeStart;
if( tmp_dwTimeStart > CHESSGAME_AI_TimeDelayMax )
{
--m_iRecursionCount;
if( m_iRecursionCount < CHESSGAME_AI_RecursionCountMin )
m_iRecursionCount = CHESSGAME_AI_RecursionCountMin;
}
else if( tmp_dwTimeStart < CHESSGAME_AI_TimeDelayMin )
{
++m_iRecursionCount;
if( m_iRecursionCount > CHESSGAME_AI_RecursionCountMax )
m_iRecursionCount = CHESSGAME_AI_RecursionCountMax;
}
return tmp_iResult;
}
//悔棋
bool CGoBeng::Ubdo()
{
if( !m_bGamePlaying )return 0;
//检查是否符合悔棋条件
for(int i=1 ;i>-1 ;--i )
{
if( !m_undoCache[i].iChessMan )return false;
if( m_undoCache[i].iPosX < 0 )return false;
if( m_undoCache[i].iPosY < 0 )return false;
if( m_undoCache[i].iPosX > 18 )return false;
if( m_undoCache[i].iPosY > 18 )return false;
}
//返回数据
for(int i=0 ;i<2 ;++i )
{
m_chessBoard.iChess[m_undoCache[i].iPosX][m_undoCache[i].iPosY] = CHESS_NONE;
}
//清空缓冲
ZeroMemory(m_undoCache ,sizeof(CHESS)*2);
++m_iHumanUndoCount;
return false;
}
//----------------------------------------------------------------------
//私有函数
//----------------------------------------------------
//检测盘面数据用函数
//检查此处是否有子
bool CGoBeng::checkEmptyPoint(const CHESSBOARD &chessBoard , char iPosX , char iPosY )
{
return (chessBoard.iChess[iPosX][iPosY] == CHESS_NONE);
}
//检查禁手情况
bool CGoBeng::checkForbiddenHand(const CHESSBOARD &chessBoard , char iPosX , char iPosY , byte iChessMan)
{
//如果是白子,返回
if( iChessMan==CHESS_WHITE)
return false;
//开始检查
//三三禁手
if( (iPosX-2) >= 0 && (iPosY+3) <= 18 )
if( chessBoard.iChess[iPosX-1][iPosY] == iChessMan && chessBoard.iChess[iPosX-2][iPosY] == iChessMan && chessBoard.iChess[iPosX][iPosY+2] == iChessMan && chessBoard.iChess[iPosX][iPosY+3] == iChessMan )
return true;
if( (iPosX-2) >= 0 && (iPosY-3) >= 0 )
if( chessBoard.iChess[iPosX-1][iPosY] == iChessMan && chessBoard.iChess[iPosX-2][iPosY] == iChessMan && chessBoard.iChess[iPosX][iPosY-2] == iChessMan && chessBoard.iChess[iPosX][iPosY-3] == iChessMan )
return true;
if( (iPosX+2) <= 18 && (iPosY+3) <= 18 )
if( chessBoard.iChess[iPosX+1][iPosY] == iChessMan && chessBoard.iChess[iPosX+2][iPosY] == iChessMan && chessBoard.iChess[iPosX][iPosY+2] == iChessMan && chessBoard.iChess[iPosX][iPosY+3] == iChessMan )
return true;
if( (iPosX+2) <= 18 && (iPosY-3) >= 0 )
if( chessBoard.iChess[iPosX+1][iPosY] == iChessMan && chessBoard.iChess[iPosX+2][iPosY] == iChessMan && chessBoard.iChess[iPosX][iPosY-2] == iChessMan && chessBoard.iChess[iPosX][iPosY-3] == iChessMan )
return true;
if( (iPosX-3) >= 0 && (iPosY+2) <= 18 )
if( chessBoard.iChess[iPosX-2][iPosY] == iChessMan && chessBoard.iChess[iPosX-3][iPosY] == iChessMan && chessBoard.iChess[iPosX][iPosY+1] == iChessMan && chessBoard.iChess[iPosX][iPosY+2] == iChessMan )
return true;
if( (iPosX-3) >= 0 && (iPosY-2) >= 0 )
if( chessBoard.iChess[iPosX-2][iPosY] == iChessMan && chessBoard.iChess[iPosX-3][iPosY] == iChessMan && chessBoard.iChess[iPosX][iPosY-1] == iChessMan && chessBoard.iChess[iPosX][iPosY-2] == iChessMan )
return true;
if( (iPosX+3) <= 18 && (iPosY+2) <= 18 )
if( chessBoard.iChess[iPosX+1][iPosY] == iChessMan && chessBoard.iChess[iPosX+3][iPosY] == iChessMan && chessBoard.iChess[iPosX][iPosY+1] == iChessMan && chessBoard.iChess[iPosX][iPosY+2] == iChessMan )
return true;
if( (iPosX+3) <= 18 && (iPosY-2) >= 0)
if( chessBoard.iChess[iPosX+1][iPosY] == iChessMan && chessBoard.iChess[iPosX+3][iPosY] == iChessMan && chessBoard.iChess[iPosX][iPosY-1] == iChessMan && chessBoard.iChess[iPosX][iPosY-2] == iChessMan )
return true;
//三三禁手
//三三禁手2
if( (iPosX-1)>=0 && (iPosX+1)<=18 && (iPosY+1)<=18 && (iPosY-1)>=0)
if( chessBoard.iChess[iPosX-1][iPosY-1] == iChessMan && chessBoard.iChess[iPosX+1][iPosY+1] == iChessMan && chessBoard.iChess[iPosX-1][iPosY+1] == iCh