-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
310 lines (259 loc) · 12.8 KB
/
Program.cs
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
class Game
{
//Scoring for the Player Character and Computer
public int playerScore = 0, CompScore = 0;
//Rounds for the Game
public int currRound = 0, maxRounds = 5;
//Has the Game Started?
public bool Init;
//Contains display data for what the Player Selected
public string[] playerSelectionPrompt = {
"Player Selected Rock!",
"Player Selected Scissors!",
"Player Selected Paper!",
"Player Selected Lizard!",
"Player Selected Spock!"
};
//Contains display data for what the Computer Selected
public string[] compSelectionPrompt = {
"Computer Selected Rock!",
"Computer Selected Scissors!",
"Computer Selected Paper!",
"Computer Selected Lizard!",
"Computer Selected Spock!"
};
//Contains display data for what the results of the round could be
public string[] resultsPrompt = {
"Result: Player Wins!\nA point is awarded to the Player!",
"Result: Computer Wins!\nA point is awarded to the Computer!",
"Result: TIE!"
};
//Result of the Table
public string[,] resultTable = {
{"TIE", "COMPUTER", "PLAYER", "PLAYER", "COMPUTER"},
{"PLAYER", "TIE", "COMPUTER", "COMPUTER", "PLAYER"},
{"COMPUTER", "PLAYER", "TIE", "PLAYER", "COMPUTER"},
{"COMPUTER", "PLAYER", "COMPUTER", "TIE", "PLAYER"},
{"PLAYER", "COMPUTER", "PLAYER", "COMPUTER", "TIE"}
};
//Reason for the Result of the Round
public string[,] reasonTable = {
{"Rock is Rock!", "Paper Covers Rock!", "Rock Crushes Scissors!", "Scissors Decapitates Lizard!", "Spock Vaporizes Rock!"},
{"Paper Covers Rock!", "Paper is Paper!", "Scissors Cutes Paper!", "Lizard Eats Paper!", "Paper Disproves Spock!"},
{"Rock Crushes Scissors!", "Scissors Cuts Paper!", "Scissors is Scissors!", "Scissors Decapitates Lizard!", "Spock Smashes Scissors!"},
{"Rock Crushes Lizard!", "Lizard Eats Paper!", "Scissors Decapitates Lizard!", "Lizard is Lizard!", "Lizard Poisons Spock!"},
{"Spock Vaporizes Rock!", "Paper Disproves Spock!", "Spock Smahes Scissors!", "Lizard Poisons Spock!", "Spock is Spock!"}
};
//Greet the player and tell the player the rules of the game
public bool Greeting()
{
//Contents of the Greeting Message
string[] prompt = {
"----------------------------------------------------------------------------------",
"Welcome to Rock, Paper, Scissors, Lizard, Spock!",
"----------------------------------------------------------------------------------",
"Rules:",
"----------------------------------------------------------------------------------",
"\t1.) Rock",
"----------------------------------------------------------------------------------",
"\t\ta.) Crushes Lizard",
"\t\tb.) Crushes Scissors",
"\t\tc.) Gets Vaporized by Spock",
"\t\td.) Gets Covered by Paper",
"----------------------------------------------------------------------------------",
"\t2.) Paper",
"----------------------------------------------------------------------------------",
"\t\ta.) Covers Rock",
"\t\tb.) Disproves Spock",
"\t\tc.) Gets Eaten by Lizard",
"\t\td.) Gets cut by Scissors",
"----------------------------------------------------------------------------------",
"\t3.) Scissors",
"----------------------------------------------------------------------------------",
"\t\ta.) Cuts Paper",
"\t\tb.) Decapitates Lizard",
"\t\tc.) Gets Crushed by Rock",
"\t\td.) Gets Smashed by Spock",
"----------------------------------------------------------------------------------",
"\t4.) Lizard",
"----------------------------------------------------------------------------------",
"\t\ta.) Poisons Spock",
"\t\tb.) Eats Paper",
"\t\tc.) Gets Crushed by Rock",
"\t\td.) Get decapitated by Scissors",
"----------------------------------------------------------------------------------",
"\t5.) Spock",
"----------------------------------------------------------------------------------",
"\t\ta.) Smashes Scissors",
"\t\tb.) Vaporizes Rock",
"\t\tc.) Gets Poisioned by Lizard",
"\t\td.) Gets Disproven by Paper",
"----------------------------------------------------------------------------------",
"\t6.) The side to win the majority of 5 rounds, will be deemed the winner",
"----------------------------------------------------------------------------------",
};
//Output each line of the Prompt
for (int i = 0; i < prompt.Length; i++)
{
Console.WriteLine(prompt[i]);
}
//Return true to indicate that the Game has begun
return true;
}
//Display the Current Score of the Player Character
public void DisplayCurrentScore(Game game)
{
Console.WriteLine($"\t\t\t\tRound: {game.currRound+1}\n\t\t\tPlayer: {game.playerScore} | Computer: {game.CompScore}");
}
//Prompt Player to make a Selection
public int MakePlayerSelection()
{
//User enter selection here
string selection = Console.ReadLine();
//Parse Selection to an Integer Value
int selectionNum = int.Parse(selection);
return selectionNum;
}
//Prompt Player if they would like to play again
public void playAgain(Game game)
{
Console.WriteLine("----------------------------------------------------------------------------------");
Console.Write("Do you want to play again? (Y/N) ");
string decision = Console.ReadLine();
if(decision.Equals("Yes", StringComparison.OrdinalIgnoreCase))
{
//Start Game from Fresh
game.GameLoop(new Game());
}
else if (decision.Equals("No", StringComparison.OrdinalIgnoreCase))
{
return;
}
else
{
Console.WriteLine("INVALID INPUT! PLEASE ENTER A VALID INPUT!");
game.playAgain(game);
}
}
//Player Choice Selection
public int PlayerSelection()
{
//Selection Menu to aid player in making selection
string[] prompt =
{
"----------------------------------------------------------------------------------",
"Selection Menu",
"----------------------------------------------------------------------------------",
"\t1.) Rock",
"\t2.) Paper",
"\t3.) Scissors",
"\t4.) Lizard",
"\t5.) Spock",
"----------------------------------------------------------------------------------",
"Make your Selection: ",
};
//Output Selection Menu
for (int i = 0; i < prompt.Length-1; i++)
{
Console.WriteLine(prompt[i]);
}
//Output Prompt for player to input their selection
Console.Write(prompt[prompt.Length-1]);
try
{
//Parse Selection to an Integer Value
int selectionNum = MakePlayerSelection();
//Format the output of the program
Console.WriteLine(prompt[prompt.Length - 2]);
return selectionNum;
}
catch(FormatException)
{
//Display Error Message
Console.WriteLine("INVALID INPUT! PLEASE ENTER A INTEGER VALUE (1-5)!");
//Perform recursion
int selectionNum = PlayerSelection();
return selectionNum;
}
}
//Generate Computer Selection
public int CompSelection()
{
// Create a new instance of the Random class
Random random = new Random();
// Generate a random integer between 1 and 5 (inclusive)
int randomNumber = random.Next(1, 6);
return randomNumber;
}
//Process the results of the match
public Game processResult(Game game, int playerSelection, int compSelection)
{
//The Index pointing at the Player's Selection and the Computer's Selection
int i = playerSelection - 1, j = compSelection - 1;
//The display the selection of the Player and Computer
string selectionDisplay = $"{game.playerSelectionPrompt[i]} {game.compSelectionPrompt[j]}";
//The reason for the round's result
string reasonDisplay = game.reasonTable[i, j];
//Get the result display of the round
string resultItem = game.resultTable[i, j];
int index = (resultItem == "PLAYER") ? 0 : (resultItem == "COMPUTER") ? 1 : 2;
string resultDisplay = game.resultsPrompt[index];
//Update Score
switch(index)
{
//If Index points to a winning player result
case 0:
game.playerScore++;
break;
case 1:
game.CompScore++;
break;
}
//Increment the current Round
game.currRound++;
//Display the result of the round
Console.WriteLine($"{selectionDisplay}\n{reasonDisplay}\n{resultDisplay}");
Console.WriteLine("----------------------------------------------------------------------------------");
return game;
}
//Display the results of the Game
public void displayResults(Game game)
{
Console.WriteLine($"Game Over!\nFinal Score: {game.playerScore} - {game.CompScore}");
if (game.playerScore > game.CompScore) Console.WriteLine("Result: Player Wins!");
else if (game.playerScore > game.CompScore) Console.WriteLine("Result: Computer Wins!");
else Console.WriteLine("Result: Tie!");
}
//Run the Game for it to run
public void GameLoop(Game game)
{
//Greet the Player
game.Init = game.Greeting();
//Game Loop
while (game.Init)
{
//Display the Current Score for the game
game.DisplayCurrentScore(game);
//Get Player and Computer Selections
int playerSelection = game.PlayerSelection();
int compSelection = game.CompSelection();
//Process the result of the player and computer decision
game = game.processResult(game, playerSelection, compSelection);
// Check if game has hit 5 rounds
if (game.currRound >= 5) game.Init = false;
}
//Display Results of the Game
game.displayResults(game);
//Ask Player if they would like to play again
game.playAgain(game);
}
//Driver Function
private static void Main()
{
//Initialize the Game Object
Game game = new Game();
//Run the Game
game.GameLoop(game);
}
}