d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Dice Game! (cho-han)
12Next
Add Reply New Topic New Poll
Member
Posts: 950
Joined: Feb 19 2009
Gold: 2,595.00
Sep 10 2013 07:01pm
The game uses two standard six-sided dice, which are shaken in a bamboo cup or bowl by a dealer. The cup is then overturned onto the floor. Players then place their wagers on whether the sum total of numbers showing on the two dice will be "Chō" (even) or "Han" (odd). The dealer then removes the cup, displaying the dice. The winners collect their money. The game was a mainstay of the bakuto, itinerant gamblers in old Japan, and is still played by the modern yakuza. In a traditional Chou-Han setting, players sit on a tatami floor. The dealer sits in the formal seiza position and is often shirtless (to prevent accusations of cheating), exposing his elaborate tattoos.
Your code must:
Ask for two to nine additional computer players until a number of players between 2 and 9 are received.
The human player (you) starts with $500.
Each bet is $50.
Each turn the human player must be asked to call Cho (even) or Han (odd) or Pass before the dice are rolled. He leaves the game if he passes. The human player must leave if he has less than $50. You can assume the player will enter only lower case, (cho,han,pass) or will lose that round.
The computer players will randomly pick Cho (even) or Han (odd).
Display the numbers on the two die, whether Cho (even) or Han (odd), whether the human player won and the total number of computer players that won, and the human players current winnings (additional amount over the initial $500)
If the human player losses subtract $50 from his current total.
If the human player wins, the total amount bet is evenly divided amongst the winning calls, and that amount is added to his current total.

this is what i have so far.

Code
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main (void)
{
   int n =0, x=1,y=0;
   cout << "How many players will be playing?\nPlease choose between 2-9 players:\n";
   cin >> n;
   while(n)
       {
           if ((n>=2) && (n<=9))
           {
               cout << "There will be " << n << " players playing.\n";
               break;
           }
           else
               {
                   cout << "Please insert a number between 2-9.\n";
                   cin >> n;
               }

       }

   int p = n+1;    //set p (players) to n+1 because it goes 0-n
   double money[p];    //using double because money has two decimals

   for (int i=0;i<p;i++)
       {
           money[i] = 500.00;      //sets all the players money to 500.00
       }
   char option[p];     //this will be the choice players will make
   int win[p];         //the money players will be winning
   int pass=0;
   string answer;  //this will be the answer the user gives

   do
       {
           cout << "Do you want to bet on odd or even?\nIf you want to pass, type pass but this will quit your game.\n";
           cin >> answer;
           char comp;      //this will be set to make it easier for comparison
           if (answer.compare("even")==0)
               comp = 'e';
           else if (answer.compare("odd")==0)
               comp = 'o';
           else if (answer.compare("pass")==0)
               comp = 'p';

           option[0]=comp;

           srand(time(NULL));      //uses the clock of the computer to generate a random number every time
           int dice1 = ((rand()%6) +1);       //adding 1 because 0-5 and we need 1-6
           int dice2 = ((rand()%6) +1);     //these are the 2 random dice rolls
           int add_dice = dice1 + dice2;

           for (int i=0;i<p;i++)
           {
               cout << "Player " << i << " has $" << money[i] << ".\n";
           }

           for (int i=0;i<p;i++)
               {
                   srand(time(NULL));
                   int num = ((rand()%2)+1);
                   switch(num)
                       {
                           case 1:
                               {
                                   option[i+1] = 'o';  //add 1 because these are for the computer's choices
                                   cout << "\nodd\n";
                               } //break;
                           case 2:
                               {
                                   option[i+1] = 'e';
                                   cout << "\neven\n";
                               } //break;
                       }
               }
           if (option[0]!='p')
               for (int i=0;i<p;i++)
                   money[i]-=50;
           else
               {
                   cout <<"You have chosen to pass your turn, therefore ending the game.\nYour final money is " << money[0] << ".\nGoodbye!";
                   return 0;
               }
           if (option[0]!='p')
               {
                   for (int i=1;i<p;i++)
                       {
                           switch (option[i])
                               {
                                   case 'e':
                                       {
                                           if (add_dice%2 ==0)
                                               win[i]=1;
                                           else
                                               win[i]=0;
                                       }; break;
                                   case 'o' :
                                       {
                                           if (add_dice%2 ==1)
                                               win[i]=1;
                                           else
                                               win[i]=0;
                                       }; break;
                               }
                       }
                   float total = 50+ n*50;
                   int winners=0;
                   for (int i=0;i<p;i++)
                       if (win[i]==1)
                           winners++;
                   for (int i=0;i<p;i++)
                       if (win[i]==1)
                           money[i] += (total/winners);
               }
           else
               {
                   for (int i=0;i<n;i++)
                       {
                           switch (option[i+1])
                               {
                                   case 'e':
                                       {
                                           if (add_dice%2==0)
                                               win[i+1]=1;
                                           else
                                               win[i+1]=0;
                                       } break;
                                   case 'o':
                                       {
                                           if (add_dice%2==1)
                                               win[i+1]=1;
                                           else
                                               win[i+1]=0;
                                       } break;
                               }
                       }
                   float total = n*50;
                   int winners = 0;
                   for (int i=0;i<n;i++)
                       if (win[i+1]==1)
                           winners++;
                   for (int i=0;i<n;i++)
                       if (win[i+1]==1)
                           money[i+1]+=(total/winners);
               }
           cout << "Die rolls were " << dice1 << " and " << dice2 << ".\nThese players won:\n";
           if (option[0]!='p')
               for(int i=0;i<p;i++)
                   if (win[i]==1)
                       cout << i << " \n";
           cout << "Your total is $" << money[0] << ".\n";
           if (money[0] < 50)
               {
                   cout << "\n\nYou have run out of money, you lose!\nGood-bye!\nThese are the final standings:\n";
                   for (int i=0;i<p;i++)
                       {
                           cout << "Player " << i << " has $" << money[i] << ".\n";
                       }
                   return 0;
               }
   } while (x);

return 0;
}


