-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
240 lines (211 loc) · 6 KB
/
Main.cs
File metadata and controls
240 lines (211 loc) · 6 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
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
using System;
using Microsoft.CSharp;
using Microsoft.Win32.SafeHandles;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Thanks to
/// http://codereview.stackexchange.com/users/38054/benvlodgi
/// http://codereview.stackexchange.com/users/4318/eric-lippert
/// http://codereview.stackexchange.com/users/23788/mats-mug
/// http://codereview.stackexchange.com/users/30346/chriswue
/// </summary>
namespace RPSLSGame
{
class MainClass
{
public static void Main (string[] args)
{
/* Here are your rules:
"Scissors cuts paper,
paper covers rock,
rock crushes lizard,
lizard poisons Spock,
Spock smashes scissors,
scissors decapitate lizard,
lizard eats paper,
paper disproves Spock,
Spock vaporizes rock.
And as it always has, rock crushes scissors."
-- Dr. Sheldon Cooper */
int win = 0;
int lose = 0;
int tie = 0;
IList<Gesture> gameGestures = GetGestures();
do{
Console.WriteLine("Please choose your Gesture ");
PrintMenu(gameGestures);
var playerGesture = gameGestures[PromptForNumber("Please choose your gesture",1,gameGestures.Count)-1];
var computerPlay = GetRandomOption(gameGestures);
Console.WriteLine ("Computer: " + computerPlay);
Console.WriteLine ("your Gesture: " + playerGesture);
if (playerGesture.Equals(computerPlay)) {
Console.WriteLine("Game ended in a stalemate")
tie++;
} else if (playerGesture.Defeats(computerPlay)) {
Console.WriteLine("You're a Winner")
win++;
} else {
Console.WriteLine("you got schooled!")
lose++;
}
Console.WriteLine ("Your Score is (W:L:T:) : {0}:{1}:{2}", win, lose, tie);
if (Choice("Would you like to reset your score?"))
{
win = 0;
lose=0;
tie=0;
}
}while (Choice("Would you like to play again? Y/N"));
Console.WriteLine("Goodbye");
Console.ReadLine ();
}
public static void DecideWinner ()
{
//TODO: Create Method for Deciding the Winner.
//I still need to move the decision logic here.
//but it won't be as big as I thought it was going to be
}
static int PromptForNumber (string prompt, int lower, int upper)
{
int? pick = null;
while (pick == null) {
Console.WriteLine(prompt);
pick = Console.ReadLine().BoundedParse (lower, upper);
}
return pick.Value;
}
public static void PrintMenu (List<string> List)
{
for (int i=0; i<List.Count; i++) {
Console.WriteLine ((i+1) + ": " + List[i]);
}
}
public static void PrintMenu (IList<Gesture> GestureList)
{
for (int i=0; i < GestureList.Count; i++) {
Console.WriteLine ((i + 1) + ": " + GestureList [i]);
}
}
public static string GetRandomOption (List<string> options)
{
Random rand = new Random();
return options[rand.Next(0,options.Count)];
}
public static Gesture GetRandomOption (IList<Gesture> options)
{
Random rand = new Random();
return options[rand.Next (0,options.Count)];
}
public static Boolean Choice (string prompt)
{
while(true)
{
Console.WriteLine (prompt);
switch (Console.ReadKey (true).Key)
{
case ConsoleKey.Y:
{
Console.Write ("Y\n");
return true;
}
case ConsoleKey.N:
{
Console.Write ("N\n");
return false;
}
}
}
}
/// <summary>
/// This is some code that I borrowed from
/// http://codereview.stackexchange.com/users/14749/peter-kiss
/// from an answer that he gave to one of my Questions, the answer has since been deleted.
/// </summary>
static IList<Gesture> GetGestures()
{
var spock = new Gesture("Spock");
var lizard = new Gesture("Lizard");
var paper = new Gesture("Paper");
var rock = new Gesture("Rock");
var scissors = new Gesture("Scissors");
spock.AddDefeats(new[] { scissors, rock });
lizard.AddDefeats(new[] { paper, spock });
paper.AddDefeats(new[] { rock, spock });
rock.AddDefeats(new[] { lizard, scissors });
scissors.AddDefeats(new[] { paper, lizard });
var gestures = new List<Gesture> {rock, paper, scissors, lizard, spock};
gestures.ForEach(x => x.Seal());
return gestures;
}
}
public class Gesture
{
public override string ToString()
{
return Name;
}
private HashSet<Gesture> _defeats = new HashSet<Gesture>();
private bool _isSealed;
public string Name { get; private set; }
public Gesture(string name)
{
if (name == null) throw new ArgumentNullException("name");
Name = name;
}
public void Seal()
{
_isSealed = true;
}
public void AddDefeats(IEnumerable<Gesture> defeats)
{
if (_isSealed)
{
throw new Exception("Gesture is sealed");
}
foreach (var gesture in defeats)
{
_defeats.Add(gesture);
}
}
//public void AddKillMessage(Tuple<Gesture,Gesture,string>
public void AddKillMessage (Dictionary<Dictionary<Gesture,Gesture>,string> killMessage)
{
if (_isSealed) {
throw new Exception ("Gesture is Sealed");
}
}
protected bool Equals(Gesture other)
{
return string.Equals(Name, other.Name);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Gesture) obj);
}
public override int GetHashCode()
{
return Name.GetHashCode();
}
public bool Defeats(Gesture gesture)
{
return _defeats.Any(x => x.Equals(gesture));
}
}
}
static class Extensions
{
public static int? BoundedParse (this string str, int lower, int upper)
{
int result;
bool success;
success = int.TryParse (str, out result);
if (!(lower <= result && result <= upper) || (!success) || (str == null)){
return null;
}
return result;
}
}