2024-11-19 23:54:26 +00:00
|
|
|
#include "gameplay.h"
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2024-11-20 01:04:51 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2024-11-19 23:54:26 +00:00
|
|
|
using namespace std;
|
2024-11-20 01:04:51 +00:00
|
|
|
|
2024-11-19 23:54:26 +00:00
|
|
|
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
|
2024-11-20 00:46:02 +00:00
|
|
|
cout << board.getRows() << " " << board.getCols() << '\n';
|
2024-11-19 23:54:26 +00:00
|
|
|
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);
|
2024-11-20 00:46:02 +00:00
|
|
|
cout << currentPlayer << " " << row << " " << col << '\n';
|
2024-11-19 23:54:26 +00:00
|
|
|
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 {
|
2024-11-20 00:46:02 +00:00
|
|
|
cout << currentPlayer << " made an invalid move at (" << row << ", " << col << ")!" << '\n';
|
2024-11-19 23:54:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the board is full
|
|
|
|
if (board.isFull()) {
|
|
|
|
cout<<"END"<<endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the final board state
|
|
|
|
board.printBoard();
|
|
|
|
|
|
|
|
// Determine the winner and display results
|
|
|
|
determineWinner();
|
2024-11-20 00:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Gameplay::~Gameplay() {
|
|
|
|
// Free the player objects
|
|
|
|
free(this->player1);
|
|
|
|
free(this->player2);
|
|
|
|
|
|
|
|
// Alternatively
|
|
|
|
/*
|
|
|
|
delete this->player1;
|
|
|
|
delete this->player2;
|
|
|
|
*/
|
2024-11-19 23:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Gameplay::determineWinner() {
|
|
|
|
int randomPlayerBoxes = 0, strategicPlayerBoxes = 0;
|
|
|
|
|
|
|
|
// Iterate over the centers of boxes
|
|
|
|
for (int r = 1; r < 2 * board.getRows() - 2; r += 2) { // Center rows of boxes
|
|
|
|
for (int c = 1; c < 2 * board.getCols() - 2; c += 2) { // Center columns of boxes
|
|
|
|
char boxOwner = board.get(r, c); // Get the owner of the box
|
|
|
|
if (boxOwner == player1->getName()) {
|
|
|
|
randomPlayerBoxes++;
|
|
|
|
} else if (boxOwner == player2->getName()) {
|
|
|
|
strategicPlayerBoxes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display results
|
2024-11-20 00:46:02 +00:00
|
|
|
cout << "Player " << player1->getName() << " has " << randomPlayerBoxes << " boxes." << '\n';
|
|
|
|
cout << "Player " << player2->getName() << " has " << strategicPlayerBoxes << " boxes." << '\n';
|
2024-11-19 23:54:26 +00:00
|
|
|
|
|
|
|
if (randomPlayerBoxes > strategicPlayerBoxes) {
|
2024-11-20 00:46:02 +00:00
|
|
|
cout << "Player " << player1->getName() << " (wins)" << '\n';
|
2024-11-19 23:54:26 +00:00
|
|
|
} else if (strategicPlayerBoxes > randomPlayerBoxes) {
|
2024-11-20 00:46:02 +00:00
|
|
|
cout << "Player " << player2->getName() << " (wins)" << '\n';
|
2024-11-19 23:54:26 +00:00
|
|
|
} else {
|
2024-11-20 00:46:02 +00:00
|
|
|
cout << "The game is a tie!" << '\n';
|
2024-11-19 23:54:26 +00:00
|
|
|
}
|
2024-11-20 00:46:02 +00:00
|
|
|
}
|