some issues i have:

if you input anything other then numbers in the first cin, it breaks -.-
at the end, all the computer players have the same amount of money
sometimes when betting, the choice of the computer (i have them couting them what their randomizing into)

any help is appreciated, thanks!
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Sep 10 2013 07:39pm
1st issue:
You're using an integer to receive the input to the cin stream. You should use a string, and then convert it to an integer.

2nd issue:
Code

   srand(time(NULL));
   int num = ((rand()%2)+1);


Your loop reseeds the pseudo random number generator at every iteration. All of your iterations occur faster than the increase of a single unit of time returned but time(NULL) (milliseconds I assume). This means your PRNG is reseted to the same value for each step of the loop, resulting in the exact same rand() value for each of the bots.

You should perform a single call to srand() before the loop and then only call rand() from within the loop.

3rd issue: I don't understand the question o.o



Member
Posts: 950
Joined: Feb 19 2009
Gold: 2,595.00
Sep 10 2013 07:49pm
Quote (flyinggoat @ Sep 10 2013 08:39pm)
1st issue:
You're using an integer to receive the input to the cin stream. You should use a string, and then convert it to an integer.

2nd issue:
Code
srand(time(NULL));
   int num = ((rand()%2)+1);


Your loop reseeds the pseudo random number generator at every iteration. All of your iterations occur faster than the increase of a single unit of time returned but time(NULL) (milliseconds I assume). This means your PRNG is reseted to the same value for each step of the loop, resulting in the exact same rand() value for each of the bots.

You should perform a single call to srand() before the loop and then only call rand() from within the loop.

3rd issue: I don't understand the question o.o


oh i forgot to finish the question haha my head hurts.

i meant to say "sometimes when betting, the choice of the computer (i have them couting them what their randomizing into) shows up twice and sometimes only once"
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Sep 10 2013 07:59pm
Quote (falco37 @ Sep 11 2013 03:49am)
oh i forgot to finish the question haha my head hurts.

i meant to say "sometimes when betting, the choice of the computer (i have them couting them what their randomizing into) shows up twice and sometimes only once"


You have commented the break statements in the first switch case scope. Uncomment the first break and the issue should go away.

This post was edited by flyinggoat on Sep 10 2013 08:00pm
Member
Posts: 950
Joined: Feb 19 2009
Gold: 2,595.00
Sep 10 2013 08:10pm
Quote (flyinggoat @ Sep 10 2013 08:59pm)
You have commented the break statements in the first switch case scope. Uncomment the first break and the issue should go away.


only the first one? and i changed the srand but still the same...

Code
srand(time(NULL));
           for (int i=0;i<n;i++)
               {
                   int num = ((rand()%2)+1);
                   switch(num)
                       {
                           case 1:
                               {
                                   option[i+1] = 'o';  //add 1 because these are for the computer's choices
                                   cout << "\nodd\n";
                               } break;
                           case 2:
                               {
                                   option[i+1] = 'e';
                                   cout << "\neven\n";
                               } break;
                       }
               }


also if i do srand all the way and the top, will that randomize everything that uses rand in the program? sorry unsure at exactly how this works

This post was edited by falco37 on Sep 10 2013 08:12pm
Sep 10 2013 08:23pm
Inappropriate Post Content
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Sep 10 2013 08:33pm
Quote
only the first one? and i changed the srand but still the same...

There is no more cases past 2, the break statement is redundant. A switch/case statement isn't the equivalent of an if/else statement. It will simply go to the related label and run all the code within the switch scope unless explicitly ordered otherwise.

Simply put, if num is 1 switch will jump to case 1:, execute the code, exit the case 1: scope and proceed to the next line of code until it exits the switch scope. In this case, it'll simply move to case 2: and execute the code there. If num is 2, it'll go to label 2:, run the code, get out of the label scope, and step out of switch's scope, as no more code is available within it past that point. The break after label 1: scope is there to signify that it should exit the switch scope after processing it.

Even though 2 bugs where taken away that forced all bot bets to be the same, the bug still remains, simply implying more bugs are left in the code.

Quote
also if i do srand all the way and the top, will that randomize everything that uses rand in the program? sorry unsure at exactly how this works


srand() seeds rand(). Everytime you call srand(), you reset rand() internal state. Let's say you seed the PRNG using srand() with value N, then call rand() M times, you'll have M pseudo random values returned. If you then reseed the PRNG with same value N, calling again srand(N), then call rand(), the first M values yielded will be the exact same ones as the previous batch.

It is proper practice to reseed you PRNG before a loop, and bad practice to reseed it for each iteration of said loop.
Member
Posts: 950
Joined: Feb 19 2009
Gold: 2,595.00
Sep 10 2013 08:38pm
Quote (flyinggoat @ Sep 10 2013 09:33pm)
There is no more cases past 2, the break statement is redundant. A switch/case statement isn't the equivalent of an if/else statement. It will simply go to the related label and run all the code within the switch scope unless explicitly ordered otherwise.

Simply put, if num is 1 switch will jump to case 1:, execute the code, exit the case 1: scope and proceed to the next line of code until it exits the switch scope. In this case, it'll simply move to case 2: and execute the code there. If num is 2, it'll go to label 2:, run the code, get out of the label scope, and step out of switch's scope, as no more code is available within it past that point. The break after label 1: scope is there to signify that it should exit the switch scope after processing it.

Even though 2 bugs where taken away that forced all bot bets to be the same, the bug still remains, simply implying more bugs are left in the code.



srand() seeds rand(). Everytime you call srand(), you reset rand() internal state. Let's say you seed the PRNG using srand() with value N, then call rand() M times, you'll have M pseudo random values returned. If you then reseed the PRNG with same value N, calling again srand(N), then call rand(), the first M values yielded will be the exact same ones as the previous batch.

It is proper practice to reseed you PRNG before a loop, and bad practice to reseed it for each iteration of said loop.


ahhh i see that makes a lot of sense.. thanks so much for all your help so far bro, i greatly appreciate it.

but yeah my new error is that whatever i get first will continue on.
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Sep 10 2013 08:44pm
Quote
a bit confused on how to convert string to integer


Code

string str;
cin >> str;
int val = atoi(str.c_str());


As for the latest bug, it's getting late and I'm too lazy right =P
Member
Posts: 950
Joined: Feb 19 2009
Gold: 2,595.00
Sep 10 2013 08:45pm
Quote (flyinggoat @ Sep 10 2013 09:44pm)
Code
string str;
cin >> str;
int val = atoi(str.c_str());


As for the latest bug, it's getting late and I'm too lazy right =P


haha its ok bro i appreciate the help.. if tomorrow you decide to help some more, i will post updates on if i find out what is wrong.

again thanks alot for the help :)
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll