ee

[/code]
Last edited on
After adding #include <time.h> it worked for me.
Just tried it again and noticed it doesn't work every time. 2nd time it seemed to work. point shouldn't be 1 I think.

C:\Temp>test123
Player rolled 7
You win!
Point is 1
Player rolled5
Want to play again? Enter 1 for yes =>

C:\Temp>test123
Player rolled 5
Point is 5
You win!
Player rolled10
Want to play again? Enter 1 for yes =>
What you need the most of the times is to compare the result of a roll to the result of another roll.
That could be comfortably done by a recursive function.

The following code is distant from yours, so, if you don't find any hint in it, just ignore it:
[EDIT: added #include <ctime> , previously forgotten]
[II EDIT: added std:: before srand - many apologizes]

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Rules of craps:
// First roll: Player rolls 2 , 3, or 12, player loses and the game ends.
// Player rolls 7 or 11, player wins and the game ends.
// Player rolls 4, 5, 6, 8, 9, or 10, the number becomes the point number.
// Player will continue to roll and win only if the roll is the point number.
// Player will lose if roll is 7.

#include <cstdlib>
#include <ctime>
#include <iostream>

struct DieRoll {
    int die1;
    int die2;
    int roll;
};

// winOrLost: accepts integer to check for victory or 0 if not to check;
//            returns false if player looses, true if wins
bool winOrLost(int pointtoget = 0);
DieRoll rollDice();

int main ()
{
    std::srand(std::time(0));

    int ans = 1;
    do {
        if(winOrLost(0))
            std::cout << "You win!" << std::endl;
        else
            std::cout << "Sorry, you lose." << std::endl;
        std::cout << "\nDo you want to play again? Enter 1 for yes => ";
        std::cin >> ans;
        std::cout << std::endl;
    } while(ans==1);

    return 0;
}

bool winOrLost(int pointtoget)
{
    DieRoll currentroll = rollDice();

    std::cout << "die one: " << currentroll.die1
              << "; die two: " << currentroll.die2 << std::endl;

    // If pointtoget != 0, it means this is the second roll, to be
    // compared with a 4, 5, 6, 7, 8, 9, 10.
    // After comparisone, we can return true or false.
    // Otherwise (if pointtoget == 0), we need to check all the combinations
    if(pointtoget) { // if pointtoget != 0
        std::cout << " * you rolled " << currentroll.roll << std::endl;
        if(currentroll.roll == pointtoget) {
            return true;
        } else if (currentroll.roll == 7) {
            return false;
        } else {
            std::cout << "Roll again to get " << pointtoget << std::endl;
            return winOrLost(pointtoget);
        }
    } else {
        std::cout << " * First roll: you rolled " << currentroll.roll
                  << std::endl;
        switch (currentroll.roll) {
        case  2:
        case  3:
        case 12:
            return false;
            break;
        case  7:
        case 11:
            return true;
            break;
        case  4:
        case  5:
        case  6:
        case  8:
        case  9:
        case 10:
        {
            std::cout << "Roll again to get " << currentroll.roll << std::endl;
            return winOrLost(currentroll.roll);
        }
            break;
        default:
            break;
        }
    }

    // we should never get here!
    std::cout << "ALERT! Some unforeseen problem occurred." << std::endl;
    return false;
}

DieRoll rollDice()
{
    DieRoll tmp;
    tmp.die1 = rand() % 6 + 1;
    tmp.die2 = rand() % 6 + 1;
    tmp.roll = tmp.die1 + tmp.die2;

    return tmp;
}

Last edited on
Topic archived. No new replies allowed.