#include "gameplay.h" #include #include using namespace std; Gameplay::Gameplay(int rows, int cols, Player *player1, Player *player2): board(rows, cols), player1(player1), player2(player2) { } Gameplay::~Gameplay() { // TODO: needs revie free(this->player1); free(this->player2); } void Gameplay::playGame() { std::ofstream outputFile("output.txt"); if (!outputFile) { std::cerr << "Error: Could not open output file for writing." << std::endl; return; } // Write board dimensions to the output file outputFile << board.getRows() << " " << board.getCols() << std::endl; std::cout << board.getRows() << " " << board.getCols() << std::endl; char currentPlayer = player1->getName(); // Start with the RandomPlayer while (true) { int row, col; // Determine the move based on the current player if (currentPlayer == player1->getName()) { player1->selectLineLocation(board, row, col); } else { player2->selectLineLocation(board, row, col); } // Validate and place the move if (board.isLineValid(row, col)) { board.placeLine(row, col, currentPlayer); outputFile << currentPlayer << " " << row << " " << col << std::endl; std::cout << currentPlayer << " " << row << " " << col << std::endl; int boxesCompleted = board.checkAndMarkBox(row, col, currentPlayer); if (boxesCompleted == 0) { // Switch to the next player if no boxes are earned currentPlayer = (currentPlayer == player1->getName()) ? player2->getName() : player1->getName(); } } else { std::cout << currentPlayer << " made an invalid move at (" << row << ", " << col << ")!" << std::endl; return; } // Check if the board is full if (board.isFull()) { outputFile << "END" << std::endl; cout<<"END"<getName()) { randomPlayerBoxes++; } else if (boxOwner == player2->getName()) { strategicPlayerBoxes++; } } } // Display results std::cout << "Player " << player1->getName() << " has " << randomPlayerBoxes << " boxes." << std::endl; std::cout << "Player " << player2->getName() << " has " << strategicPlayerBoxes << " boxes." << std::endl; if (randomPlayerBoxes > strategicPlayerBoxes) { std::cout << "Player " << player1->getName() << " (wins)" << std::endl; } else if (strategicPlayerBoxes > randomPlayerBoxes) { std::cout << "Player " << player2->getName() << " (wins)" << std::endl; } else { std::cout << "The game is a tie!" << std::endl; } }