Skip to content

Commit 56e70de

Browse files
Melinda FernandesMelinda Fernandes
Melinda Fernandes
authored and
Melinda Fernandes
committed
using switch statements instead of if-else
1 parent 5b036e6 commit 56e70de

File tree

1 file changed

+45
-30
lines changed

1 file changed

+45
-30
lines changed

CrapsGame/main.cpp

+45-30
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// main.cpp
33
// CrapsGame
44
//
5+
//
56
// Created by Melinda Fernandes on 02/03/2017.
67
// Copyright © 2017 Melinda Fernandes. All rights reserved.
78
//
@@ -13,6 +14,10 @@ using std::endl;
1314
#include <String>
1415
using std::string;
1516

17+
#include <cstdlib>
18+
using std::rand;
19+
using std::srand;
20+
1621
#include <time.h>
1722
using std::time;
1823

@@ -24,42 +29,52 @@ int main(){
2429
int sum = 0; // store returned sum from rollDice function
2530
int point = 0; // store point when it has been made
2631
string hasRolled = "Player has rolled "; // roll statement to be printed after rolls
27-
32+
2833
srand(time(0)); // store seed from time
2934

3035
sum = rollDice();
3136
cout << hasRolled << sum << endl;
3237

3338
// 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+
6378

6479
return 0; //terminate game
6580
}

0 commit comments

Comments
 (0)