show result in alphabetical order

This commit is contained in:
Sandipsinh Rathod 2024-11-19 20:14:47 -05:00
parent 4d69307f8c
commit 4c59f9dd78

@ -5,8 +5,8 @@
using namespace std;
Gameplay::Gameplay(int rows, int cols, Player *player1, Player *player2): board(rows, cols), player1(player1), player2(player2) {
Gameplay::Gameplay(int rows, int cols, Player *player1, Player *player2): board(rows, cols), player1(player1),
player2(player2) {
}
void Gameplay::playGame() {
@ -29,8 +29,8 @@ void Gameplay::playGame() {
if (boxesCompleted == 0) {
// Switch to the next player if no boxes are earned
currentPlayer = (currentPlayer == player1->getName())
? player2->getName()
: player1->getName();
? player2->getName()
: player1->getName();
}
} else {
cout << currentPlayer << " made an invalid move at (" << row << ", " << col << ")!" << '\n';
@ -39,7 +39,7 @@ void Gameplay::playGame() {
// Check if the board is full
if (board.isFull()) {
cout<<"END"<<endl;
cout << "END" << endl;
break;
}
}
@ -63,30 +63,45 @@ Gameplay::~Gameplay() {
*/
}
template<typename T>
void mySwap(T &a, T &b) {
if (!(a == b)) {
a ^= b;
b ^= a;
a ^= b;
}
}
void Gameplay::determineWinner() {
int randomPlayerBoxes = 0, strategicPlayerBoxes = 0;
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
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++;
player1Score++;
} else if (boxOwner == player2->getName()) {
strategicPlayerBoxes++;
player2Score++;
}
}
}
// Display results
cout << "Player " << player1->getName() << " has " << randomPlayerBoxes << " boxes." << '\n';
cout << "Player " << player2->getName() << " has " << strategicPlayerBoxes << " boxes." << '\n';
if (randomPlayerBoxes > strategicPlayerBoxes) {
cout << "Player " << player1->getName() << " (wins)" << '\n';
} else if (strategicPlayerBoxes > randomPlayerBoxes) {
cout << "Player " << player2->getName() << " (wins)" << '\n';
} else {
cout << "The game is a tie!" << '\n';
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)": "") << '\n';
cout << "Player " << players[1] << " has " << player2Score << " boxes " << (player2Score > player1Score? "(win)": "") << '\n';
if (player1Score == player2Score) {
cout << "It's a tie!" << '\n';
}
cout.flush();
}