fix tie print

This commit is contained in:
Sandipsinh Rathod 2024-11-19 23:54:35 -05:00
parent 4e9ce49935
commit 3de22dbb3e
3 changed files with 36 additions and 25 deletions

@ -3,17 +3,17 @@ project(hw4)
set(CMAKE_CXX_STANDARD 17)
add_executable(hw4 main.cpp
add_executable(hw4 main.cxx
board.h
random_player.h
strategic_player.h
gameplay.h
board.cpp
random_player.cpp
strategic_player.cpp
gameplay.cpp
board.cxx
random_player.cxx
strategic_player.cxx
player.h
raii.h
utils.cpp
utils.cxx
utils.h
gameplay.cxx
)

@ -5,14 +5,20 @@
using namespace std;
Gameplay::Gameplay(int rows, int cols, Player *player1, Player *player2): board(rows, cols), player1(player1),
player2(player2) {
Gameplay::Gameplay(
int rows,
int cols,
Player *player1,
Player *player2
): board(rows, cols),
player1(player1),
player2(player2) {
}
void Gameplay::playGame() {
// Write board dimensions to the output file
cout << board.getRows() << " " << board.getCols() << '\n';
char currentPlayer = player1->getName(); // Start with the RandomPlayer
char currentPlayer = player1->getName();
while (true) {
int row, col;
// Determine the move based on the current player
@ -90,12 +96,12 @@ void Gameplay::determineWinner() {
}
// Display results
cout << "Player " << players[0] << " has " << player1Score << " boxes " << (player1Score > player2Score? "(win)": "") << '\n';
cout << "Player " << players[1] << " has " << player2Score << " boxes " << (player2Score > player1Score? "(win)": "") << '\n';
if (player1Score == player2Score) {
cout << "It's a tie!" << '\n';
}
cout << "Player " << players[0] << " has " << player1Score << " boxes " << (player1Score > player2Score
? "(win)"
: player1Score == player2Score? "(tie)": "") << '\n';
cout << "Player " << players[1] << " has " << player2Score << " boxes " << (player2Score > player1Score
? "(win)"
: player1Score == player2Score? "(tie)": "") << '\n';
cout.flush();
}

@ -50,22 +50,27 @@ int main() {
}
// Determine player types and initialize gameplay
Player *randomPlayerName, *strategicPlayerName;
Player *player1, *player2;
if (equalsIgnoreCase(player1Type, "Random") && equalsIgnoreCase(player2Type, "Strategic")) {
randomPlayerName = new RandomPlayer(player1Name);
strategicPlayerName = new StrategicPlayer(player2Name);
player1 = new RandomPlayer(player1Name);
player2 = new StrategicPlayer(player2Name);
} else if (equalsIgnoreCase(player1Type, "Strategic") && equalsIgnoreCase(player2Type, "Random")) {
randomPlayerName = new RandomPlayer(player2Name);
strategicPlayerName = new StrategicPlayer(player1Name);
player1 = new RandomPlayer(player2Name);
player2 = new StrategicPlayer(player1Name);
} else if (equalsIgnoreCase(player1Type, "Random") && equalsIgnoreCase(player2Type, "Random")) {
randomPlayerName = new RandomPlayer(player2Name);
strategicPlayerName = new RandomPlayer(player1Name);
player1 = new RandomPlayer(player2Name);
player2 = new RandomPlayer(player1Name);
} else {
randomPlayerName = new StrategicPlayer(player2Name);
strategicPlayerName = new StrategicPlayer(player1Name);
player1 = new StrategicPlayer(player2Name);
player2 = new StrategicPlayer(player1Name);
}
if (player1->getName() != player1Name) {
Player *tmp = player1;
player1 = player2;
player2 = tmp;
}
Gameplay(rows, cols, randomPlayerName, strategicPlayerName)
Gameplay(rows, cols, player1, player2)
.playGame();
return 0;