Board ExecuteMoveSequence(MoveSequence sequence, Board board) { Board finalboard = board; for(unsigned int i=0; i<sequence.moves.size(); i++) { finalboard = ExecuteMove(sequence.moves[i],finalboard); } return finalboard; }
/** * @brief Executes the first command in the commandQue */ void CMobileCAI::Execute() { Command& c = commandQue.front(); switch (c.id) { case CMD_SET_WANTED_MAX_SPEED: { ExecuteSetWantedMaxSpeed(c); return; } case CMD_MOVE: { ExecuteMove(c); return; } case CMD_PATROL: { ExecutePatrol(c); return; } case CMD_FIGHT: { ExecuteFight(c); return; } case CMD_GUARD: { ExecuteGuard(c); return; } case CMD_LOAD_ONTO: { ExecuteLoadUnits(c); return; } default: { CCommandAI::SlowUpdate(); return; } } }
/** * @brief Executes the first command in the commandQue */ void CMobileCAI::Execute(){ Command& c=commandQue.front(); switch(c.id){ case CMD_SET_WANTED_MAX_SPEED: return ExecuteSetWantedMaxSpeed(c); case CMD_MOVE: return ExecuteMove(c); case CMD_PATROL: return ExecutePatrol(c); case CMD_FIGHT: return ExecuteFight(c); case CMD_GUARD: return ExecuteGuard(c); default: return CCommandAI::SlowUpdate(); } }
SMove NextMove(const SGameState * const gameState) { SMove ret; //On recopie le plateau courant CpyBoard(gameState,currentGame); //On dévoile la valeur du pion ennemi gagnant de la dernière attaque if( lastEnemyAttackPiece != -1 && lastEnemyAttackPos.line != -1 && lastEnemyAttackPos.col != -1 ){ currentGame->board[lastEnemyAttackPos.line][lastEnemyAttackPos.col].piece = lastEnemyAttackPiece; lastEnemyAttackPiece = -1 ; lastEnemyAttackPos.line = -1 ; lastEnemyAttackPos.col = -1 ; } ListAvailableMoves(); int indexBestMove = ChoseBestMove(); if( indexBestMove == -1 ){ ret.start.line = -1; ret.start.col = -1; ret.end.line = -1; ret.end.col = -1; } else{ ret = listAvailableMoves[indexBestMove].move; } //On ajoute le mouvement au mouvements répétés MouvementDetail mvtD; mvtD.color = color; mvtD.move = ret; mvtD.piece = currentGame->board[ret.start.line][ret.start.col].piece; switch (repeatedMouv) { case 0: listRepeatedMouv[repeatedMouv] = mvtD; repeatedMouv++; break; case 1: if( mvtD.piece == listRepeatedMouv[0].piece ){ listRepeatedMouv[repeatedMouv] = mvtD; repeatedMouv++; } else{ repeatedMouv = 0; } break; case 2: if( (mvtD.piece == listRepeatedMouv[0].piece) && (CompareMoves(&mvtD.move,&listRepeatedMouv[0].move)==1) ){ listRepeatedMouv[repeatedMouv] = mvtD; repeatedMouv++; } else{ repeatedMouv = 0; } break; default: repeatedMouv = 0; break; } //On exécute le mouvement sur notre plateau courant ExecuteMove(currentGame,&ret); return ret; }
//void deroulement_du_jeu() int main(int argc, char *argv[] ) { // if( !InitGUI() ){ return 0; } if( !CheckCommand(argc,argv) ){ return 0; } if( !InitLogFile() ){ printf("Fail init logFile\n"); } PrintHeaderMatch(); //Il faut nbMaxCps pour chaques joueurs, donc on multiplie par 2 nbMaxCps *= 2; if( !PrintLibInitHeader() ){ printf("Fail print libHeader\n"); } if( !LoadLibraries() ){ return(0); } PrintTLine(); SGameState* gameState = CreateBoard(); //Srand pour obtenir des nombres aléatoires srand(time(NULL)); //Initialisation des joueurs j1InitLibrary(j1Name); j2InitLibrary(j2Name); if( strlen(pathIA1) && !strlen(pathIA2) ){ PrintPhysicalPlayers(NULL,j2Name); } else if( !strlen(pathIA1) && strlen(pathIA2) ){ PrintPhysicalPlayers(j1Name,NULL); } else if( !strlen(pathIA1) && !strlen(pathIA2) ){ PrintPhysicalPlayers(j1Name,j2Name); } else { PrintPhysicalPlayers(NULL,NULL); } PrintIAPlayers(pathIA1,pathIA2); PrintMaxCps(nbMaxCps/2); j1StartMatch(); j2StartMatch(); int nbMatch = 3; int j1Win = 0; int j2Win = 0; while( nbMatch>0 && j1Win<2 && j2Win<2){ PrintMatch(4-nbMatch); // InitBoard //On initialise le nombre de coups maximums int cptCps = nbMaxCps; j1NbPenalty = 0; j2NbPenalty = 0; InitGameState(gameState); EPiece j1BoardInit[4][10]; EPiece j2BoardInit[4][10]; SetPlayerColors(); j1StartGame(j1Color,j1BoardInit); j2StartGame(j2Color,j2BoardInit); if( j1Color == ECblue ){ PrintColors(j2Name, j1Name); } else{ PrintColors(j1Name, j2Name); } //Initialisation des pions sur le plateau if( j1Color == ECblue ){ if( !InitBlueBoard(gameState,j1BoardInit) ){PrintBoardInitError(ECblue);return 0;} if( !InitRedBoard(gameState,j2BoardInit) ){PrintBoardInitError(ECred);return 0;} } else{ if( !InitRedBoard(gameState,j1BoardInit) ){PrintBoardInitError(ECred);return 0;} if( !InitBlueBoard(gameState,j2BoardInit) ){PrintBoardInitError(ECblue);return 0;} } //Le premier joueur est le rouge EColor player = ECred; EColor winner = 0; PrintLine(); PrintGameState(gameState); while( !winner && cptCps>0 ){ SMove move; //Duplication du plateau SGameState *gameStateCpy = (SGameState*) malloc(sizeof(SGameState)); GameStateCpy(gameState,gameStateCpy); //Si c'est le tour du joueur rouge, on inverse son plateau if( player == ECred ){ RevertGame(gameStateCpy); } //On cache les pions du joueur ennemi HideColor(gameStateCpy,abs((player+1)%2)+2); if( player == j1Color ){ move = j1NextMove(gameStateCpy); } else{ move = j2NextMove(gameStateCpy); } if( player == ECred ){ move.start.line = 9-move.start.line; move.end.line = 9-move.end.line; } int moveType = CorrectMove(gameState,move,player); PrintMove(player,move); //Mouvement incorrecte if( moveType == 0 ){ if( player == j1Color ){ j1NbPenalty++; j1Penalty(); PrintInvalidMove(player,j1NbPenalty); if( j1NbPenalty == 3 ){ PrintPenalty(j1Name,j1Color); winner = abs((j1Color+1)%2)+2; break; } } else{ j2NbPenalty++; j2Penalty(); PrintInvalidMove(player,j2NbPenalty); if( j2NbPenalty == 3 ){ PrintPenalty(j2Name,j2Color); winner = abs((j2Color+1)%2)+2; break; } } } else{ //Attaque if( moveType == 1 ){ EPiece army = gameState->board[move.start.line][move.start.col].piece; EPiece enemy = gameState->board[move.end.line][move.end.col].piece; if( player == j1Color ){ j1AttackResult(move.start,army,move.end,enemy); j2AttackResult(move.end,enemy,move.start,army); } else{ j1AttackResult(move.end,enemy,move.start,army); j2AttackResult(move.start,army,move.end,enemy); } PrintAttackResult(player,move.start,army,move.end,enemy); } ExecuteMove(gameState,move,player); } PrintGameState(gameState); free(gameStateCpy); if( player == ECred ){ player = ECblue; } else{ player = ECred; } winner = Finished(gameState); cptCps--; } if( cptCps == 0){ PrintMaxMoveReached(); } else{ if( j1Color == winner ){ PrintMatchWinner(j1Name, winner, 4-nbMatch);j1Win++; } else{ PrintMatchWinner(j2Name, winner, 4-nbMatch);j2Win++; } } nbMatch--; j1EndGame(); j2EndGame(); printf("j1Win : %d\nj2Win : %d\n",j1Win,j2Win); } if( j1Win >= 2 ){ PrintGameWinner(j1Name, j1Color); } else{ PrintGameWinner(j2Name, j2Color); } j1EndMatch(); j2EndMatch(); free(gameState); return(0); }
char *GetCaptureText(enum ChessPiece board[8][8], char *longmove) { char *letters = "abcdefgh"; char *numbers = "87654321"; char *pieces = "oPNBRQKpnbrqk"; char *submove; char txt[512]; enum ChessPiece tempboard[8][8]; txt[0] = '\0'; submove = strdup(longmove); /* filter for castling */ if (strlen(longmove) == 9 && longmove[4] == ' ') { int x1, y1, x2, y2; x1 = (int)(strchr(letters, longmove[0]) - letters); y1 = (int)(strchr(numbers, longmove[1]) - numbers); x2 = (int)(strchr(letters, longmove[5]) - letters); y2 = (int)(strchr(numbers, longmove[6]) - numbers); if ((board[x1][y1] == CP_WKING && board[x2][y2] == CP_WROOK) || (board[x1][y1] == CP_WROOK && board[x2][y2] == CP_WKING) || (board[x1][y1] == CP_BKING && board[x2][y2] == CP_BROOK) || (board[x1][y1] == CP_WROOK && board[x2][y2] == CP_WKING)) { return ""; } } CopyBoard(board, tempboard); do { char *space = strchr(submove, ' '); int x, y, len; enum ChessPiece captured; if (space) { len = space - submove; *space = '\0'; } else { len = strlen(submove); } if (len == 3) { x = (int)(strchr(letters, submove[0]) - letters); y = (int)(strchr(numbers, submove[1]) - numbers); } else { x = (int)(strchr(letters, submove[2]) - letters); y = (int)(strchr(numbers, submove[3]) - numbers); } captured = tempboard[x][y]; if (tempboard[x][y] != CP_NONE) { strncat(txt, &(pieces[captured]), 1); strncat(txt, &(letters[x]), 1); strncat(txt, &(numbers[y]), 1); } ExecuteMove(tempboard, submove, 0, NULL, NULL); if (space) { submove = space + 1; } else { submove = NULL; } } while (submove && *submove); return strdup(txt); }
int IsMoveLegal(enum ChessPiece board[8][8], int oldx, int oldy, int newx, int newy, enum ChessCastling *castling, char **enpassanttarget) { enum ChessPiece tempboard[8][8]; char *letters = "abcdefgh"; char *numbers = "87654321"; char move[5]; int iswhitemove = IsPieceWhite(board[oldx][oldy]); if (CanPieceMoveHere(board, oldx, oldy, newx, newy, castling, enpassanttarget)) { /* is this a castling move? Then check the intermediate square */ if (oldx == 4 && oldy == 7 && newy == 7 && (newx == 6 || newx == 2) && board[oldx][oldy] == CP_WKING) { CopyBoard(board, tempboard); if (newx == 6) { strcpy(move, "e8f8"); ExecuteMove(tempboard, move, 0, NULL, NULL); if (!IsPositionLegal(tempboard, !iswhitemove)) { return 0; } } if (newx == 2) { strcpy(move, "e8d8"); ExecuteMove(tempboard, move, 0, NULL, NULL); if (!IsPositionLegal(tempboard, !iswhitemove)) { return 0; } } } if (oldx == 4 && oldy == 7 && newy == 0 && (newx == 6 || newx == 2) && board[oldx][oldy] == CP_BKING) { CopyBoard(board, tempboard); if (newx == 6) { strcpy(move, "e1f1"); ExecuteMove(tempboard, move, 0, NULL, NULL); if (!IsPositionLegal(tempboard, !iswhitemove)) { return 0; } } if (newx == 2) { strcpy(move, "e1d1"); ExecuteMove(tempboard, move, 0, NULL, NULL); if (!IsPositionLegal(tempboard, !iswhitemove)) { return 0; } } } CopyBoard(board, tempboard); move[0] = letters[oldx]; move[1] = numbers[oldy]; move[2] = letters[newx]; move[3] = numbers[newy]; move[4] = '\0'; ExecuteMove(tempboard, move, 0, NULL, NULL); return IsPositionLegal(tempboard, !iswhitemove); } return 0; }