From 273662fde026c0030b77531b56924a5a6654247a Mon Sep 17 00:00:00 2001 From: Sandipsinh Rathod Date: Tue, 19 Nov 2024 19:11:06 -0500 Subject: [PATCH] cleanup --- CMakeLists.txt | 3 ++- gameplay.cpp | 8 ++++---- gameplay.h | 1 - io.cpp | 7 +++++++ main.cpp | 2 +- player.h | 6 ------ random_player.cpp | 10 ++++++++++ random_player.h | 15 +++++---------- strategic_player.cpp | 43 ++++++++++++++++++++++++++++++------------- strategic_player.h | 10 ++-------- 10 files changed, 61 insertions(+), 44 deletions(-) create mode 100644 io.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a707f82..1657a1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,4 +13,5 @@ add_executable(hw4 main.cpp strategic_player.cpp gameplay.cpp gameplay.cpp - player.h) + player.h + io.cpp) diff --git a/gameplay.cpp b/gameplay.cpp index 0231551..577432a 100644 --- a/gameplay.cpp +++ b/gameplay.cpp @@ -13,15 +13,15 @@ Gameplay::~Gameplay() { } void Gameplay::playGame() { - std::ofstream outputFile("output.txt"); + ofstream outputFile("output.txt"); if (!outputFile) { - std::cerr << "Error: Could not open output file for writing." << std::endl; + cerr << "Error: Could not open output file for writing." << endl; return; } // Write board dimensions to the output file - outputFile << board.getRows() << " " << board.getCols() << std::endl; - std::cout << board.getRows() << " " << board.getCols() << std::endl; + outputFile << board.getRows() << " " << board.getCols() << endl; + cout << board.getRows() << " " << board.getCols() << endl; char currentPlayer = player1->getName(); // Start with the RandomPlayer while (true) { int row, col; diff --git a/gameplay.h b/gameplay.h index e321beb..e6f4aa5 100644 --- a/gameplay.h +++ b/gameplay.h @@ -6,7 +6,6 @@ #include "strategic_player.h" class Gameplay { -private: Board board; Player* player1; // Use pointers to enable polymorphism Player* player2; diff --git a/io.cpp b/io.cpp new file mode 100644 index 0000000..88ddf24 --- /dev/null +++ b/io.cpp @@ -0,0 +1,7 @@ +#include +#include + +template +void print(T val) { + std::cout << val; +} diff --git a/main.cpp b/main.cpp index 1e20c81..2831308 100644 --- a/main.cpp +++ b/main.cpp @@ -5,7 +5,7 @@ using namespace std; -int main(int argc, char* argv[]) { +int main() { int rows, cols; char player1Name, player2Name; string player1Type, player2Type; diff --git a/player.h b/player.h index 894a0d3..5d737a9 100644 --- a/player.h +++ b/player.h @@ -4,13 +4,7 @@ #include "board.h" class Player { -private: - char name; - public: - Player(char name){ - this->name = name; - } virtual void selectLineLocation(Board& board, int& row, int& col) = 0; virtual char getName() const = 0; }; diff --git a/random_player.cpp b/random_player.cpp index 29abeed..057c21a 100644 --- a/random_player.cpp +++ b/random_player.cpp @@ -1,6 +1,12 @@ #include "random_player.h" #include + +RandomPlayer::RandomPlayer(char name) { + this->name = name; + srand(time(0)); // Seed the random number generator +} + void RandomPlayer::selectLineLocation(Board& board, int& row, int& col) { int rows = board.getRows(); int cols = board.getCols(); @@ -11,3 +17,7 @@ void RandomPlayer::selectLineLocation(Board& board, int& row, int& col) { col = rand() % (2 * cols - 1); // Random column within the board dimensions } while (!board.isLineValid(row, col)); // Check if the move is valid } + +char RandomPlayer::getName() const { + return this->name; +} diff --git a/random_player.h b/random_player.h index 1a89224..2fcf1bb 100644 --- a/random_player.h +++ b/random_player.h @@ -6,20 +6,15 @@ #include #include -class RandomPlayer: public Player { -private: +class RandomPlayer : public Player { char name; + public: - RandomPlayer(char name): Player(name) { - srand(static_cast(time(nullptr))); + RandomPlayer(char name); - this->name = name; - } - void selectLineLocation(Board& board, int& row, int& col); + void selectLineLocation(Board &board, int &row, int &col); - char getName() const override { - return this->name; - } + char getName() const; }; #endif //HW4_RANDOM_PLAYER_H diff --git a/strategic_player.cpp b/strategic_player.cpp index 2fe8781..0dd174d 100644 --- a/strategic_player.cpp +++ b/strategic_player.cpp @@ -1,19 +1,25 @@ #include "strategic_player.h" -using namespace std; -void StrategicPlayer::selectLineLocation(Board& board, int& row, int& col) { +StrategicPlayer::StrategicPlayer(char name) { + this->name = name; +} + +void StrategicPlayer::selectLineLocation(Board &board, int &row, int &col) { int rows = board.getRows(); int cols = board.getCols(); // Step 1: Try to complete a box - for (int r = 1; r < 2 * rows - 2; r += 2) { // Iterate over box centers (odd rows) - for (int c = 1; c < 2 * cols - 2; c += 2) { // Iterate over box centers (odd cols) + for (int r = 1; r < 2 * rows - 2; r += 2) { + // Iterate over box centers (odd rows) + for (int c = 1; c < 2 * cols - 2; c += 2) { + // Iterate over box centers (odd cols) // Check adjacent lines for an opportunity to complete a box if (board.isLineValid(r - 1, c) && // Top line board.get(r + 1, c) != ' ' && // Bottom line board.get(r, c - 1) != ' ' && // Left line - board.get(r, c + 1) != ' ') { // Right line + board.get(r, c + 1) != ' ') { + // Right line row = r - 1; col = c; return; @@ -21,7 +27,8 @@ void StrategicPlayer::selectLineLocation(Board& board, int& row, int& col) { if (board.isLineValid(r + 1, c) && // Bottom line board.get(r - 1, c) != ' ' && // Top line board.get(r, c - 1) != ' ' && // Left line - board.get(r, c + 1) != ' ') { // Right line + board.get(r, c + 1) != ' ') { + // Right line row = r + 1; col = c; return; @@ -29,7 +36,8 @@ void StrategicPlayer::selectLineLocation(Board& board, int& row, int& col) { if (board.isLineValid(r, c - 1) && // Left line board.get(r - 1, c) != ' ' && // Top line board.get(r + 1, c) != ' ' && // Bottom line - board.get(r, c + 1) != ' ') { // Right line + board.get(r, c + 1) != ' ') { + // Right line row = r; col = c - 1; return; @@ -37,7 +45,8 @@ void StrategicPlayer::selectLineLocation(Board& board, int& row, int& col) { if (board.isLineValid(r, c + 1) && // Right line board.get(r - 1, c) != ' ' && // Top line board.get(r + 1, c) != ' ' && // Bottom line - board.get(r, c - 1) != ' ') { // Left line + board.get(r, c - 1) != ' ') { + // Left line row = r; col = c + 1; return; @@ -45,16 +54,19 @@ void StrategicPlayer::selectLineLocation(Board& board, int& row, int& col) { } } -// Step 2: Avoid moves that leave a box with one line remaining - for (int r = 0; r < 2 * rows - 1; ++r) { // Iterate over all valid rows - for (int c = 0; c < 2 * cols - 1; ++c) { // Iterate over all valid cols + // Step 2: Avoid moves that leave a box with one line remaining + for (int r = 0; r < 2 * rows - 1; ++r) { + // Iterate over all valid rows + for (int c = 0; c < 2 * cols - 1; ++c) { + // Iterate over all valid cols if (board.isLineValid(r, c)) { // Simulate placing the line board.placeLine(r, c, name); // Check if this move leaves a box with only one line remaining bool createsOpportunity = false; - for (int nr = 1; nr < 2 * rows - 2; nr += 2) { // Iterate over box centers + for (int nr = 1; nr < 2 * rows - 2; nr += 2) { + // Iterate over box centers for (int nc = 1; nc < 2 * cols - 2; nc += 2) { if (board.get(nr, nc) == ' ') { int adjacentLines = 0; @@ -63,7 +75,8 @@ void StrategicPlayer::selectLineLocation(Board& board, int& row, int& col) { if (nc > 0 && board.get(nr, nc - 1) != ' ') adjacentLines++; // Left line if (nc < 2 * cols - 2 && board.get(nr, nc + 1) != ' ') adjacentLines++; // Right line - if (adjacentLines == 3) { // Opponent can complete this box + if (adjacentLines == 3) { + // Opponent can complete this box createsOpportunity = true; break; } @@ -94,3 +107,7 @@ void StrategicPlayer::selectLineLocation(Board& board, int& row, int& col) { } } } + +char StrategicPlayer::getName() const { + return this->name; +} diff --git a/strategic_player.h b/strategic_player.h index 2a75e1b..e2ffe81 100644 --- a/strategic_player.h +++ b/strategic_player.h @@ -9,18 +9,12 @@ #include "player.h" class StrategicPlayer: public Player { -private: char name; public: - - StrategicPlayer(char name): Player(name) { - this->name = name; - } + StrategicPlayer(char name); void selectLineLocation(Board& board, int& row, int& col); - char getName() const override { - return this->name; - } + char getName() const; }; #endif //HW4_STRATEGIC_PLAYER_H