0

its a text based monopoly game where i need the dice to select the number from the array like on a board. I have the number generator, what i need to do though is when the value comes up it pluses it on the array to get the matching number so for example if the players rolls a 6, the 6 + array 0 = array value 6 which will be a name of a street but it means the player knows which place on the made up board they are on. here is the coding i am using to try and do so but i keep on getting 006ff65 what ever. i how can i get it for showing just the number as the names will be added later.

{
    int main()
    {
        int number = 12;
        int rnum = (rand() % number) + 1;
        int house = 1;
        int moneyscore = 10000;
        double values[] = { 
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40 };





        char name[50];
        cout << "Who are you, Dog, Car, Hat or Bus" << endl;
        cin.getline(name, 50);
        cout << "Welcome to Our Game " << name << " You have " << moneyscore << " .PLease Roll dice to get started" <<  endl;
        cout << "\n----------------------Press any Enter to roll dice----------------------" << endl;


        system("cls");
        int choiceOne_Path;
        cout << "# You roll a " << rnum << endl;
        rnum = values[rnum];
        cout << "# you have " << moneyscore << endl;
        cout << "# You move to grid "<< values << endl;
        cout << "\t >> Enter '1' Buy Property" << endl;
        cout << "\t >> Enter '2' Recieve Rent" << endl;
        cout << "\t >> Enter '3' End turn" << endl;
    retry:
        cout << "\nEnter your choice: ";
        cin >> choiceOne_Path;
        if (choiceOne_Path == 1)
        {
            cout << "\n Buy Property " << endl;
            cout << "  " << name << " has " << moneyscore << endl;
            cout << "  " << house <<" House has been placed by " << name <<" who spent 2,500" << endl;
            moneyscore -= 2500;
            cout << "  " << name << " now has " << moneyscore << endl;
            cout << "\n Roll again" << endl;
            cout << "# You roll a " << rnum << endl;


        }
        else if (choiceOne_Path == 2)
        {
            cout << "\n You recieved 2500 from rent" << endl;
            moneyscore += 2500;
            cout << "  " << name << "\n now has" << moneyscore << endl;
            cout << "\n(Player will gain money form house, will need to find a way in order to make the 
            console remember what score == to postion)" << endl;
            cout << "Ends turn" << endl;

        }
        else if (choiceOne_Path == 3)
        {
            cout << "\n Roll again" << endl;
            cout << "# You roll a " << rnum << endl;
        }
        else
        {
            cout << "You are doing it wrong, player! Press either '1' or '2', nothing else!" << endl;
            goto retry;
        }

        cout << "\n----------------------Press any key to continue----------------------" << endl;
        _getch();
    }

}
Antonio B
  • 9
  • 1
  • 1
    I once had programmed a dice game, and what I'd done was that it gets the system time and extracts the milliseconds part. I'd then seeded the random number generator in C using that number/10 – kesarling He-Him Apr 29 '20 at 15:35
  • The thing is, the random number generator in C is broken (not exactly) and is not as good as the random number generators in other languages – kesarling He-Him Apr 29 '20 at 15:36
  • https://stackoverflow.com/questions/2242963/get-the-current-time-in-seconds Try this link – kesarling He-Him Apr 29 '20 at 15:36
  • 1
    Also check out https://www.geeksforgeeks.org/rand-and-srand-in-ccpp/ and https://stackoverflow.com/questions/19665818/generate-random-numbers-using-c11-random-library?rq=1 – kesarling He-Him Apr 29 '20 at 15:37
  • https://stackoverflow.com/q/19665818/11829849 – xorover Apr 29 '20 at 15:56
  • Does this answer your question? [Generate random numbers using C++11 random library](https://stackoverflow.com/questions/19665818/generate-random-numbers-using-c11-random-library) – xorover Apr 29 '20 at 15:57
  • 1
    Wait. Is this a question about generating a random dice roll (RNG question) or what to do with the resulting dice roll value once you get it? It sounds like the latter, but all the answers are addressing the former. – rebusB Apr 29 '20 at 16:12
  • So Antonio... any luck solving your problem? Let us know! – rebusB May 06 '20 at 19:27

2 Answers2

0

As far as I know, you should use srand (time(NULL)); between every call to rand() to correctly return a new random number from every call. "srand" initialize the random number generator using a seed. In this case seed is time, which should be different on every call.

SunPaz
  • 145
  • 7
  • "I have the number generator, what i need to do though is when the value comes up it pluses it on the array to get the matching number " So... shouldn't this be a comment on nice RNG practice since the question is about array offsets? – rebusB Apr 29 '20 at 16:14
  • And you do not need to seed the RNG every call. Just once at the start of the program to prevent the same stream of random numbers from occurring every time it runs. – rebusB Apr 29 '20 at 16:46
  • @rebusB You are right, I just read that. Thanks for the info. – SunPaz Apr 29 '20 at 16:57
0

Pretty basic. You either made a few typos or need to learn how arrays work (and program flow, and subroutines, but perhaps that is for later lessons.)

First you are assigning the result of the array lookup back into your random number: rnum = values[rnum]; which is not a big deal except you use that variable later and it no longer contains what you may think it does. It actually contains the value you are looking for!

Second the variable values is a pointer to the head of your array so you are outputting the address of the values array with this line: cout << "# You move to grid "<< values << endl; there is no array look up happening at all here. It is strange you missed that because you did reference the array contents properly when you replaced the random number value earlier.

rebusB
  • 518
  • 5
  • 19
  • But looking at the logic of the rest of your code these may be the least of you problems. What happens when the goto retry; is called? Do you get a new dice roll (no) and will that dice roll be applied to the players new position from the last roll (no). – rebusB Apr 29 '20 at 20:52