2
2
// main.cpp
3
3
// CrapsGame
4
4
//
5
+ //
5
6
// Created by Melinda Fernandes on 02/03/2017.
6
7
// Copyright © 2017 Melinda Fernandes. All rights reserved.
7
8
//
@@ -13,6 +14,10 @@ using std::endl;
13
14
#include < String>
14
15
using std::string;
15
16
17
+ #include < cstdlib>
18
+ using std::rand;
19
+ using std::srand;
20
+
16
21
#include < time.h>
17
22
using std::time;
18
23
@@ -24,42 +29,52 @@ int main(){
24
29
int sum = 0 ; // store returned sum from rollDice function
25
30
int point = 0 ; // store point when it has been made
26
31
string hasRolled = " Player has rolled " ; // roll statement to be printed after rolls
27
-
32
+
28
33
srand (time (0 )); // store seed from time
29
34
30
35
sum = rollDice ();
31
36
cout << hasRolled << sum << endl;
32
37
33
38
// determine game outcome
34
- if (sum == 7 || sum == 11 ){
35
- cout << " WIN" << endl;
36
- return 0 ; // terminate game
37
- }
38
- if (sum == 2 || sum == 3 || sum == 12 ){
39
- cout << " LOSE" << endl;
40
- return 0 ; // terminate game
41
- }
42
- if (sum == 4 || sum == 5 || sum == 6 || sum == 8 || sum == 9 || sum == 10 ){
43
- point = sum;
44
- cout << " Point made is " << point << endl;
45
-
46
- sum = rollDice ();
47
- while (sum != point){
48
- if (sum == 7 ){
49
- cout << hasRolled << sum << endl;
50
- cout << " LOSE" << endl;
51
- return 0 ; // terminate game
52
- }
53
- else
54
- sum = rollDice (); // to continue while loop
55
- } // end while
56
-
57
- // return "WIN" as while condition is not satisfied
58
- cout << hasRolled << sum << endl;
59
- cout << " Point has been made" << endl;
60
- cout << " WIN" << endl;
61
-
62
- }
39
+ switch (sum){
40
+ // player wins if
41
+ case 7 : // roll 7 on first throw
42
+ case 11 : // roll 11 on first throw
43
+ cout << " WIN" << endl;
44
+ return 0 ; // terminate game
45
+ // player loses if
46
+ case 2 : // roll 2 on first throw
47
+ case 3 : // etc.
48
+ case 12 :
49
+ cout << " LOSE" << endl;
50
+ return 0 ; // terminate game
51
+ // point is made and game continues if
52
+ default : // default cases cover sums 4, 5, 6, 8, 9, 10
53
+ point = sum; // point takes the value of whatever sum is
54
+ cout << " Point made is " << point << endl;
55
+
56
+ // continue game
57
+ sum = rollDice ();
58
+ while (sum != point){
59
+ switch (sum){
60
+ // lose if rolled 7 before point
61
+ case 7 :
62
+ cout << hasRolled << sum << endl;
63
+ cout << " LOSE" << endl;
64
+ return 0 ; // terminate game
65
+ // continue game and while loop if any other number is rolled
66
+ default :
67
+ sum = rollDice ();
68
+ }
69
+ } // point has been made, end while
70
+
71
+ // sum == point, return win
72
+ cout << hasRolled << sum << endl;
73
+ cout << " Point has been made" << endl;
74
+ cout << " WIN" << endl;
75
+
76
+ } // exit switch statement
77
+
63
78
64
79
return 0 ; // terminate game
65
80
}
0 commit comments