CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2013
    Posts
    3

    number of players

    i am writing a game that is 2 to 4 players. i am wondering how to ask how many players, and make the score stay with each player. it is a rip off of zombie dice, this is what i have so far the playerScore array is to hold the score for each player and the turn array is hold hold the values that they rolled that turn, i also dont know how to clear out the turn array when it goes to the next player.
    #include <iostream>
    #include <cstdlib>
    #include <cmath>
    #include <ctime>
    using namespace std;

    int main()
    {
    int playerScore[4];
    int turn[3];

    return 0;
    }

    void roll()
    {
    srand (time(0));
    int s;

    for (int x=1,x<4, x++)
    {
    s = rand()%13;

    if (s<6)
    {
    greenDice;
    }
    else if(s>5 && s<9)
    {
    yellowDice;
    }
    else if(s>8)
    {
    redDice;
    }
    }
    }
    void greenDice()
    {
    srand (time(0));
    int g;

    g = rand()%6;

    if (g<3)
    {
    fish;
    }
    else if(g>2 && g<5)
    {
    hook;
    }
    else if(g>4)
    {
    boot;
    }
    }
    void yellowDice()
    {
    srand (time(0));
    int y;

    y = rand()%6;

    if (y<2)
    {
    fish;
    }
    else if(y>1 && y<4)
    {
    hook;
    }
    else if(y>3)
    {
    boot;
    }
    }
    void redDice()
    {
    srand (time(0));
    int r;

    r = rand()%6;

    if (r<1)
    {
    fish;
    }
    else if(r>0 && r<3)
    {
    hook;
    }
    else if(r>2)
    {
    boot;
    }
    }
    int fish()
    {
    cout<< "You rolled a fish";
    ++ turn[1];
    }
    int hook()
    {
    cout << "You rolled a hook";
    ++ turn[2];
    }
    int boot()
    {
    cout <<"You rolled a boot.";
    ++ turn[3];
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: number of players

    Whn posting code, please format properly before posting and use code tags (Go advanced, select code, click '#'). Your code as posted is just about unreadable.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: number of players

    As 2kaud said, you'll get more help when your code is properly formatted and readable.

    Just at a quick glance, this is not how you call a function.
    boot;

    Also, you should only call srand once in your program.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: number of players

    Quote Originally Posted by dthunderchunky View Post
    and make the score stay with each player.
    The quick suggestion to you is to learn what a struct or class is before going further.

    If you want to keep like items together, then the facility to do that is to use struct/class. Right now, your code has variables for different things, but the issue is that all these variables are basically "loose" -- there is no cohesion between them. A struct allows you to group several items under one aggregation.
    Code:
    struct PlayerInfo
    {
        std::string playerName;
        int playerScore;
        //...
    };
    So for example, in the code above, I have a PlayerInfo struct that holds both the player's name and the player's score.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured