d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Dice Game! (cho-han)
Prev12
Add Reply New Topic New Poll
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Sep 10 2013 08:53pm
quick bug report, maybe it'll help:

Code

         if (option[0]!='p')
              {
                  for (int i=1;i<n;i++)
                      {


Loop starts at 1, win[0] is never calculated, thus never changes.

Code

                  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);


loop ends at n-1, however arrays are accessed to indexes up to i+1, going from 1 to n. In an array of N elements, indexes span from 0 to n-1, index n doesn't exist. You have a case of buffer overload, this can result in data corruption and crashes when you attempt to pause the game.
Sep 11 2013 09:13am
Inappropriate Post Content
Member
Posts: 950
Joined: Feb 19 2009
Gold: 2,595.00
Sep 12 2013 08:13am
so as of now my program is working except for eliminating players after they get below $50... it will say they were eliminated, and not show up their money for the next turn but then they show up again the following turn and then the turn after that, the game ends... i'm confused and any help is appreciated :)

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

using namespace std;

int main (void)
{
   int n =0, x=1,y=0;
   srand(time(NULL));
   int lost=1;
   cout << "How many total players will be playing?\nPlease choose between 2-9 players:\n";
   while (1)
   {
       if (cin >> 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";
           }
       }
       else
       {
           cout << "Not a valid input, try again..." << endl;
           cin.clear();
           while (cin.get() != '\n'); //keeps the program from going on forever, empty loop
       }
   }
   double money[n];    //using double because money has two decimals

   for (int i=0;i<n;i++)
       {
           money[i] = 500.00;      //sets all the players money to 500.00
       }
   char option[n];     //this will be the choice players will make
   int win[n];         //variable to hold who is winning
   int losers[n];
   int counter;
   for (int i=0;i<n;i++)
       losers[i] == 0;
   string answer;  //this will be the answer the user gives


   while (x)
   {
       counter++;
       loop:
       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';
       }
       else
       {
           cout << "\nYou did not insert the correct word, try again please.\n\n";
           goto loop;
       }
       option[0]=comp;

       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<n;i++)
       {
           int num = ((rand()%2)+1);
           switch(num)
           {
               case 1:
               {
                   if (losers[i] == 0)
                       option[i+1] = 'o';  //add 1 because these are for the computer's choices
                   else
                       option[i+1] = 'l';
               } break;
               case 2:
               {
                   if (losers[i] == 0)
                       option[i+1] = 'e';
                   else
                       option[i+1] = 'l';
               } break;
           }
       }
       for (int i=0;i<n;i++)
           if (losers[i] == 0)
               money[i]-=50;
       if (option[0]=='p')
       {
           cout <<"\nYou have chosen to pass your turn, therefore ending the game.\nYour final money is $" << setprecision (5) << money[0] << ".\nGoodbye!\n";
           return 0;
       }
       if (option[0]!='p')     //makes sure user isn't passing
       {
           for (int i=0;i<n;i++)
           {
               if (losers[i] == 0)
               switch (option[i])  //checks everyone's choice to see if they picked even or odd
               {
                   case 'e':
                   {
                       if (add_dice%2 ==0)     //if even, the remainder will be 0
                           win[i]=1;           //sets value of win[i] to 1 for corresponding player
                       else
                           win[i]=0;           //sets value of win[i] to 0 for corresponding player
                   }; break;
                   case 'o' :
                   {
                       if (add_dice%2 == 1)    //if odd, it will have a remainder of 1
                           win[i]=1;
                       else
                           win[i]=0;
                   }; break;
               }
           }
           double total =0;        //the total money in the pot
           for (int i=0;i<n;i++)
               if (losers[i] == 0)     //if the player hasn't been eliminated (same concept as win[i]), it will add 50 per player not eliminated
                   total = total +50;
           int winners=0;
           for (int i=0;i<n;i++)
              if ((win[i]==1) && (losers[i]==0))
                   winners++;
           for (int i=0;i<n;i++)
               if ((win[i]==1) && (losers[i]==0))
                   money[i] += (total/winners);
           }
           cout << "\nDie rolls were " << dice1 << " and " << dice2 << ".\nThese players won:\n";
           if (option[0]!='p')
               for(int i=0;i<n;i++)
                   if ((win[i]==1) && (losers[i]==0))
                       cout << "Player: " << i << " \n";

           cout << "\nThese are the standings for the players:\n";
           for (int i=0;i<n;i++)
           {
               if (losers[i] == 0)
                   cout << "Player " << i << ": $" << setprecision (5) <<  money[i] << "\n";
           }
           for (int i=0;i<n;i++)
           {
               if ((money[i] < 50) && losers[i] == 0)
               {
                   cout << "\nPlayer " << i << " has been eliminated!\n";
                   losers[i] = 1;
                   lost++;
                   if (money[0] < 50)
                   {
                       cout << "\n\nYou do not have money, you lose!\nGood-bye!\nThese are the final standings:\n";
                       for (int i=0;i<n;i++)
                       {
                           cout << "Player " << i << " ended with $" << setprecision (5) <<  money[i] << "\n";
                       }
                       return 0;
                   }
                   if (lost == (n+1))
                   {
                   cout << "There are no more players.\n";
                   cout << "You have won!! Congratulations!\nYour final money is $" << setprecision (5) << money[0] << ".\n";
                   return 0;
                   }
               }
               else
                   losers[i] = 0;
           }
   }
   return 0;
}
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Sep 12 2013 08:53am
Code
              if ((money[i] < 50) && losers[i] == 0)
              {
                  cout << "\nPlayer " << i << " has been eliminated!\n";
                  losers[i] = 1;
                  lost++;
                  if (money[0] < 50)
                  {
                      cout << "\n\nYou do not have money, you lose!\nGood-bye!\nThese are the final standings:\n";
                      for (int i=0;i<n;i++)
                      {
                          cout << "Player " << i << " ended with $" << setprecision (5) <<  money[i] << "\n";
                      }
                      return 0;
                  }
                  if (lost == (n+1))
                  {
                  cout << "There are no more players.\n";
                  cout << "You have won!! Congratulations!\nYour final money is $" << setprecision (5) << money[0] << ".\n";
                  return 0;
                  }
              }
              else
                  losers[i] = 0;


