show result in alphabetical order
This commit is contained in:
parent
4d69307f8c
commit
4c59f9dd78
45
gameplay.cpp
45
gameplay.cpp
@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
using namespace std;
|
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() {
|
void Gameplay::playGame() {
|
||||||
@ -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() {
|
void Gameplay::determineWinner() {
|
||||||
int randomPlayerBoxes = 0, strategicPlayerBoxes = 0;
|
int player1Score = 0, player2Score = 0;
|
||||||
|
|
||||||
// Iterate over the centers of boxes
|
// Iterate over the centers of boxes
|
||||||
for (int r = 1; r < 2 * board.getRows() - 2; r += 2) { // Center rows of boxes
|
for (int r = 1; r < 2 * board.getRows() - 2; r += 2) {
|
||||||
for (int c = 1; c < 2 * board.getCols() - 2; c += 2) { // Center columns of boxes
|
// 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
|
char boxOwner = board.get(r, c); // Get the owner of the box
|
||||||
if (boxOwner == player1->getName()) {
|
if (boxOwner == player1->getName()) {
|
||||||
randomPlayerBoxes++;
|
player1Score++;
|
||||||
} else if (boxOwner == player2->getName()) {
|
} else if (boxOwner == player2->getName()) {
|
||||||
strategicPlayerBoxes++;
|
player2Score++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char players[2] = {player1->getName(), player2->getName()};
|
||||||
|
if (players[0] > players[1]) {
|
||||||
|
mySwap(players[0], players[1]);
|
||||||
|
mySwap(player1Score, player2Score);
|
||||||
|
}
|
||||||
|
|
||||||
// Display results
|
// Display results
|
||||||
cout << "Player " << player1->getName() << " has " << randomPlayerBoxes << " boxes." << '\n';
|
cout << "Player " << players[0] << " has " << player1Score << " boxes " << (player1Score > player2Score? "(win)": "") << '\n';
|
||||||
cout << "Player " << player2->getName() << " has " << strategicPlayerBoxes << " boxes." << '\n';
|
cout << "Player " << players[1] << " has " << player2Score << " boxes " << (player2Score > player1Score? "(win)": "") << '\n';
|
||||||
|
|
||||||
if (randomPlayerBoxes > strategicPlayerBoxes) {
|
if (player1Score == player2Score) {
|
||||||
cout << "Player " << player1->getName() << " (wins)" << '\n';
|
cout << "It's a tie!" << '\n';
|
||||||
} else if (strategicPlayerBoxes > randomPlayerBoxes) {
|
|
||||||
cout << "Player " << player2->getName() << " (wins)" << '\n';
|
|
||||||
} else {
|
|
||||||
cout << "The game is a tie!" << '\n';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cout.flush();
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user