I wanted to create a simple game, akin to poker, where players are asked if they want to discard dice. It is required that there be at least a pair of dice. For example, if a player has 2 dice with a value of 5, they get 5 points, for 3 dice they get 10 points, and so on.
The script has an issue with counting points and ending a player's turn because it should end if the player cannot reshuffle the array, which they can only do once in the game, and if their dice cannot be paired.
If someone would be interested here's source code:
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <ctime>
#include <vector>
using namespace std;
void fill(vector<int>& player) {
for (int i = 0; i < 5; i++) {
player.push_back(rand() % 6 + 1);
}
}
int countOccurrences(const vector<int>& array, int value) {
int count = 0;
for (int i = 0; i < array.size(); i++) {
if (array[i] == value) {
count++;
}
}
return count;
}
int scoreAndRemove(vector<int>& array, int value, int& playerPoints) {
int points = 0;
int occurrences = countOccurrences(array, value);
if (occurrences >= 2) {
points = value * occurrences;
playerPoints += points;
for (int j = 0; j < array.size();) {
if (array[j] == value) {
array.erase(array.begin() + j);
} else {
j++;
}
}
} else if (occurrences == 3) {
points = (value * 2);
playerPoints += points;
for (int j = 0; j < array.size();) {
if (array[j] == value) {
array.erase(array.begin() + j);
} else {
j++;
}
}
} else if (occurrences == 4) {
points = (value * occurrences);
playerPoints += points;
for (int j = 0; j < array.size();) {
if (array[j] == value) {
array.erase(array.begin() + j);
} else {
j++;
}
}
}
return playerPoints;
}
void displayResults(const vector<int>& array) {
bool anythingDisplayed = false;
for (int i = 0; i < array.size(); i++) {
cout << array[i] << " ";
anythingDisplayed = true;
}
if (!anythingDisplayed) {
cout << "No dice.";
}
cout << endl;
}
bool checkPairing(const vector<int>& array) {
for (int i = 0; i < array.size(); i++) {
int occurrences = countOccurrences(array, array[i]);
if (occurrences >= 2) {
return true;
}
}
return false;
}
bool endTurn(const vector<int>& player) {
return player.size() <= 1;
}
bool endGame(const vector<int>& player1, const vector<int>& player2) {
return endTurn(player1) && endTurn(player2);
}
int countPoints(const vector<int>& player) {
int points = 0;
for (int i = 1; i <= 6; ++i) {
int occurrences = countOccurrences(player, i);
if (occurrences >= 2) {
points += i * occurrences;
} else if (occurrences == 3) {
points += i * 2;
} else if (occurrences == 4) {
points += i * occurrences;
}
}
return points;
}
int main() {
srand(time(NULL));
vector<int> dice1;
vector<int> dice2;
int playerPoints1 = 0;
int playerPoints2 = 0;
fill(dice1);
fill(dice2);
bool endTurnPlayer1 = false;
bool endTurnPlayer2 = false;
while (!endGame(dice1, dice2)) {
cout << "Player 1's Turn" << endl;
cout << "Player 1's Dice: ";
displayResults(dice1);
if (checkPairing(dice1)) {
int action;
do {
cout << "What do you want to do? (1 - Discard dice, 2 - Reshuffle, 3 - End turn): ";
cin >> action;
} while (action < 1 || action > 3);
switch (action) {
case 1: {
int value;
cout << "Enter the value of the dice to discard (1-6): ";
cin >> value;
while (value < 1 || value > 6) {
cout << "You entered an invalid value. Please enter a value between 1 and 6: ";
cin >> value;
}
playerPoints1 = scoreAndRemove(dice1, value, playerPoints1);
break;
}
case 2: {
dice1.clear();
fill(dice1);
break;
}
case 3: {
endTurnPlayer1 = true;
break;
}
}
} else {
cout << "No dice to pair. ";
if (!endTurnPlayer1) {
cout << "You can reshuffle." << endl;
dice1.clear();
fill(dice1);
} else {
cout << "End of player 1's turn." << endl;
}
endTurnPlayer1 = true;
}
cout << "Player 2's Turn" << endl;
cout << "Player 2's Dice: ";
displayResults(dice2);
if (checkPairing(dice2)) {
int action;
do {
cout << "What do you want to do? (1 - Discard dice, 2 - Reshuffle, 3 - End turn): ";
cin >> action;
} while (action < 1 || action > 3);
switch (action) {
case 1: {
int value;
cout << "Enter the value of the dice to discard (1-6): ";
cin >> value;
while (value < 1 || value > 6) {
cout << "You entered an invalid value. Please enter a value between 1 and 6: ";
cin >> value;
}
playerPoints2 = scoreAndRemove(dice2, value, playerPoints2);
break;
}
case 2: {
dice2.clear();
fill(dice2);
break;
}
case 3: {
endTurnPlayer2 = true;
break;
}
}
} else {
cout << "No dice to pair. ";
if (!endTurnPlayer2) {
cout << "You can reshuffle." << endl;
dice2.clear();
fill(dice2);
} else {
cout << "End of player 2's turn." << endl;
}
endTurnPlayer2 = true;
}
if (endTurnPlayer1 && endTurnPlayer2) {
break;
}
}
playerPoints1 = countPoints(dice1);
playerPoints2 = countPoints(dice2);
cout << "Final Scores:" << endl;
cout << "Player 1 scores " << playerPoints1 << " points." << endl;
cout << "Player 2 scores " << playerPoints2 << " points." << endl;
if (playerPoints1 > playerPoints2) {
cout << "Player 1 wins!" << endl;
} else if (playerPoints1 < playerPoints2) {
cout << "Player 2 wins!" << endl;
} else {
cout << "It's a tie!" << endl;
}
return 0;
}
Tried: I tried to do it with twice the number of functions for each single player, I tried to rebuild the point counting function using a loop but this is not the optimal solution.
Expecting: So that the program can count points correctly and players' turns end when no dice can be paired and there are no re-draws.