cmpsc330hw4/gameplay.cxx

108 lines
3.1 KiB
C++
Raw Permalink Normal View History

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-20 04:54:35 +00:00
Gameplay::Gameplay(
int rows,
int cols,
Player *player1,
Player *player2
): board(rows, cols),
player1(player1),
player2(player2) {
2024-11-19 23:54:26 +00:00
}
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-20 04:54:35 +00:00
char currentPlayer = player1->getName();
2024-11-19 23:54:26 +00:00
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())
2024-11-20 01:14:47 +00:00
? player2->getName()
: player1->getName();
2024-11-19 23:54:26 +00:00
}
} 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()) {
2024-11-20 01:14:47 +00:00
cout << "END" << endl;
2024-11-19 23:54:26 +00:00
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);
2024-11-19 23:54:26 +00:00
}
2024-11-20 01:14:47 +00:00
template<typename T>
void mySwap(T &a, T &b) {
if (!(a == b)) {
a ^= b;
b ^= a;
a ^= b;
}
}
2024-11-19 23:54:26 +00:00
void Gameplay::determineWinner() {
2024-11-20 01:14:47 +00:00
int player1Score = 0, player2Score = 0;
2024-11-19 23:54:26 +00:00
// Iterate over the centers of boxes
2024-11-20 01:14:47 +00:00
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
2024-11-19 23:54:26 +00:00
if (boxOwner == player1->getName()) {
2024-11-20 01:14:47 +00:00
player1Score++;
2024-11-19 23:54:26 +00:00
} else if (boxOwner == player2->getName()) {
2024-11-20 01:14:47 +00:00
player2Score++;
2024-11-19 23:54:26 +00:00
}
}
}
2024-11-20 01:14:47 +00:00
char players[2] = {player1->getName(), player2->getName()};
if (players[0] > players[1]) {
mySwap(players[0], players[1]);
mySwap(player1Score, player2Score);
}
2024-11-19 23:54:26 +00:00
// Display results
2024-11-20 04:54:35 +00:00
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';
2024-11-20 01:14:47 +00:00
cout.flush();
}