-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSPGameV2.cpp
126 lines (115 loc) · 4.28 KB
/
RSPGameV2.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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
#include <limits>
using namespace std;
int main() {
int rock = 0;
int scissors = 1;
int paper = 2;
int playerChoice;
int playerWins = 0;
int cpuWins = 0;
cout << "Welcome to ROCK, SCISSORS, PAPER! " << endl;
cout << "Rules: " << endl;
cout << "ROCK beats SCISSORS " << endl;
cout << "SCISSORS beats PAPER " << endl;
cout << "PAPER beats ROCK " << endl;
cout << "----Results stored in LOG file--- " << endl;
cout << "Instructions: " << endl;
cout << "For ROCK, choose 0 " << endl;
cout << "For SCISSORS, choose 1 " << endl;
cout << "For PAPER, choose 2 " << endl;
while (playerWins < cpuWins + 2 && cpuWins < playerWins + 2) {
cout << endl << "Enter your choice: ";
cin >> playerChoice;
cout << playerChoice << endl;
srand(time(NULL));
int cpuChoice = rand() % 3 ;
while(1)
{
if(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "You have entered wrong input " << endl ;
cout << "Please enter a valid value for either ROCK, SCISSORS, or PAPER ";
cin >> playerChoice;
}
if(!cin.fail())
break;
}
switch (playerChoice) {
case 0: // The case for when the player picks ROCK
if (cpuChoice == 0) {
cout << "You've chosen ROCK, and the PC played ROCK.. " << endl << "YOU TIE! " << endl;
break;
}
if (cpuChoice == 1) {
cout << "You've chosen ROCK, and the PC played SCISSORS.. " << endl << "YOU WIN! " << endl;
playerWins++;
break;
}
if (cpuChoice == 2) {
cout << "You've chosen ROCK, and the PC played PAPER.. " << endl << "YOU LOSE! " << endl;
cpuWins++;
break;
}
case 1: // the case for when the player picks SCISSORS
if (cpuChoice == 0) {
cout << "You've chosen SCISSORS, and the PC played ROCK.. " << endl << "YOU LOSE! " << endl;
cpuWins++;
break;
}
if (cpuChoice == 1) {
cout << "You've chosen SCISSORS, and the PC played SCISSORS.. " << endl << "YOU TIE! " << endl;
break;
}
if (cpuChoice == 2) {
cout << "You've chosen SCISSORS, and the PC played PAPER.. " << endl << "YOU WIN! " << endl;
playerWins++;
break;
}
case 2: // the case for when the player picks PAPER
if (cpuChoice == 0) {
cout << "You've chosen PAPER, and the PC played ROCK.. " << endl << "YOU WIN! " << endl;
playerWins++;
break;
}
if (cpuChoice == 1) {
cout << "You've chosen PAPER, and the PC played SCISSORS. " << endl << "YOU LOSE! " << endl;
cpuWins++;
break;
}
if (cpuChoice == 2) {
cout << "You've chosen PAPER, and the PC played PAPER .. " << endl << "YOU TIE! " << endl;
break;
}
default:
cout << "Please enter a valid value for either ROCK, SCISSORS, or PAPER " << endl;
break;
}
if (playerWins >= cpuWins + 2 || cpuWins >= playerWins + 2) {
cout << "GAME OVER\n";
cout << "Would you like to play again? (Yes: 1. No, 0)" << endl;
int choice;
cin >> choice;
if (choice == 1) {
cpuWins = 0;
playerWins = 0;
}
}
}
ofstream output;
output.open("RPSResults.txt");
output << "Results" << endl;
output << "Player Wins: " << playerWins << endl;
output << "Computer Wins: " << cpuWins << endl;
output.close();
return 0;
}
//
// Created by Toby on 2021-09-27.
//