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) set(CMAKE_CXX_STANDARD 17)
add_executable(hw4 main.cpp add_executable(hw4 main.cxx
board.h board.h
random_player.h random_player.h
strategic_player.h strategic_player.h
gameplay.h gameplay.h
board.cpp board.cxx
random_player.cpp random_player.cxx
strategic_player.cpp strategic_player.cxx
gameplay.cpp
player.h player.h
raii.h raii.h
utils.cpp utils.cxx
utils.h utils.h
gameplay.cxx
) )

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

@ -50,22 +50,27 @@ int main() {
} }
// Determine player types and initialize gameplay // Determine player types and initialize gameplay
Player *randomPlayerName, *strategicPlayerName; Player *player1, *player2;
if (equalsIgnoreCase(player1Type, "Random") && equalsIgnoreCase(player2Type, "Strategic")) { if (equalsIgnoreCase(player1Type, "Random") && equalsIgnoreCase(player2Type, "Strategic")) {
randomPlayerName = new RandomPlayer(player1Name); player1 = new RandomPlayer(player1Name);
strategicPlayerName = new StrategicPlayer(player2Name); player2 = new StrategicPlayer(player2Name);
} else if (equalsIgnoreCase(player1Type, "Strategic") && equalsIgnoreCase(player2Type, "Random")) { } else if (equalsIgnoreCase(player1Type, "Strategic") && equalsIgnoreCase(player2Type, "Random")) {
randomPlayerName = new RandomPlayer(player2Name); player1 = new RandomPlayer(player2Name);
strategicPlayerName = new StrategicPlayer(player1Name); player2 = new StrategicPlayer(player1Name);
} else if (equalsIgnoreCase(player1Type, "Random") && equalsIgnoreCase(player2Type, "Random")) { } else if (equalsIgnoreCase(player1Type, "Random") && equalsIgnoreCase(player2Type, "Random")) {
randomPlayerName = new RandomPlayer(player2Name); player1 = new RandomPlayer(player2Name);
strategicPlayerName = new RandomPlayer(player1Name); player2 = new RandomPlayer(player1Name);
} else { } else {
randomPlayerName = new StrategicPlayer(player2Name); player1 = new StrategicPlayer(player2Name);
strategicPlayerName = new StrategicPlayer(player1Name); 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(); .playGame();
return 0; return 0;