Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,557 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,927 people online right now. Registration is fast and FREE... Join Now!




Multi Player Yahtzee

 
Reply to this topicStart new topic

Multi Player Yahtzee

lillywhite
7 May, 2008 - 05:25 PM
Post #1

New D.I.C Head
*

Joined: 7 May, 2008
Posts: 3

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
User is offlineProfile CardPM
+Quote Post

KYA
RE: Multi Player Yahtzee
7 May, 2008 - 05:31 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
I would have an array for each player to keep track of their rolls and eventually their scores. When you say multilayer you mean multiple people playing on the same computer? If not then you need some Windows API thrown in like WinSock(), etc...
User is offlineProfile CardPM
+Quote Post

lillywhite
RE: Multi Player Yahtzee
7 May, 2008 - 05:35 PM
Post #3

New D.I.C Head
*

Joined: 7 May, 2008
Posts: 3

QUOTE(KYA @ 7 May, 2008 - 06:31 PM) *

I would have an array for each player to keep track of their rolls and eventually their scores. When you say multilayer you mean multiple people playing on the same computer? If not then you need some Windows API thrown in like WinSock(), etc...


From the same computer. I think I can get the arrays, I'm just not sure how to create a loop that will allow the players to alternate turns...
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Multi Player Yahtzee
7 May, 2008 - 05:44 PM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,465



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
A common thing to do is use an infinite loop, & test for exit results within the functions.
cpp

while(1) {
player_1();
player_2();
}

User is offlineProfile CardPM
+Quote Post

lillywhite
RE: Multi Player Yahtzee
7 May, 2008 - 05:50 PM
Post #5

New D.I.C Head
*

Joined: 7 May, 2008
Posts: 3

QUOTE(no2pencil @ 7 May, 2008 - 06:44 PM) *

A common thing to do is use an infinite loop, & test for exit results within the functions.
cpp

while(1) {
player_1();
player_2();
}



Hmmm... sounds good. I'll try it and see where I can get with it. Thanks :-)
User is offlineProfile CardPM
+Quote Post

KYA
RE: Multi Player Yahtzee
7 May, 2008 - 09:13 PM
Post #6

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
Offtopic: Infiniteloop is my gamer handle smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:26PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month