-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLecture4_addingFunctions.cpp
More file actions
85 lines (73 loc) · 2.37 KB
/
Copy pathLecture4_addingFunctions.cpp
File metadata and controls
85 lines (73 loc) · 2.37 KB
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
#include <iostream>
#include <cstdlib>
#include <array>
#include <vector>
using std::cout;
using std::string;
using std::cin;
// choices = available choices for both computer and player, pchoices = player's choices
// putting in global to access in both function and main
const std::array<string, 3> choices{ "rock", "paper", "scissors" };
string determine_outcome(string p, string computer) {
// Check CPU vs. Player choice and display win/tie/loss message
if (p == computer) {
// Check tie first (to reduce number of if/elses)
return "TIE!";
} else if (p == "rock") {
if (computer == "paper") {
return "you lose!";
} else {
cout << "you win";
}
} else if (p == "paper") {
if (computer == "scissors") {
return "you lose!";
} else {
return "you win";
}
} else { // Don't need if scissors as only choice left
if (computer == "rock") {
return "you lose!";
} else {
return "you win";
}
}
}
string get_player_move() {
// Give user all choices and return their selected play
string confirm = "";
for (const auto choice : choices) {
cout << "Do you choose " + choice + "? (y/n)";
cin >> confirm;
// Check if answer starts with Y or y:
// If so: add to vector of choices and exit for
if (confirm[0] == 'y' or confirm[0] == 'Y') {
return choice;
}
}
}
int main()
{
// second version get's multiple choices from computer then generates a whole game
std::vector<string> pchoices;
// Get player choice (keep looping until valid choice or start game selected)
while (true) {
pchoices.push_back(get_player_move());
// Ask player if want to start games because choice made (or no choice made)
string start{};
cout << "\nAre you ready to play the games? (y/n)";
cin >> start;
// If they want to leave we just exit not break (cause done)
if (start[0] == 'y' or start[0] == 'Y') {
cout << "\nOkay, Starting games\n";
break;
}
}
// Loop through choices and compare with our computer's random choice
for (auto p : pchoices) {
// randomly generate choice and print current game
string computer = choices[rand() % 3];
cout << "\n\nCPU chooses " << computer << " vs. " << p << "\n";
cout << determine_outcome(p, computer) << "\n";
}
}