void update(const std::string& player, const std::string& type, const std::string& value) { if (player != "game" && player != myName_) { return; } if (type == "round") { round_ = stringToInt(value); } else if (type == "move") { move_ = stringToInt(value); } else if (type == "field") { std::vector<std::string> rawValues; split(value, ',', rawValues); for (Square i = 0; i < 81; ++i) { Square ct = StLS[i][0], mg = StLS[i][1]; if (stringToPiece(rawValues[i]) != position_->get_piece(ct, mg)) { position_->do_move(ct, mg); break; } } } else if (type == "macroboard") { } else { debug("Unknown update <" + type + ">."); } }
/* * Main function for handling the "set" command for setting pieces on the board during the settings stage. * * @return: -1 if the input tile, color or piece type were not formatted legally * -2 if the input tile is not a possible position on the board * -8 if the specified piece cannot be added there are already * too many pieces of this type on the board * 0 otherwise */ int setPiece(char* command){ int x, y; char tile[6]; char colorString[6]; char pieceString[7]; if (sscanf(command, "set %5s %5s %6s", tile, colorString, pieceString) != 3){ return -1; } if (readTile(tile, &x, &y) == -1){ return -1; } if (!Board_isInRange(x, y)){ return -2; } int color = stringToColor(colorString); char piece = stringToPiece(pieceString, color); if (piece == 0 || color == -1){ return -1; } if (PieceCounter_isAtMax(counter, piece, x, y)){ return -8; } if ((piece == Board_BLACK_PAWN || piece == Board_WHITE_PAWN) && Board_isFurthestRowForPlayer(color, y)){ return -8; } char removedPiece = Board_getPiece(&board, x, y); PieceCounter_update(counter, removedPiece, -1, x, y); PieceCounter_update(counter, piece, 1, x, y); Board_setPiece(&board, x, y, piece); Board_updateKingPosition(&board, x, y); return 0; }
PossibleMove* readMove(char* command, int* exitcode){ char fromTile[6]; char toTile[6]; char promoteToAsString[10]; char promoteTo = 0; //initializing promoteTo to avoid problems with promotion-less move commands strncpy(promoteToAsString, "undefined", 10); *exitcode = 0; if (sscanf(command, "move %5s to %5s %6s", fromTile, toTile, promoteToAsString) < 2){ *exitcode = -1; return NULL; } int fromX, fromY, toX, toY; if (readTile(fromTile, &fromX, &fromY) == -1 || readTile(toTile, &toX, &toY) == -1){ *exitcode = -1; return NULL; } if (!Board_isInRange(fromX, fromY) || !Board_isInRange(toX, toY) ){ *exitcode = -2; return NULL; } if (!str_equals(promoteToAsString, "undefined")){ promoteTo = stringToPiece(promoteToAsString, turn); if (!promoteTo){ *exitcode = -1; // the promotion was not input legally return NULL; } if (promoteTo && (!Board_isFurthestRowForPlayer(turn, toY) || !pieceIsPawn(fromX, fromY))){ *exitcode = -6; // the promotion was input legally, but the move itself is illegal return NULL; } } if(pieceIsPawn(fromX, fromY) && Board_isFurthestRowForPlayer(turn, toY) && promoteTo == 0){ promoteTo = (turn = WHITE)? 'q':'Q'; //default promotion } if (!Board_isInRange(fromX, fromY) || !Board_isInRange(toX, toY)){ *exitcode = -2; return NULL; } if (Board_getColor(&board, fromX, fromY) != turn){ *exitcode = -5; return NULL; } PossibleMove* move = PossibleMove_new(fromX, fromY, toX, toY, promoteTo, &board); if (!move){ *exitcode = 1; return NULL; } return move; }
void setting(const std::string& type, const std::string& value) { if (type == "timebank") { timebank_ = stringToInt(value); } else if (type == "time_per_move") { timePerMove_ = stringToInt(value); } else if (type == "player_names") { split(value, ',', playerNames_); } else if (type == "your_bot") { myName_ = value; } else if (type == "your_botid") { botId_ = stringToPiece(value); } else { debug("Unknown setting <" + type + ">."); } }