Messed up if else statement. A player at <$50 and loser==0 would pass the if conditions, and have it's loser set to 1. Next pass it would fail the if statement and move to the else statement, which sets losers to 0 again. Next pass the if would trigger again, set losers at 1 and so on.

I'm not sure what you are trying to achieve with the else statement, so this fix may not respect that intention.

Code

if(money[i] < 50)
{
    if(!losers[i])
    {
          //your cout here
    }

    losers[i] = 1;
}
else losers[i] = 0;

Member
Posts: 950
Joined: Feb 19 2009
Gold: 2,595.00
Sep 12 2013 10:04am
Quote (flyinggoat @ Sep 12 2013 09:53am)
Code
if ((money[i] < 50) && losers[i] == 0)
              {
                  cout << "\nPlayer " << i << " has been eliminated!\n";
                  losers[i] = 1;
                  lost++;
                  if (money[0] < 50)
                  {
                      cout << "\n\nYou do not have money, you lose!\nGood-bye!\nThese are the final standings:\n";
                      for (int i=0;i<n;i++)
                      {
                          cout << "Player " << i << " ended with $" << setprecision (5) <<  money[i] << "\n";
                      }
                      return 0;
                  }
                  if (lost == (n+1))
                  {
                  cout << "There are no more players.\n";
                  cout << "You have won!! Congratulations!\nYour final money is $" << setprecision (5) << money[0] << ".\n";
                  return 0;
                  }
              }
              else
                  losers[i] = 0;


Messed up if else statement. A player at <$50 and loser==0 would pass the if conditions, and have it's loser set to 1. Next pass it would fail the if statement and move to the else statement, which sets losers to 0 again. Next pass the if would trigger again, set losers at 1 and so on.

I'm not sure what you are trying to achieve with the else statement, so this fix may not respect that intention.

Code
if(money[i] < 50)
{
    if(!losers[i])
    {
          //your cout here
    }

    losers[i] = 1;
}
else losers[i] = 0;



yeah i had a bunch of wrong if statements with bad assignments.. but now i have it fully working.. thanks for help kind sir :)
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll