Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am to make a Yahtzee program for at least one playe rwith all rules apllied.

3 rolls per turn ,five dice used and player choice of what dice to hold and/ or r-roll. And a display of scores on a scorecard
this is to be a console application.
I have this copied code but it wont work. Personally I would like something easier to deal with. Can anyone help?
here is what I found so far just can't seem to merge them together to make it work.
C++
#include <iostream>
 #include <cstdlib>
 #include <time.h>
 using namespace std;
    int main() 
 {    
srand(static_cast<unsigned int="">(time (0)));   // Seeds the random number I would like to have a random roll on the five dice
int RandomValue =rand() %6;	
  int nums[5]; int i, j, input; int count[6]; 

 int sum = 0;  

         int num = 5;  

         int roll[5] = {0};  

         int count[6] = {0};  

         int score[13] = {0};  
                

                              
         cout<<"Please enter 5 numbers"<<endl;     // I Would like to put in a random 5 dice roll here         srand(static_cast<unsigned int="">(time (0))); 
 
                 for (int i=0; i<5; i++)  

                         cin>>roll[i];  

                            

                 for (int i=0; i<5; i++)  

                         sum += roll[i];    
                 

                switch(roll[5])  

                {  
                        case 1: ++count[0]; break;  
                         case 2: ++count[1]; break;  

                         case 3: ++count[2]; break;  

                         case 4: ++count[3]; break;  

                        case 5: ++count[4]; break;  

                        case 6: ++count[5]; break;  

 }  

         /*for (int i = 0; i < num;  // These following algorithms are incorrect is there an easier way?

         i++)  

                    

         {              if (roll[i] == 1)     

                         count[0]++;  
                        

                         if (roll[i] == 2)  

                        count[1]++;  

    count[2]++;  

                    

                         if (roll[i] == 4)  

                         count[3]++;  

                   

                        if (roll[i] == 5)  

                         count[4]++;  


                         if (roll[i] == 6)  
                        count[5]++;  

         }  
              */ 

                score[0] = count[0]*1;  

                score[1] = count[1]*2;  

                score[2] = count[2]*3;  

                score[3] = count[3]*4;  

                score[4] = count[4]*5;  

                score[5] = count[5]*6;  

           

               for (int i = 0; i <5; i++)  

         {              if (count[i]/3 >= 1)  

                        score[6] = sum;  

        }  

                 for (int i = 0; i <5; i++)  

        {              if (count[i]/4 >= 1)  

                        score[7] = sum;    

         }  

                 for (int i = 0; i <5; i++)  

        {              if (count[i]/3 >=1 && count[i]/2 >=1)  

                         score[8] = 25;  

         }                

                 for (int i = 0; i <5; i++)  

         {  

                 if ((count[0]*count[1]*count[2]*count[3]) == 1 ||  

                        (count[1]*count[2]*count[3]*count[4]) == 1 ||  

                         (count[2]*count[3]*count[4]*count[5]) == 1)  

                         score[9] = 30;  

        }  

         {      if ((count[0]*count[1]*count[2]*count[3]*count[4]) == 1 ||  

                        (count[1]*count[2]*count[3]*count[4]*count[5]) == 1)  

                         score[10] = 40;  

         }  

                 for (int i = 0; i < 5; i++)  

         {              if (count[i]/5 >= 1)  

                        score[11] = 50;  

       }  

                 score[12] = sum;  

                           

         cout<<setw(30)<<"******************************"<<endl;  

         cout<<"Ace's"<<setw(14)<<score[0]<<endl;  

         cout<<"Two's"<<setw(14)<<score[1]<<endl;  

         cout<<"Three's"<<setw(12)<<score[2]<<endl;  

         cout<<"Four's"<<setw(13)<<score[3]<<endl;  

         cout<<"Five's"<<setw(13)<<score[4]<<endl;  

         cout<<"Six's"<<setw(14)<<score[5]<<endl;  

         cout<<"Three of a Kind"<<setw(4)<<score[6]<<endl;  

         cout<<"Four of a Kind"<<setw(5)<<score[7]<<endl;  

         cout<<"Full House"<<setw(9)<<score[8]<<endl;  

         cout<<"Small Straight"<<setw(5)<<score[9]<<endl;  

         cout<<"Large Straight"<<setw(5)<<score[10]<<endl;  

         cout<<"YAHTZEE"<<setw(12)<<score[11]<<endl;  

         cout<<"Chance"<<setw(13)<<score[12]<<endl;  

         cout<<setw(30)<<"******************************"<<endl;  
           

        
} 
      
          for(j= 0; j < 14; j++) 
         
          for(i = 0; i < 5; i++) 
    {        
 nums[i] = rand()37;6 + 1;					// sets number of rolls to 37
 {
         cout << nums[i] << endl;
         switch(nums[i]);
        {        
             case 1: ++count[0]; break;
             case 2: ++count[1]; break;
             case 3: ++count[2]; break; 
             case 4: ++count[3]; break;  
             case 5: ++count[4]; break;
             case 6: ++count[5]; break;
             default: cout << "An error has occured!"; break;    // screen print if error occurs
        } 
        cout << "Choose which numbers you want to keep?( -1 to keep all numbers ) \n";   // user input which die to hold 
                 "\n";     
      cin >> input;
 }
           if(input == -1) 
         break;    
           if (input > 6		  
	 cout << "Please select number from 1 to 6 only \n\n";     
}           
  if()
 system("PAUSE");          
 return 0;  
}
Posted
Updated 17-Nov-11 8:42am
v3
Comments
Albert Holguin 17-Nov-11 14:25pm    
"but it won't work" is not an accurate description of a problem. Please specify your question.
bobschmitz 17-Nov-11 14:27pm    
oh i am to have this project completed by Sunday Nov. 20/2011. Any help will be useful Thank you !!! moreline2000@hotmail.com
Sergey Alexandrovich Kryukov 17-Nov-11 14:32pm    
This is not what Albert mean by the "problem". What is the problem in the code?
--SA
Albert Holguin 17-Nov-11 15:38pm    
As a few have already told you, your deadlines are meaningless to us. You can't post random code with no question and expect us to fix it. What's your actual question? Be as specific as possible.
Sander Rossel 17-Nov-11 14:42pm    
How about not copying, but doing your OWN homework?

