108 lines
3.1 KiB
C++
108 lines
3.1 KiB
C++
#include "gameplay.h"
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <cstdlib>
|
|
|
|
using namespace std;
|
|
|
|
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();
|
|
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);
|
|
cout << currentPlayer << " " << row << " " << col << '\n';
|
|
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 {
|
|
cout << currentPlayer << " made an invalid move at (" << row << ", " << col << ")!" << '\n';
|
|
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();
|
|
}
|
|
|
|
Gameplay::~Gameplay() {
|
|
// Free the player objects
|
|
free(this->player1);
|
|
free(this->player2);
|
|
}
|
|
|
|
template<typename T>
|
|
void mySwap(T &a, T &b) {
|
|
if (!(a == b)) {
|
|
a ^= b;
|
|
b ^= a;
|
|
a ^= b;
|
|
}
|
|
}
|
|
|
|
void Gameplay::determineWinner() {
|
|
int player1Score = 0, player2Score = 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()) {
|
|
player1Score++;
|
|
} else if (boxOwner == player2->getName()) {
|
|
player2Score++;
|
|
}
|
|
}
|
|
}
|
|
|
|
char players[2] = {player1->getName(), player2->getName()};
|
|
if (players[0] > players[1]) {
|
|
mySwap(players[0], players[1]);
|
|
mySwap(player1Score, player2Score);
|
|
}
|
|
|
|
// Display results
|
|
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();
|
|
}
|