void ChessBoard::generatePawnMoves(ChessBoard& board, int sqSrc, Moves& mvs) { int sqDst; if (board.m_sdPlayer != SQ_SIDE(sqSrc)) { sqDst = sqSrc - 1; if (IN_BOARD(sqDst) && board.canMove(board, sqSrc, sqDst)) { mvs.append(ChessBoard::mv(sqSrc, sqDst)); } sqDst = sqSrc + 1; if (IN_BOARD(sqDst) && board.canMove(board, sqSrc, sqDst)) { mvs.append(ChessBoard::mv(sqSrc, sqDst)); } } if (board.m_sdPlayer==0) { sqDst = sqSrc - 16; } else{ sqDst = sqSrc + 16; } if (IN_BOARD(sqDst) && board.canMove(board, sqSrc, sqDst)) { mvs.append(ChessBoard::mv(sqSrc, sqDst)); } }
UltimateTicTacToeMontecarloAI::Moves UltimateTicTacToeMontecarloAI::movementOptions(Board const& board, int const previousMove) const { int gridIndex = previousMove % GRID_SIZE; bool playAny = previousMove < 0 || gridWinner(board.grids, gridIndex) || gridFull(board.grids, gridIndex); Moves options; if(playAny) { for(int i = 0; i < BOARD_SIZE; ++i) { if(board.grids.at(i) == 0) { options.append(i); } } //qDebug() << "Play to any grid," << options.size() << "options"; } else { for(int i = 0; i < GRID_SIZE; ++i) { int position = gridIndex * GRID_SIZE + i; if(board.grids.at(position) == 0) { options.append(position); } } //qDebug() << "Play to grid" << gridIndex << "," << options.size() << "options"; } return options; }
void ChessBoard::generateCannonMoves(ChessBoard& board, int sqSrc, Moves& mvs) { static const int delta[4] = {-16, -1, 1, 16}; for(int i=0; i<4; i++) { int sqDst = sqSrc; bool skiped = false; while (true) { sqDst += delta[i]; if (!IN_BOARD(sqDst)) break; if (!board.m_data[sqDst]) { if (!skiped) { mvs.append(ChessBoard::mv(sqSrc, sqDst)); } } else{ if (!skiped) { skiped = true; } else{ int pc = board.m_data[sqDst]; if (pc!=0) { if (board.canMove(board, sqSrc, sqDst)) { mvs.append(ChessBoard::mv(sqSrc, sqDst)); } } break; } } } } }
void ChessBoard::generateRookMoves(ChessBoard& board, int sqSrc, Moves& mvs) { static const int delta[4] = {-16, -1, 1, 16}; for(int i=0; i<4; i++) { int sqDst = sqSrc; while (true) { sqDst += delta[i]; if (!IN_BOARD(sqDst)) break; if (!board.m_data[sqDst]) { mvs.append(ChessBoard::mv(sqSrc, sqDst)); } else{ if (board.canMove(board, sqSrc, sqDst)) { mvs.append(ChessBoard::mv(sqSrc, sqDst)); } break; } } } }
void ChessBoard::generateAdvisorMoves(ChessBoard& board, int sqSrc, Moves& mvs) { static const int ccAdvisorDelta[4] = {-17, -15, 15, 17}; for(int i=0; i<4; i++) { int sqDst = sqSrc + ccAdvisorDelta[i]; if (IN_FORT(sqDst)) { if (board.canMove(board, sqSrc, sqDst)) { mvs.append(ChessBoard::mv(sqSrc, sqDst)); } } } }
void ChessBoard::generateKingMoves(ChessBoard& board, int sqSrc, Moves& mvs) { static const int ccKingDelta[4] = {-16, -1, 1, 16}; for(int i=0; i<4; i++) { int sqDst = sqSrc + ccKingDelta[i]; if (IN_FORT(sqDst)) { if (board.canMove(board, sqSrc, sqDst)) { mvs.append(ChessBoard::mv(sqSrc, sqDst)); } } } }
void ChessBoard::generateBishopMoves(ChessBoard& board, int sqSrc, Moves& mvs) { static const int ccBishopDelta[4] = {-34, -30, 30, 34}; static const int ccBishopPin[4] = {-17, -15, 15, 17}; for(int i=0; i<4; i++) { int sqDst = sqSrc + ccBishopDelta[i]; if (!IN_BOARD(sqDst)) continue; int sqPin = sqSrc + ccBishopPin[i]; if (board.m_data[sqPin]) continue; if (SAME_HALF(sqSrc, sqDst)) { if (board.isInBoard(sqDst) && board.canMove(board, sqSrc, sqDst)) { mvs.append(ChessBoard::mv(sqSrc, sqDst)); } } } }
void ChessBoard::generateKnightMoves(ChessBoard& board, int sqSrc, Moves& mvs) { static const int ccKingDelta[4] = {-16, -1, 1, 16}; static const int ccKnightDelta[4][2] = {{-33, -31}, {-18, 14}, {-14, 18}, {31, 33}}; for (int i=0; i<4; i++) { int sqPin = sqSrc + ccKingDelta[i]; if (IN_BOARD(sqPin) && board.m_data[sqPin]==0) { //马腿没有东西 for(int j=0; j<2; j++) { int sqDst = sqSrc + ccKnightDelta[i][j]; if (board.isInBoard(sqDst) && board.canMove(board, sqSrc, sqDst)) { mvs.append(ChessBoard::mv(sqSrc, sqDst)); } } } } }