📄 gchess.cpp
字号:
/* Copyright (C) 2006, Mike Gashler This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. see http://www.gnu.org/copyleft/lesser.html*/#include "GChess.h"bool GChessBoard::CheckMove(int* pOutMoves, int* pnMoves, int col, int row, bool bWhite){ if(col < 0 || row < 0) return true; bool white; Piece piece = GetPiece(col, row, &white); if(piece > 0 && white == bWhite) return true; int index = (*pnMoves) * 2; pOutMoves[index] = col; pOutMoves[index + 1] = row; (*pnMoves)++; return (piece > 0);}bool GChessBoard::CheckPawnMove(int* pOutMoves, int* pnMoves, int col, int row, bool bDiagonal, bool bWhite){ if(col < 0 || row < 0) return true; bool white; Piece piece = GetPiece(col, row, &white); if(piece > 0 && (!bDiagonal || white == bWhite)) return true; int index = (*pnMoves) * 2; pOutMoves[index] = col; pOutMoves[index + 1] = row; (*pnMoves)++; return (piece > 0);}int GChessBoard::GetMoves(int* pOutMoves, int col, int row){ bool bWhite; Piece piece = GetPiece(col, row, &bWhite); int nMoves = 0; int i, j; switch(piece) { case Pawn: if(bWhite) { if(!CheckPawnMove(pOutMoves, &nMoves, col, Inc(row), false, bWhite) && row == 1) CheckPawnMove(pOutMoves, &nMoves, col, Inc(Inc(row)), false, bWhite); CheckPawnMove(pOutMoves, &nMoves, Inc(col), Inc(row), true, bWhite); CheckPawnMove(pOutMoves, &nMoves, Dec(col), Inc(row), true, bWhite); } else { if(!CheckPawnMove(pOutMoves, &nMoves, col, Dec(row), false, bWhite) && row == 6) CheckPawnMove(pOutMoves, &nMoves, col, Dec(Dec(row)), false, bWhite); CheckPawnMove(pOutMoves, &nMoves, Inc(col), Dec(row), true, bWhite); CheckPawnMove(pOutMoves, &nMoves, Dec(col), Dec(row), true, bWhite); } break; case Bishop: for(i = Inc(col), j=Inc(row); true; i = Inc(col), j = Inc(row)) if(CheckMove(pOutMoves, &nMoves, i, j, bWhite)) break; for(i = Dec(col), j=Inc(row); true; i = Dec(col), j = Inc(row)) if(CheckMove(pOutMoves, &nMoves, i, j, bWhite)) break; for(i = Inc(col), j=Dec(row); true; i = Inc(col), j = Dec(row)) if(CheckMove(pOutMoves, &nMoves, i, j, bWhite)) break; for(i = Dec(col), j=Dec(row); true; i = Dec(col), j = Dec(row)) if(CheckMove(pOutMoves, &nMoves, i, j, bWhite)) break; break; case Knight: CheckMove(pOutMoves, &nMoves, Inc(Inc(col)), Inc(row), bWhite); CheckMove(pOutMoves, &nMoves, Inc(col), Inc(Inc(row)), bWhite); CheckMove(pOutMoves, &nMoves, Dec(col), Inc(Inc(row)), bWhite); CheckMove(pOutMoves, &nMoves, Dec(Dec(col)), Inc(row), bWhite); CheckMove(pOutMoves, &nMoves, Dec(Dec(col)), Dec(row), bWhite); CheckMove(pOutMoves, &nMoves, Dec(col), Dec(Dec(row)), bWhite); CheckMove(pOutMoves, &nMoves, Inc(col), Dec(Dec(row)), bWhite); CheckMove(pOutMoves, &nMoves, Inc(Inc(col)), Dec(row), bWhite); break; case Rook: for(i = Inc(col); true; i = Inc(col)) if(CheckMove(pOutMoves, &nMoves, i, row, bWhite)) break; for(i = Dec(col); true; i = Dec(col)) if(CheckMove(pOutMoves, &nMoves, i, row, bWhite)) break; for(j = Inc(row); true; j = Inc(row)) if(CheckMove(pOutMoves, &nMoves, col, j, bWhite)) break; for(j = Dec(row); true; j = Dec(row)) if(CheckMove(pOutMoves, &nMoves, col, j, bWhite)) break; break; case Queen: for(i = Inc(col); true; i = Inc(col)) if(CheckMove(pOutMoves, &nMoves, i, row, bWhite)) break; for(i = Dec(col); true; i = Dec(col)) if(CheckMove(pOutMoves, &nMoves, i, row, bWhite)) break; for(j = Inc(row); true; j = Inc(row)) if(CheckMove(pOutMoves, &nMoves, col, j, bWhite)) break; for(j = Dec(row); true; j = Dec(row)) if(CheckMove(pOutMoves, &nMoves, col, j, bWhite)) break; for(i = Inc(col), j=Inc(row); true; i = Inc(col), j = Inc(row)) if(CheckMove(pOutMoves, &nMoves, i, j, bWhite)) break; for(i = Dec(col), j=Inc(row); true; i = Dec(col), j = Inc(row)) if(CheckMove(pOutMoves, &nMoves, i, j, bWhite)) break; for(i = Inc(col), j=Dec(row); true; i = Inc(col), j = Dec(row)) if(CheckMove(pOutMoves, &nMoves, i, j, bWhite)) break; for(i = Dec(col), j=Dec(row); true; i = Dec(col), j = Dec(row)) if(CheckMove(pOutMoves, &nMoves, i, j, bWhite)) break; break; case King: CheckMove(pOutMoves, &nMoves, Inc(col), row, bWhite); CheckMove(pOutMoves, &nMoves, Inc(col), Inc(row), bWhite); CheckMove(pOutMoves, &nMoves, col, Inc(row), bWhite); CheckMove(pOutMoves, &nMoves, Dec(col), Inc(row), bWhite); CheckMove(pOutMoves, &nMoves, Dec(col), row, bWhite); CheckMove(pOutMoves, &nMoves, Dec(col), Dec(row), bWhite); CheckMove(pOutMoves, &nMoves, col, Dec(row), bWhite); CheckMove(pOutMoves, &nMoves, Inc(col), Dec(row), bWhite); break; default: break; } return nMoves;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -