I have written a one player Yahtzee game in C++ but I would like to make it multi player. I know I need to create or alter an array to keep track of the different scores and that there needs to be some kind of loop for however many players there are (up to 5) but I'm not quite sure how to make that kind of array or where to put the loop. I've looked for examples of multi player games but I can't find any that really help. Suggestions?
Here is my code;
CODE
#include "myPrompt.h"
#include <fstream>
const int DICE_CNT = 5;
const int NUM_ROLL = 3;
const int NUM_SIDES = 6;
const int CAT_NUM = 13;
const int NUM_TURNS = 13;
///////////////////////////CALL RULES//////////////////////
void Rules () {
//Calls and displays text document with rules
ifstream input;
string line;
ifstream myfile ("YtzRules.txt");
if (myfile.is_open()) {
while (!myfile.eof()) {
getline (myfile, line);
cout << line << endl;
}
myfile.close();
system ("pause");
system ("cls");
}
else
cout << "Unable to open file";
}
//////////////////////////ROLLDICE/////////////////////////
void RollDice (int myDie[DICE_CNT], bool Keep [DICE_CNT]) {
// rolls dice
Dice die(NUM_SIDES);
int count;
/*
for (count = 0; count < DICE_CNT; count ++) {
if (Keep [count])
myDie [count] = die.Roll();
}
*/
// TEST YAHTZEE
for(count = 0; count < DICE_CNT; count++) {
cout << "Enter Dice# " << count+1;
cin >> myDie[count];
}
}
///////////////////////KEEPERS/////////////////////////////
void Keepers (bool Keep [DICE_CNT]) {
// Asks users which dice they want to keep
int loop;
for (loop = 0; loop < DICE_CNT; loop ++) {
cout << "Dice #" << loop + 1 << " - ";
if (PromptYesNo("Would you like to reroll this die?") ) {
Keep [loop] = true; //no roll
cout << endl;
}
else {
Keep [loop] = false; //roll
cout << endl;
}
}
}
///////////////////////SORT DICE////////////////////////
void SortDice (int myDie[]) {
//Sorts the dice for the straight categories
int count, holder, idx;
for (count = 1; count< DICE_CNT; count ++) {
holder = myDie [count];
idx = count;
while (idx > 0 && holder < myDie [idx-1]) {
myDie [idx] = myDie [idx-1];
idx --;
}
myDie [idx] = holder;
}
}
///////////////////////CALC UPPPER SECTION///////////////
void CalcUpper (bool noavail [NUM_TURNS], int myDie [DICE_CNT], int score [CAT_NUM], int& upTotal, int& catnum) {
//Calculates the upper section scores and totals
int cnt;
int cat = 1;
int sum = 0;
while (cat <= 6) {
if (catnum == cat) {
noavail [catnum - 1] = true;
for (cnt = 0; cnt < DICE_CNT; cnt ++) {
if (myDie [cnt] == cat)
sum = sum + myDie[cnt];
}
score [catnum - 1] = sum;
break;
}
cat ++;
}
upTotal = upTotal + score [catnum-1];
}
///////////////////////THREE OF A KIND////////////////////
void ThreeKind (bool noavail [NUM_TURNS], int myDie [DICE_CNT], int score [CAT_NUM], int& lowTotal) {
// Calculate three of a kind
int cnt = 0;
int count = 0;
int holder = 0;
noavail [6] = true;
SortDice (myDie);
if (myDie [0] == myDie [2] || myDie [2] == myDie [4] || myDie [1] && myDie [3] == myDie [2]) {
for (cnt = 0; cnt <= DICE_CNT - 1; cnt ++) {
if (myDie [cnt] == myDie [cnt + 1]) {
count ++;
}
holder = holder + myDie [cnt];
}
}
if (count = 2)
score [6] = holder;
else
score [6] = 0;
lowTotal = lowTotal + score [6];
}
////////////////////////FOUR OF A KIND/////////////////////
void FourKind (bool noavail [NUM_TURNS], int myDie [DICE_CNT], int score [CAT_NUM], int& lowTotal) {
//Calculates four of a kind
int cnt = 0;
int count = 0;
int holder = 0;
noavail [7] = true;
SortDice (myDie);
for (cnt = 0; cnt <= DICE_CNT - 1; cnt ++) {
if (myDie [cnt] == myDie [cnt + 1]) {
count ++;
}
holder = holder + myDie [cnt];
}
if (count >= 3)
score [7] = holder;
else
score [7] = 0;
lowTotal = lowTotal + score [7];
}
////////////////////////FULL HOUSE/////////////////////////
void FullHouse (bool noavail [NUM_TURNS], int myDie [DICE_CNT], int score [CAT_NUM], int& lowTotal) {
//Calculates Full House
int cnt = 0;
int count = 0;
noavail [8] = true;
SortDice (myDie);
if (myDie [0] == myDie [1] && myDie [2] == myDie [3] && myDie [4] || myDie [0] == myDie [1] && myDie [2] && myDie [3] == myDie [4]) {
for (cnt = 0; cnt <= DICE_CNT - 1; cnt ++) {
if (myDie [cnt] == myDie [cnt + 1])
count ++;
}
}
if (count >= 3 || count >= 4)
score [8] = 25;
else
score [8] = 0;
lowTotal = lowTotal + score [8];
}
///////////////////////CALC SMALL STRAIGHT/////////////////
void CalcSmall(bool noavail [NUM_TURNS], int myDie [DICE_CNT], int score [CAT_NUM], int& lowTotal) {
// Calculate small straight
int cnt;
int count = 0;
noavail [9] = true;
SortDice (myDie);
if (myDie [1] + 1 == myDie [2] && myDie [2] + 1 == myDie [3]) {
for (cnt = 0; cnt < DICE_CNT; cnt ++) {
if (myDie [cnt] + 1 == myDie [cnt+1])
count++;
}
if (count == 3 || count == 4)
score [9] = 30;
lowTotal = lowTotal + score [9];
}
else
score [9] = 0;
}
///////////////////////CALC LARGE STRAIGHT/////////////////
void CalcLarge (int myDie[DICE_CNT], bool noavail[NUM_TURNS], int score[CAT_NUM], int& lowTotal) {
//Calculates large straight
int cnt;
int count = 0;
noavail [10] = true;
SortDice(myDie);
for (cnt = 0; cnt < DICE_CNT; cnt ++) {
if (myDie [cnt] + 1 == myDie [cnt+1])
count++;
}
if (count == 4)
score [10] = 40;
else
score [10] = 0;
lowTotal = lowTotal + score [10];
}
///////////////////////CALC YAHTZEE////////////////////////
void CalcYahtzee (int myDie [DICE_CNT], bool noavail[NUM_TURNS], int score[CAT_NUM], int& lowTotal) {
// Calculates score for Yahtzee Category
int count = 0;
int cnt = 0;
noavail [11] = true;
for (count = 0; count < DICE_CNT; count ++) {
if (myDie [count] == myDie [count+1])
cnt++;
}
if (cnt == 4 )
score [11] = 50;
else
score [11] = 0;
lowTotal = lowTotal + score [11];
}
///////////////////////CALC CHANCE/////////////////////////
void CalcChance (int myDie [DICE_CNT], bool noavail [NUM_TURNS], int score [CAT_NUM], int& lowTotal) {
//calculates chance
int cnt;
noavail [12] = true;
for (cnt = 0; cnt < DICE_CNT; cnt ++) {
score [12] = score [12] + myDie [cnt];
}
lowTotal = lowTotal + score [12];
}
///////////////////////DISPLAY BOARD////////////////////////
void DisplayBoard (bool noavail [NUM_TURNS], int score [CAT_NUM], int& upTotal, int& lowTotal, int YBcnt) {
//Displays category scores and upper, lower, and grand totals
int gTotal = 0;
int UpTotal = upTotal;
int bonus = 0;
cout << endl << " ~~~~~~~~~~~~~~~~~~~~~~~~~ \t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "\tUpper Section\t\t\t\tLower Section\n";
cout << endl << " ~~~~~~~~~~~~~~~~~~~~~~~~~ \t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" << endl;
cout << " 1. Ones: ";
if (noavail [0])
cout << score [0];
cout << "\t\t\t\t7. Three of a Kind: ";
if (noavail [6])
cout << score [6];
cout << "\n 2. Twos: ";
if (noavail [1])
cout << score [1];
cout << "\t\t\t\t8. Four of a Kind: ";
if (noavail [7])
cout << score [7];
cout << "\n 3. Threes: ";
if (noavail [2])
cout << score [2];
cout << "\t\t\t\t9. Full House: ";
if (noavail [8])
cout << score [8];
cout << "\n 4. Fours: ";
if (noavail [3])
cout << score [3];
cout << "\t\t\t\t10. Small Straight: ";
if (noavail [9])
cout << score [9];
cout << "\n 5. Fives: ";
if (noavail [4])
cout << score [4];
cout << "\t\t\t\t11. Large Straight: ";
if (noavail [10])
cout << score [10];
cout << "\n 6. Sixes: ";
if (noavail [5])
cout << score [5] << endl;
cout << "\t\t\t\t12. Yahtzee: ";
if (noavail [11])
cout << score [11];
cout << "\t\t\t\t\t\t\t\t\t13. Chance: ";
if (noavail [12])
cout << score [12];
cout << endl << "\n Upper Section Total Score: " << upTotal;
if (upTotal >= 63) {
bonus = 35;
upTotal = upTotal + bonus;
}
if (YBcnt > 0) {
YBcnt = YBcnt * 100;
}
cout << "\t\tYahtzee Bonus: " << YBcnt;
cout << endl << " Bonus (if total is <= 63): " << bonus;
cout << "\n\n ------------------------------------------------------------------" << endl;
cout << endl <<" Upper Total plus Bonus: " << upTotal << "\t\tLower Section Total Score: " << lowTotal + YBcnt;
gTotal = lowTotal + YBcnt + upTotal;
cout << endl << endl << " Grand Total: " << gTotal << endl << endl;
}
/////////////////////DISPLAY DICE/////////////////////////
void DisplayDice (int boardDie [DICE_CNT]) {
// displays current dice
int cnt;
system("cls");
cout << "\t-------------------------" << endl;
cout << "\t Your Current Dice\n" << endl;
for (cnt = 0; cnt < DICE_CNT; cnt ++) {
cout << " " << boardDie [cnt] << "\t";
}
cout << endl;
cout << "\t-------------------------" << endl << endl;
}
///////////////////////PLAY YAHTZEE////////////////////////
void PlayYahtzee () {
//Play game
int myDie [DICE_CNT];
bool Keep [DICE_CNT];
bool noavail [CAT_NUM];
int rollnum;
int cnt;
int catnum;
int score [CAT_NUM];
int numturns = 1;
int upTotal = 0;
int lowTotal = 0;
int YBcnt = 0;
int count = 0;
for (cnt = 0; cnt < NUM_TURNS; cnt ++) {
noavail [cnt] = false;
}
while (numturns < NUM_TURNS) {
Keep [DICE_CNT] = true;
RollDice (myDie, Keep);
DisplayDice (myDie);
rollnum = 1;
while (rollnum < NUM_ROLL) {
if (PromptYesNo ("Would you like to reroll any of your dice?")) {
cout << endl;
Keepers (Keep);
system ("pause");
system ("cls");
RollDice (myDie, Keep);
DisplayDice (myDie);
rollnum ++;
}
else
rollnum = NUM_ROLL;
}
DisplayDice(myDie);
DisplayBoard(noavail, score, upTotal, lowTotal, YBcnt);
if (score [11] == 50) {
for (count = 0; count < DICE_CNT; count ++) {
if (myDie [count] == myDie [count+1])
cnt++;
}
if (cnt == 4)
lowTotal = lowTotal + 100;
YBcnt ++;
}
catnum = PromptRange ("Into which category would you like to place your dice?",1,13);
if (catnum <= 6 && catnum >= 1)
CalcUpper (noavail, myDie, score, upTotal,catnum);
else if (catnum == 7)
ThreeKind (noavail, myDie, score, lowTotal);
else if (catnum == 8)
FourKind (noavail, myDie, score, lowTotal);
else if (catnum == 9)
FullHouse (noavail, myDie, score, lowTotal);
else if (catnum == 10)
CalcSmall (noavail, myDie, score, lowTotal);
else if (catnum == 11)
CalcLarge (myDie, noavail, score, lowTotal);
else if (catnum == 12)
CalcYahtzee (myDie,noavail, score, lowTotal);
else if (catnum == 13)
CalcChance (myDie, noavail, score, lowTotal);
DisplayDice (myDie);
DisplayBoard (noavail, score, upTotal, lowTotal, YBcnt);
system ("pause");
system ("cls");
numturns ++;
cout << "Turn # " << numturns;
}
}
///////////////////////MENU/////////////////////////////////
int Menu () {
// allows user to choose which option they want (includes directions)
int ans;
bool loop = true;
while (loop) {
cout << "1. Rules" << endl;
cout << "2. Play Yahtzee" << endl;
cout << "3. Quit Program" << endl << endl;
ans = PromptRange ("Please choose how you would like to proceed: ", 1, 3);
if (ans == 1) {
Rules ();
system ("cls");
}
else if (ans == 2) {
int playnum = 0;
playnum = PromptRange ("\n\nHow many players are there?: ", 1, 5);
system ("cls");
PlayYahtzee ();
}
else if (ans == 3) {
cout << "Thank you for playing Yahtzee!" << endl;
loop = false;
}
}
exit (1);
return 0;
}
///////////////////////MAIN////////////////////////////////
int main () {
// runs all other functions, sets noavail to false, asks for category
cout << "~~~~~ Welcome to Yahtzee ~~~~~" << endl << endl;
Menu ();
return 0;
}
Thanks in advance for any help :-)
This post has been edited by lillywhite: 7 May, 2008 - 05:29 PM