-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_2.cpp
More file actions
185 lines (152 loc) · 4.45 KB
/
assignment_2.cpp
File metadata and controls
185 lines (152 loc) · 4.45 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
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
174
175
176
177
178
179
180
181
182
183
184
/*
* Name : Shoot Out
* Author : Matt Sterkel
* Description : Western style shoot out game
*/
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include "CinReader.h"
using std::cout;
using std::cin;
using std::endl;
using std::string;
void ClearScreen();
int main() {
// Set up our CinReader instance
CinReader reader;
char again ='Y', player_choice;
int computer_choice;
bool user_dead, computer_dead, draw;
int user_gun;
int computer_gun;
int victory = 0;
int defeat = 0;
int tie = 0;
// Seed the Random Number Generator
srand(time(0));
cout << "Welcome to Shoot out! \n \nPress any key to continue" << endl;
reader.readString();
ClearScreen();
// GAME LOOP
do {
computer_choice = 0;
user_dead = false;
computer_dead = false;
draw = false;
user_gun = 0;
computer_gun = 1;
cout << "Ememy Gun: Unloaded Your Gun: Unloaded" <<
endl<<endl;
cout << "Let's do this! \n\"r\" for reload - "<< endl;
cout << "\"s\" for shoot (if gun is loaded) - " << endl;
cout << "\"b\" for block" << endl;
player_choice = tolower(reader.readChar("BbRr"));
ClearScreen();
if(player_choice == 'r') {
user_gun = 1;
cout << "Locked and loaded." << endl;
}
else {cout << "A little jumpy huh..." << endl;}
do {
if (user_gun == 0 && computer_gun == 0) {
cout << "Ememy Gun: Unloaded Your Gun: Unloaded" <<
endl<<endl;
}
else if (user_gun == 0 && computer_gun != 0) {
cout << " Enemy Gun: Loaded Your Gun: Unloaded " <<
endl<<endl;
}
else if (user_gun != 0 && computer_gun == 0) {
cout << " Enemy Gun: Unloaded Your Gun: Loaded " <<
endl<<endl;
}
else { cout << " Enemy Gun: Loaded Your Gun: Loaded " <<
endl<<endl;
}
cout << "Make your move! \n\"r\" for reload - "<< endl;
cout << "\"s\" for shoot (if gun is loaded) - " << endl;
cout << "\"b\" for block" << endl;
if(user_gun == 0) {
player_choice = tolower(reader.readChar("BbRr"));
}
else {
player_choice = tolower(reader.readChar("BbSs"));
}
ClearScreen();
if(computer_gun == 0) {
computer_choice = rand() % 2;
}
else {
computer_choice = rand() % 2 +1;
}
if(player_choice == 'r' && computer_choice == 0) {
user_gun = 1;
computer_gun = 1;
cout << "Locked and loaded." << endl;
}
else if(player_choice == 'r' && computer_choice == 1) {
user_gun = 1;
cout << "Locked and loaded." << endl;
}
else if(player_choice == 'r' && computer_choice == 2) {
user_dead = true;
cout << "You've been hit! \nGame over pal.\n" << endl;
}
else if(player_choice == 'b' && computer_choice == 0) {
computer_gun = 1;
cout << "Carefull, your opponent's gun is now loaded!" << endl;
}
else if(player_choice == 'b' && computer_choice == 1) {
cout << "Looks like you both blocked...cowards!" << endl;
}
else if(player_choice == 'b' && computer_choice == 2) {
computer_gun = 0;
cout << "Successful evade!" << endl;
}
else if(player_choice == 's' && computer_choice == 0){
computer_dead = true;
cout << "You got em'!\n" << endl;
}
else if(player_choice == 's' && computer_choice == 1) {
cout << "Ah shucks...your opponent evaded your shot." << endl;
user_gun =0;
}
else if(player_choice == 's' && computer_choice == 2) {
draw = true;
cout << "You got each other... a draw I reckon.\n" << endl;
}
else{ cout << "Game Has Crashed - Sorry Partner" << endl; }
/* The following code was used when trouble shooting this program.
cout << "computer_choice=" << computer_choice << " user dead=" << user_dead
<< "draw = " << draw << "player_choice= "<<player_choice
<< "computer_dead= " << computer_dead << "computer_gun =" <<
computer_gun <<endl;
*/
} while(user_dead == false && computer_dead == false && draw == false);
if(user_dead == true) {
++defeat;}
else if(computer_dead == true) {
++victory;}
else {
++tie;}
cout << "Wins: " << victory << " " << "Losses: " << defeat << " " << "Ties: "
<< tie << endl;
// Prompt user to see if the want to continue to play
cout << "Would you like to play again? (Y / N)\n";
again = toupper(reader.readChar("YNyn"));
cout << endl;
ClearScreen();
} while (again == 'Y');
return 0;
}
void ClearScreen() {
#ifdef _WIN32
std::system("cls");
#else
// Assume POSIX
std::system("clear");
#endif
}