📄 movegenerator.cpp
字号:
// COPYRIGHT NOTES
// ---------------
// This source code is a part of chess which is an example of <Game Programing guide>.
// You may use, compile or redistribute it as part of your application
// for free.
// You cannot redistribute sources without the official agreement of the author.
// If distribution of you application which contents code below was occured, place
// e-mail <hidebug@hotmail.com> on it is to be appreciated.
// This code can be used WITHOUT ANY WARRANTIES on your own risk.
//
// Spring Wang <hidebug@hotmail.com>
// ---------------
// 版权声明
// ---------------
// 本文件所含之代码是《人机博弈程序设计指南》的范例程序中国象棋的一部分
// 您可以免费的使用, 编译 或者作为您应用程序的一部分。
// 但,您不能在未经作者书面许可的情况下分发此源代码。
// 如果您的应用程序使用了这些代码,在您的应用程序界面上
// 放入 e-mail <hidebug@hotmail.com> 是令人欣赏的做法。
// 此代码并不含有任何保证,使用者当自承风险。
//
// 王小春 <hidebug@hotmail.com>
// MoveGenerator.cpp: implementation of the CMoveGenerator class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "chess.h"
#include "MoveGenerator.h"
#ifdef _DEBUG
#undef THIS_FILE
static BYTE THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMoveGenerator::CMoveGenerator()
{
}
CMoveGenerator::~CMoveGenerator()
{
}
int CMoveGenerator::AddMove(int nFromX, int nFromY, int nToX, int nToY,int nPly)
{
m_MoveList[nPly][m_nMoveCount].From.x = nFromX;
m_MoveList[nPly][m_nMoveCount].From.y = nFromY;
m_MoveList[nPly][m_nMoveCount].To.x = nToX;
m_MoveList[nPly][m_nMoveCount].To.y = nToY;
m_nMoveCount++;
return m_nMoveCount;
}
int CMoveGenerator::CreatePossibleMove(BYTE position[10][9], int nPly, int nSide)
{
int nChessID;
int i,j;
m_nMoveCount = 0;
for (i = 0; i < 10; i++)
for (j = 0; j < 9; j++)
{
if (position[i][j] != NOCHESS)
{
nChessID = position[i][j];
if (!nSide && IsRed(nChessID))
continue;
if (nSide && IsBlack(nChessID))
continue;
switch(nChessID)
{
case R_KING:
case B_KING:
Gen_KingMove(position, i, j, nPly);
break;
case R_BISHOP:
Gen_RBishopMove(position, i, j, nPly);
break;
case B_BISHOP:
Gen_BBishopMove(position, i, j, nPly);
break;
case R_ELEPHANT:
case B_ELEPHANT:
Gen_ElephantMove(position, i, j, nPly);
break;
case R_HORSE:
case B_HORSE:
Gen_HorseMove(position, i, j, nPly);
break;
case R_CAR:
case B_CAR:
Gen_CarMove(position, i, j, nPly);
break;
case R_PAWN:
Gen_RPawnMove(position, i, j, nPly);
break;
case B_PAWN:
Gen_BPawnMove(position, i, j, nPly);
break;
case B_CANON:
case R_CANON:
Gen_CanonMove(position, i, j, nPly);
break;
default:
break;
}
}
}
return m_nMoveCount;
}
void CMoveGenerator::Gen_KingMove(BYTE position[10][9], int i, int j, int nPly)
{
int x, y;
for (y = 0; y < 3; y++)
for (x = 3; x < 6; x++)
if (IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
for (y = 7; y < 10; y++)
for (x = 3; x < 6; x++)
if (IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
}
void CMoveGenerator::Gen_RBishopMove(BYTE position[10][9], int i, int j, int nPly)
{
int x, y;
for (y = 7; y < 10; y++)
for (x = 3; x < 6; x++)
if (IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
}
void CMoveGenerator::Gen_BBishopMove(BYTE position[10][9], int i, int j, int nPly)
{
int x, y;
for (y = 0; y < 3; y++)
for (x = 3; x < 6; x++)
if (IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
}
void CMoveGenerator::Gen_ElephantMove(BYTE position[10][9], int i, int j, int nPly)
{
int x, y;
x=j+2;
y=i+2;
if(x < 9 && y < 10 && IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
x=j+2;
y=i-2;
if(x < 9 && y>=0 && IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
x=j-2;
y=i+2;
if(x>=0 && y < 10 && IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
x=j-2;
y=i-2;
if(x>=0 && y>=0 && IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
}
void CMoveGenerator::Gen_HorseMove(BYTE position[10][9], int i, int j, int nPly)
{
int x, y;
x=j+2;
y=i+1;
if((x < 9 && y < 10) &&IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
x=j+2;
y=i-1;
if((x < 9 && y >= 0) &&IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
x=j-2;
y=i+1;
if((x >= 0 && y < 10) &&IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
x=j-2;
y=i-1;
if((x >= 0 && y >= 0) &&IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
x=j+1;
y=i+2;
if((x < 9 && y < 10) &&IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
x=j-1;
y=i+2;
if((x >= 0 && y < 10) &&IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
x=j+1;
y=i-2;
if((x < 9 && y >= 0) &&IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
x=j-1;
y=i-2;
if((x >= 0 && y >= 0) &&IsValidMove(position, j, i, x, y))
AddMove(j, i, x, y, nPly);
}
void CMoveGenerator::Gen_RPawnMove(BYTE position[10][9], int i, int j, int nPly)
{
int x, y;
int nChessID;
nChessID = position[i][j];
y = i - 1;
x = j;
if(y > 0 && !IsSameSide(nChessID, position[y][x]))
AddMove(j, i, x, y, nPly);
if(i < 5)
{
y=i;
x=j+1;
if(x < 9 && !IsSameSide(nChessID, position[y][x]))
AddMove(j, i, x, y, nPly);
x=j-1;
if(x >= 0 && !IsSameSide(nChessID, position[y][x]))
AddMove(j, i, x, y, nPly);
}
}
void CMoveGenerator::Gen_BPawnMove(BYTE position[10][9], int i, int j, int nPly)
{
int x, y;
int nChessID;
nChessID = position[i][j];
y = i + 1;
x = j;
if(y < 10 && !IsSameSide(nChessID, position[y][x]))
AddMove(j, i, x, y, nPly);
if(i > 4)
{
y=i;
x=j+1;
if(x < 9 && !IsSameSide(nChessID, position[y][x]))
AddMove(j, i, x, y, nPly);
x=j-1;
if(x >= 0 && !IsSameSide(nChessID, position[y][x]))
AddMove(j, i, x, y, nPly);
}
}
void CMoveGenerator::Gen_CarMove(BYTE position[10][9], int i, int j, int nPly)
{
int x, y;
int nChessID;
nChessID = position[i][j];
x=j+1;
y=i;
while(x < 9)
{
if( NOCHESS == position[y][x] )
AddMove(j, i, x, y, nPly);
else
{
if(!IsSameSide(nChessID, position[y][x]))
AddMove(j, i, x, y, nPly);
break;
}
x++;
}
x = j-1;
y = i;
while(x >= 0)
{
if( NOCHESS == position[y][x] )
AddMove(j, i, x, y, nPly);
else
{
if(!IsSameSide(nChessID, position[y][x]))
AddMove(j, i, x, y, nPly);
break;
}
x--;
}
x=j;
y=i+1;//
while(y < 10)
{
if( NOCHESS == position[y][x])
AddMove(j, i, x, y, nPly);
else
{
if(!IsSameSide(nChessID, position[y][x]))
AddMove(j, i, x, y, nPly);
break;
}
y++;
}
x = j;
y = i-1;//
while(y>=0)
{
if( NOCHESS == position[y][x])
AddMove(j, i, x, y, nPly);
else
{
if(!IsSameSide(nChessID, position[y][x]))
AddMove(j, i, x, y, nPly);
break;
}
y--;
}
}
void CMoveGenerator::Gen_CanonMove(BYTE position[10][9], int i, int j, int nPly)
{
int x, y;
BOOL flag;
int nChessID;
nChessID = position[i][j];
x=j+1; //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -