Menu

Help with my Yahtzee game.

2009-04-27
2012-09-26
  • Albert Mendoza

    Albert Mendoza - 2009-04-27

    It does not show the score, I know its probably a mistake I made somewhere in there but cant seem to find it. I tried looking in the analyze count function where I thought the mistake was in but I think its correct.

    Here is the code:

    include<iostream>

    include<ctime>

    using namespace std;
    void yahtzee(void);
    void getAnswer(char&);
    void rollDice(int&,int&,int&,int&,int&);
    void displayDice(int,int,int,int,int);
    void keepDie(int&,int,int&,int&,int&,int&,int&,int&);
    void analyzeCount(int&,int&,int&,int,int,int,int,int,int);
    int main(void)
    {
    char again;
    srand(time(0));
    do
    {
    yahtzee();
    {

     cout&lt;&lt;&quot;Play another game (Y/N)? &quot;;
     getAnswer(again);}
     }while (again=='Y');
     system(&quot;pause&quot;);
     return 0;}
    

    void yahtzee(void)
    {
    int die1, die2,die3,die4,die5,c1=0,c2=0,c3=0,c4=0,c5=0,c6=0,high,c,score;
    system("cls");
    cout<<"Limited Edition Yahtzee\n\n";
    cout<<"To score points-get 3 or more of the same number on the five dice.\n\n";
    rollDice(die1,die2,die3,die4,die5);
    keepDie(die1,1,c1,c2,c3,c4,c5,c6);
    keepDie(die2,2,c1,c2,c3,c4,c5,c6);
    keepDie(die3,3,c1,c2,c3,c4,c5,c6);
    keepDie(die4,4,c1,c2,c3,c4,c5,c6);
    keepDie(die5,5,c1,c2,c3,c4,c5,c6);
    cout<<"\n\nThis is what you rolled.\n\n";
    displayDice(die1,die2,die3,die4,die5);
    analyzeCount(high, c, score, c1, c2, c3, c4, c5, c6);
    if (high>0)
    {
    cout<<"You rolled "<<high<<" "<<c<<"\'s for "<<score<<" points.\n\n";
    }
    else
    {cout<<"You had 0 points.\n\n";}
    }
    void getAnswer(char& ans)
    {
    do
    {
    cin>>ans;
    ans=toupper(ans);
    }
    while(ans!='Y'&&ans!='N');
    }
    void rollDice(int& die1,int& die2,int& die3,int& die4,int& die5)
    {
    die1=1+rand()%6;
    die2=1+rand()%6;
    die3=1+rand()%6;
    die4=1+rand()%6;
    die5=1+rand()%6;
    displayDice(die1,die2,die3,die4,die5);

    }
    void displayDice(int d1,int d2, int d3, int d4, int d5)
    {
    int z;
    for(z=1;z<=5;z++)
    {cout<<char(218)<<char(196)<<char(191)<<"\t";}
    cout<<endl;
    cout<<char(179)<<d1<<char(179)<<"\t";
    cout<<char(179)<<d2<<char(179)<<"\t";
    cout<<char(179)<<d3<<char(179)<<"\t";
    cout<<char(179)<<d4<<char(179)<<"\t";
    cout<<char(179)<<d5<<char(179)<<"\n";
    for(z=1;z<=5;z++)
    {cout<<char(192)<<char(196)<<char(217)<<"\t";}
    cout<<"\n\n";
    cout<<d1<<" "<<d2<<" "<<d3<<" "<<d4<<" "<<d5<<"\n\n";
    }
    void keepDie(int& die, int d, int& c1,int& c2, int& c3, int& c4, int& c5, int& c6)
    {
    char ans;
    cout<<"Keep die # "<<d<<" ("<<die<<") (Y/N)?";
    getAnswer(ans);
    if (ans=='N')
    {die=1+rand()%6;}
    switch(die)
    {
    case1:c1++;break;
    case2:c2++;break;
    case3:c3++;break;
    case4:c4++;break;
    case5:c5++;break;
    case6:c6++;break;
    }
    }
    void analyzeCount(int& high, int& c, int& score, int c1, int c2, int c3, int c4, int c5, int c6)
    {
    high=0;
    if (c1>2) {high=c1; c=1; }
    if (c2>2) {high=c2; c=2; }
    if (c3>2) {high=c3; c=3; }
    if (c4>2) {high=c4; c=4; }
    if (c5>2) {high=c5; c=5; }
    if (c6>2) {high=c6; c=6; }
    score=high*c;
    if (high==4) {score+=10;}
    if (high==5) {score+=50;}
    }

     
    • Albert Mendoza

      Albert Mendoza - 2009-04-27

      Never mind figured it out, amazing, apperentley if you dont put a space in between the case and the digit it wont work.

       
      • cpns

        cpns - 2009-04-27

        Ok you figured it out while I was answering. However the advice still stands. Get the compiler to help you by showing and fixing warnings.

        "case6:" is syntactically a target label for the 'goto' statment. That is why you need the space. But you should not be surprised, if you append an alphanumeric or underscore character to any reserved word, it is no longer a reserved word but an identifier.

        Clifford

         
    • cpns

      cpns - 2009-04-27

      You are asking a lot with all that code and so little detail about the problem. In fact the only practical course would be for someone to compile and run it to find out what it actually does.

      I did compile it (using MSCV++) and got the following warnings:

      (13) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
      (51) : warning C4244: '=' : conversion from 'int' to 'char', possible loss of data
      (69) : warning C4310: cast truncates constant value
      (69) : warning C4310: cast truncates constant value
      (69) : warning C4310: cast truncates constant value
      (71) : warning C4310: cast truncates constant value
      (71) : warning C4310: cast truncates constant value
      (72) : warning C4310: cast truncates constant value
      (72) : warning C4310: cast truncates constant value
      (73) : warning C4310: cast truncates constant value
      (73) : warning C4310: cast truncates constant value
      (74) : warning C4310: cast truncates constant value
      (74) : warning C4310: cast truncates constant value
      (75) : warning C4310: cast truncates constant value
      (75) : warning C4310: cast truncates constant value
      (77) : warning C4310: cast truncates constant value
      (77) : warning C4310: cast truncates constant value
      (77) : warning C4310: cast truncates constant value
      (96) : warning C4060: switch statement contains no 'case' or 'default' labels
      (95) : warning C4102: 'case6' : unreferenced label
      (94) : warning C4102: 'case5' : unreferenced label
      (93) : warning C4102: 'case4' : unreferenced label
      (92) : warning C4102: 'case3' : unreferenced label
      (91) : warning C4102: 'case2' : unreferenced label
      (90) : warning C4102: 'case1' : unreferenced label
      (90) : warning C4702: unreachable code
      (91) : warning C4702: unreachable code
      (92) : warning C4702: unreachable code
      (93) : warning C4702: unreachable code
      (94) : warning C4702: unreachable code
      (95) : warning C4702: unreachable code

      Code with that many warnings to be honest is almost certainly incorrect. Fix the warnings first

      The warnings listed for lines 80 to 96 are certainly errors. There must be a space between the keyword 'case' and the constant, otherwise it is just a 'goto' label and not a case statement at all.

      Using Dev-C++ set the following compiler options -Wall -Werror, you will not get the same warnings that MSVC++ emits, but you will be more likely to spot errors in your code. Warnings are errors; is you take that attitude when programming, the quality of your code will increase.

      for the cast warnings instead of say char(218) use a hex literal character constant like '\xda' instead. Then you'll have type agreement without the cast.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.