/*Add legal Moves for Bishop/Rook/Queen/King at Location to Linked List*/ int getBRQKMoves(Game *game, Location *loc, ListNode *temp){ int killflag, blockedflag, i, j, dist, len; int flag = 0; MoveType type; MoveType *list = getMoveTypeList(game->board, loc); len = getMoveTypeListLength(game->board, loc); for (i = 0; i < len; i++){ killflag = 0; blockedflag = 0; type = list[i]; dist = distFrom(game->board, loc, type); if (isKing(game->board, loc) && dist>0){ dist = 1; } for (j = 1; j <= dist&&!killflag&&!blockedflag; j++){ Location *tLoc = setLocation(loc->column + shiftCol(game->board, loc, type)*j, loc->row + shiftRow(game->board, loc, type)*j); if (legalMoveToKing(game, loc, tLoc)){ Move *move = setMove(loc, tLoc, NONETYPE); if (isValidMove(game, move)){ addMoveToList(temp, move); flag = 1; } else{ freeMove(move); } } else{ free(tLoc); } } } return flag; }
void getNextPlay(int stream, play_t *play, deck_t *deck) { static char buffer[MAXLINE]; static char cmd[MAXLINE]; static char from[MAXLINE]; static char onto[MAXLINE]; read(stream,buffer,MAXLINE); sscanf(buffer,"%s",cmd); if (cmd[0] == 'm') { // it's a move onto a lain stack sscanf(buffer,"%s %s",cmd,from); play->from = cardOf(from,deck); if (isKing(play->from)) { play->type = KING_PLAY; play->onto = NULL; } else { play->type = MOVE_PLAY; sscanf(buffer,"%s %s %s",cmd,from,onto); play->onto = cardOf(onto,deck); } } else if (cmd[0] == 'p') { // it's a play onto the arena play->type = ARENA_PLAY; sscanf(buffer,"%s %s",cmd,from); play->from = cardOf(from,deck); play->onto = NULL; } else if (cmd[0] == 'd') { // it's a draw play->type = DRAW_PLAY; play->from = NULL; play->onto = NULL; } }
std::vector<Move> Chessboard::getMoves(char color) { std::vector<Move> moves; char kingInCheck = 0; for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { if (getColor(row, col) == color) { std::vector<Move> m; char piece = get(row, col); if (isPawn(piece)) { m = getPawnMoves(*this, row, col); } else if (isKnight(piece)) { m = getKnightMoves(*this, row, col); } else if (isBishop(piece)) { m = getBishopMoves(*this, row, col); } else if (isRook(piece)) { m = getRookMoves(*this, row, col); } else if (isQueen(piece)) { m = getQueenMoves(*this, row, col); } else if (isKing(piece)) { m = getKingMoves(*this, row, col); } moves.insert(moves.end(), m.begin(), m.end()); } } } buildCheckBoard(moves); return moves; }
/*Determine whether Linked List contains a Move capturing the King. The function is called to determine whether the opposing Color is in Mate*/ int hasMate(char board[BOARD_SIZE][BOARD_SIZE], ListNode *list){ ListNode *temp = list; while (temp != NULL){ if (isKing(board, temp->move->dest)){ return 1; } temp = temp->next; } return 0; }
static int performAction(pawn *currentPawn, pawn *targetPawn) { if (targetPawn == NULL) { ///< Targeted field was empty. return UNIT_MOVED; } if (isKing(currentPawn)) { if (isKing(targetPawn)) { return BOTH_UNITS_DIED; } else if (isPeasant(targetPawn)) { return ATTACKER_KILLED; } else if (isKnight(targetPawn)) { return DEFENDER_KILLED_KING; } else { return ERROR; } } else if (isPeasant(currentPawn)) { if (isKing(targetPawn)) { return DEFENDER_KILLED; } else if (isPeasant(targetPawn)) { return BOTH_UNITS_DIED; } else if (isKnight(targetPawn)) { return DEFENDER_KILLED; } else { return ERROR; } } else if (isKnight(currentPawn)) { if (isKing(targetPawn)) { return ATTACKER_KILLED_KING; } else if (isPeasant(targetPawn)) { return ATTACKER_KILLED; } else if (isKnight(targetPawn)) { return BOTH_UNITS_DIED; } else { return ERROR; } } else { return ERROR; } }
MoveList* getMoves(char** board, int player) { MoveList* result = NULL; for (int i = 0; i < BOARD_SIZE; ++i) for (int j = 0; j < BOARD_SIZE; ++j) { Position position = {.x = i, .y = j}; if (playerInPosition(position, board, player)) { MoveList* discMoves = isKing(getValueInPosition(position, board)) ? getKingMoves(position, board, player) : getManMoves(position, board, player); result = bestMoveList(result, discMoves); } } return result; }
bool CCardOperatorBase::isTypeStraight(const list<int>& lstCards){ bool isStraight = false; do { if (lstCards.size() < STRAIGHT_MIN_COUNTS) break; // 先转化成 value list<int> lstCardsValue; for (int seq : lstCards) { lstCardsValue.push_back(getValue(seq)); } lstCardsValue.sort(); if (isKing(lstCardsValue.back())) break; // 如果带A, A拿掉以后必须头是2或尾是K bool hasA = isA(lstCardsValue.front()); if (hasA) { lstCardsValue.pop_front(); if (!isK(lstCardsValue.back()) && !is2(lstCardsValue.front()) ) break; } // 去掉A后肯定是顺 int tmp = -1; bool straight = true; for (int v : lstCardsValue) { if (tmp == -1){ tmp = v; continue; } if (v != tmp + 1) { straight = false; break; } } isStraight = straight; } while (false); return isStraight; }
void Piece::draw(QPainter *e) { e->setBrush(c); e->drawEllipse(QPoint(posX,posY), 40, 40); if (isKing()) { int yUpperBound = (posY / 80) * 80; int xLeftBound = (posX / 80) * 80; e->setPen(QColor(255,255,255)); e->drawLine(xLeftBound + 40, yUpperBound + 10, xLeftBound + 40, yUpperBound + 70); e->drawLine(xLeftBound + 25, yUpperBound + 25, xLeftBound + 55, yUpperBound + 25); } e->setPen(QColor(0,0,0)); }
int move(int x1, int y1, int x2, int y2) { /// Validates fields. if (!isValidField(currentGame.mapSize, x1, y1) || !isValidField(currentGame.mapSize, x2, y2)) { return ERROR; } /// Validates initialization. if (currentGame.isInitialized == false) { return ERROR; } /// Validates distance between fields. if (distMax(x1, y1, x2, y2) != 1) { return ERROR; } /// Picks pawn from board. pawn *currentPawn = hashmapRemove(currentGame.gameMap, x1, y1); /// Checks if any pawn was picked. if (currentPawn == NULL) { return ERROR; } /// Checks if pawn is able to move. if (currentPawn->lastMove >= currentGame.currentRound) { free(currentPawn); return ERROR; } /// Checks if pawn belong to current player. if (currentGame.playerTurn != getPawnAdherence(currentPawn)) { free(currentPawn); return ERROR; } currentPawn->lastMove = currentGame.currentRound; pawn *targetPawn = hashmapRemove(currentGame.gameMap, x2, y2); if (getPawnAdherence(currentPawn) == getPawnAdherence(targetPawn)) { ///< Player wants to move onto his own pawn. free(currentPawn); if (targetPawn != NULL) { free(targetPawn); } return ERROR; } else { int actionResult = performAction(currentPawn, targetPawn); switch (actionResult) { /// Frees pawn that was killed. /// Puts pawn that survived. case UNIT_MOVED: currentPawn->x = (unsigned int) x2 - 1; ///< 1-based to 0-based. currentPawn->y = (unsigned int) y2 - 1; hashmapPut(currentGame.gameMap, currentPawn); break; case ATTACKER_KILLED: currentPawn->x = (unsigned int) x2 - 1; currentPawn->y = (unsigned int) y2 - 1; hashmapPut(currentGame.gameMap, currentPawn); free(targetPawn); break; case DEFENDER_KILLED: hashmapPut(currentGame.gameMap, targetPawn); free(currentPawn); break; case ATTACKER_KILLED_KING: currentPawn->x = (unsigned int) x2 - 1; currentPawn->y = (unsigned int) y2 - 1; hashmapPut(currentGame.gameMap, currentPawn); free(targetPawn); /// Player that made move is victorious. return currentGame.playerTurn == PLAYER_A_TURN ? PLAYER_A_WON : PLAYER_B_WON; case DEFENDER_KILLED_KING: free(currentPawn); hashmapPut(currentGame.gameMap, targetPawn); /// Player that made move is beaten. return currentGame.playerTurn == PLAYER_A_TURN ? PLAYER_B_WON : PLAYER_A_WON; case BOTH_UNITS_DIED: if (isKing(currentPawn)) { free(currentPawn); free(targetPawn); return DRAW; ///< Kings killed each other. } else { free(currentPawn); free(targetPawn); } break; default: return ERROR; } } return GAME_OK; }