diff --git a/CMakeLists.txt b/CMakeLists.txt index 3da79d0..b2ba21e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ) diff --git a/gameplay.cxx b/gameplay.cxx index 9c7e881..10dec69 100644 --- a/gameplay.cxx +++ b/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(); } diff --git a/main.cxx b/main.cxx index 4e5162a..4d95812 100644 --- a/main.cxx +++ b/main.cxx @@ -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;