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 165 166 167 168 169 170 171 172 173
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
int main() {
enum choice {ROCK, PAPER, SCISSORS};
choice select;
char user;
bool play_again;
string play;
int num_games = 0;
int player_wins = 0;
int computer_wins = 0;
bool tie = false;
int num_ties = 0;
ofstream outfile;
ifstream infile;
// Ask the user if they would like to play.
cout << "Would you like to play Rock, Paper, Scissors? (y or n): ";
cin >> play;
// If they answer yes, then play, otherwise no.
if(play == "y" || play == "Y"){
play_again = true;
num_games++;
infile.open("RockPaperScissors.txt");
infile >> player_wins >> computer_wins;
infile.close();
}
// Start playing the actual game if they want to play.
while(play_again != false){
cout << endl << endl << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
cin >> user;
cout << endl;
// Taking the input and applying it to constant in enum.
if (user == 'r' || user == 'R'){
select = ROCK;
}
else if (user == 'p' || user == 'P'){
select = PAPER;
}
else if (user == 's' || user == 'S'){
select = SCISSORS;
}
else {
cout << "Come on... That's not an option." << endl;
cout << endl << endl << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
cin >> user;
}
long seed = time(NULL); // gets current time
srand(seed);
// Computer's random selection
int computer = rand();
//DEBUG: cout << computer << endl;
// Computer's random selection
computer = rand() % 3;
//DEBUG: cout << computer << endl << endl;;
//DEBUG: cout << "Here is a test: " << select << endl;
// In the event of a tie, make both players make a new selection and re-evaluate them.
while(computer == select){
cout << endl << "It's a tie, Pick again please: ";
cin >> user;
// This is repeated... :/
if (user == 'r' || user == 'R'){
select = ROCK;
}
else if (user == 'p' || user == 'P'){
select = PAPER;
}
else if (user == 's' || user == 'S'){
select = SCISSORS;
}
else {
cout << "Come on... That's not an option." << endl;
cout << "Rock, Paper, or Scissors? ('r', 'p', or 's'): " ;
cin >> user;
}
computer = rand() % 3;
}
// Applying the computer's selection to the appropriate value in enum.
choice cpu_select;
if (computer == 0){
cpu_select = ROCK;
}
else if (computer == 1){
cpu_select = PAPER;
}
else if (computer == 2){
cpu_select = SCISSORS;
}
// Displays each respective selection.
cout << endl << endl << "Your selection is " << select << endl;
cout << "The computer selects " << cpu_select << endl;
// Win conditions
if ((select == ROCK && cpu_select == SCISSORS) || (select == PAPER && cpu_select == ROCK) || (select == SCISSORS && cpu_select == PAPER)){
cout << endl << "You win." << endl;
player_wins ++;
}
else if((cpu_select == ROCK && select == SCISSORS) || (cpu_select == PAPER && select == ROCK) || (cpu_select == SCISSORS && select == PAPER)){
cout << endl << "The computer wins." << endl;
computer_wins ++;
}
// Show the results of all the games.
cout << "The player has won " << player_wins << " games." << endl;
cout << "The computer has won " << computer_wins << " games." << endl;
// Ask if the player would like to play again.
cout << endl << endl << "Would you like to play again?: ";
cin >> play;
// Exit the loop if the player chooses no.
if (play == "n" || play == "N"){
play_again = false;
}
else if (play == "y" || play == "Y"){
num_games++;
}
outfile.open("RockPaperScissors.txt");
//outfile << num_games << "\n";
outfile << player_wins << "\n";
outfile << computer_wins << "\n";
outfile << num_ties << "\n";
outfile.close();
}
system("pause");
return 0;
}
|