-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcoin_toss.cpp
164 lines (139 loc) · 2.88 KB
/
coin_toss.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Chapter 1 - 7.2 knowledge practice:
2 Player coin toss game:
This practice session is focused on generating a cointoss game
between 2 players. Each player will take turns guessing heads or
tails. The first person to guess 5 correctly, wins.
CoinToss
- playerName
- score
- tossCoin
*/
#include <iostream>
#include <string>
#include <stdlib.h> //required to run rand()
#include <time.h> //used for rand()
using namespace std;
class CoinToss{
public:
void setDefault();
void setPlayerName(string param);
void addScore(int modifier);
bool tossCoin();
string getPlayerName();
int getScore();
private:
string playerName;
int score;
};
void CoinToss::setDefault(){
score = 0;
playerName = "undefined";
}
void CoinToss::setPlayerName(string param){
playerName = param;
}
void CoinToss::addScore(int modifier){
score = score + modifier;
}
bool CoinToss::tossCoin(){
srand (time(NULL));
return rand() % 2;
}
string CoinToss::getPlayerName(){
return playerName;
}
int CoinToss::getScore(){
return score;
}
void AskPlayer1(){
CoinToss player1;
char playerChoice = '?';
//system("clear");
cout << "Player 1: " << player1.getPlayerName() << endl;
cout << "Heads or Tails (h/t): ";
cin >> playerChoice;
switch (playerChoice) {
case 'h':
if(player1.tossCoin() == 1){
cout << "Its heads. You gain 1 point!" << endl;
player1.addScore(1);
}
else{
cout << "Sorry, it landed on tails." << endl;
}
break;
case 't':
if(player1.tossCoin() == 0){
cout << "Its tails. You gain 1 point!" << endl;
player1.addScore(1);
}
else{
cout << "Sorry, it landed on heads." << endl;
}
break;
default:
break;
}
cout << endl;
}
void AskPlayer2(){
CoinToss player2;
char playerChoice = '?';
cout << "Player 2: " << player2.getPlayerName() << endl;
cout << "Heads or Tails (h/t): ";
cin >> playerChoice;
switch (playerChoice) {
case 'h':
if(player2.tossCoin() == 1){
cout << "Its heads. You gain 1 point!" << endl;
player2.addScore(1);
}
else{
cout << "Sorry, it landed on tails." << endl;
}
break;
case 't':
if(player2.tossCoin() == 0){
cout << "Its tails. You gain 1 point!" << endl;
player2.addScore(1);
}
else{
cout << "Sorry, it landed on heads." << endl;
}
break;
default:
break;
}
cout << endl;
}
void AskPlayerName(){
CoinToss player1;
CoinToss player2;
//Ask for players name
string p1, p2;
cout << "Player 1: ";
getline(cin, p1);
player1.setPlayerName(p1);
cout << "Player 2: ";
getline(cin, p2);
player2.setPlayerName(p2);
}
//** MAIN **//
int main(int argc, char const *argv[]) {
//create player objects
CoinToss player1;
CoinToss player2;
//set defaults
player1.setDefault();
player2.setDefault();
do{
AskPlayer1();
cout << player1.getScore();
AskPlayer2();
}while(
!(((player1.getScore() >= 5)) || ((player2.getScore() >= 5)))
);
cout << endl;
return 0;
}