1 solution

I can see only one problem: the code is written by a non-programmer.

No, no, please don't get offended: I'm not going to discourage you and suggest to quite. Just the opposite: I want to encourage you to become a programmer. This is first thing you need, but it will require you to change your thinking dramatically.

Sorry, but I cannot help you to solve the problem you want to solve and meet deadline. Remember that a deadline is your problem, not ours. And you did not formulate your problem; and this is another sign of a non-programmer. You need to change yourself first until it's too late.

Let's see:

C#
switch(roll[5])   
{  
   case 1: ++count[0]; break;  
   case 2: ++count[1]; break;   
   case 3: ++count[2]; break;   
   case 4: ++count[3]; break;  
   case 5: ++count[4]; break;   
   case 6: ++count[5]; break;  
}


A programmer will never write anything like that, not even roll[5], because immediate constant 5 is not supportable. It will be something like
C#
++count[roll[maxRollNumber] - 1];


Same thing about
C#
score[0] = count[0]*1;  
score[1] = count[1]*2;
//...
// how many times do you have to write this line to start thinking instead?


Must be something like
C#
for (int index = 0; index < maxRollNumber; ++index)
    score[index] *= 2;
And so on… What you have written is something opposite to the very idea of programming. And don't even play with the idea that you need to pass the test first and get a working program, fix things letter. In certain principle things, there is no later fix. Never.

And, please, one warning before you decide to reply: please, don't repeat a common mistake, don't say "I'm a beginner". Just don't. We know, so what? This is completely irrelevant.

As Yoda wrote:
Unlearn what you have learned.
—SA
 
Share this answer
 
v3
Comments
Richard MacCutchan 17-Nov-11 15:08pm    
+5 for good advice, and not just for this poster.
Sergey Alexandrovich Kryukov 17-Nov-11 16:02pm    
Thank you, Richard.
--SA
Albert Holguin 17-Nov-11 15:39pm    
I like the quote... can be VERY applicable to a lot of people.
Sergey Alexandrovich Kryukov 17-Nov-11 16:02pm    
Oh, yes!
Thank you, Albert.
--SA
TRK3 17-Nov-11 16:11pm    
Nice reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900