diff --git a/Pontu/include/model/Game.h b/Pontu/include/model/Game.h index 13d9ce1..fb11a41 100644 --- a/Pontu/include/model/Game.h +++ b/Pontu/include/model/Game.h @@ -47,6 +47,7 @@ typedef struct { Player arrPlayers[4]; ///< The array of all the players in this game size_t nbPlayers; Board board; ///< The board for this game + int lastRank; } Game; /** diff --git a/Pontu/src/model/Game.c b/Pontu/src/model/Game.c index 015e871..03e30b6 100644 --- a/Pontu/src/model/Game.c +++ b/Pontu/src/model/Game.c @@ -37,7 +37,8 @@ Game newGame(const size_t nbPlayers, const Player player[]) .nb_rounds = 0, .phase = PLACEMENT, .board = newBoard(nbPlayers), - .nbPlayers = nbPlayers + .nbPlayers = nbPlayers, + .lastRank = 0 }; for (size_t player_i = 0; player_i < nbPlayers; player_i++) @@ -53,6 +54,16 @@ Game newGame(const size_t nbPlayers, const Player player[]) return g; } +void eliminatePlayer(Game* game, const size_t playerId) { + game->arrPlayers[playerId].eliminationTurn = game->nb_rounds; + ++game->lastRank; + game->arrPlayers[playerId].rank = game->lastRank; + fprintf(stderr, "Rank : %d\n", game->lastRank); +} + +void endGame(Game* game) { + game->phase = GAME_ENDED; +} void changePhaseOrPlayerTurn(Game* game) { @@ -74,6 +85,14 @@ void changePhaseOrPlayerTurn(Game* game) case RM_BRIDGE: { const size_t lastPlayerId = game->currentPlayerID; + if (areAllPlayerPiecesStucked(lastPlayerId, game->board.arrPieces, game->board.nbPieces)) { + eliminatePlayer(game, lastPlayerId); + if (game->nbPlayers-1 == game->lastRank) { + endGame(game); + return; + } + } + do { game->currentPlayerID++; @@ -81,13 +100,20 @@ void changePhaseOrPlayerTurn(Game* game) { game->currentPlayerID = 0; } - if (lastPlayerId == game->currentPlayerID) { + /*if (lastPlayerId == game->currentPlayerID) { game->phase = GAME_ENDED; return; + }*/ + + if (game->arrPlayers[game->currentPlayerID].rank != 0 && areAllPlayerPiecesStucked(game->currentPlayerID, game->board.arrPieces, game->board.nbPieces)) { + eliminatePlayer(game, game->currentPlayerID); + if (game->nbPlayers-1 == game->lastRank) { + endGame(game); + return; + } } - } while (areAllPlayerPiecesStucked(game->currentPlayerID, game->board.arrPieces, - game->board.nbPieces)); + } while (game->arrPlayers[game->currentPlayerID].rank != 0); fflush(stderr); diff --git a/Pontu/src/view/GameMain.c b/Pontu/src/view/GameMain.c index bdddc2a..f3ffddb 100644 --- a/Pontu/src/view/GameMain.c +++ b/Pontu/src/view/GameMain.c @@ -73,6 +73,9 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend case GameAction_MovePiece: drawMovePiece(renderer, &boardRect, &inputElement.data.move.start, &inputElement.data.move.end, textureHandler.textures[TEXTURE_PieceRed], textureHandler.textures[TEXTURE_Island]); SDL_RenderPresent(renderer); + if (game.phase == GAME_ENDED) { + *generalState = GS_EndOfGameMenu; + } break; } @@ -98,6 +101,9 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend if (actionRealized != GameAction_None) { inputProcessor.selectedCase = newCoord(-1,-1); + if (game.phase == GAME_ENDED) { + *generalState = GS_EndOfGameMenu; + } } break; @@ -116,6 +122,7 @@ void gameView(GeneralState* generalState, SDL_Window* window, SDL_Renderer* rend freeTextureHandler(&textureHandler); array_Coord_Free(&interactiveCases); + freeGameInputProcessor(&inputProcessor); SDL_Quit(); }