-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
213 lines (187 loc) · 6.03 KB
/
Program.cs
File metadata and controls
213 lines (187 loc) · 6.03 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
/**
* This program allows user to play ROCK, PAPER AND SCISSOR game vs Computer
* @author yam shrestha
*/
using System;
using System.Collections.Generic;
namespace RPSGame
{
class MainClass
{
//This field will store how many times the specific move has been used by computer
private Dictionary<string,int> computerDic = new Dictionary<string, int> ();
//This field will store how many times the specific move has been used by computer
private Dictionary<string,int> humanDic = new Dictionary<string, int> ();
//This field will store how many times computer has won
private static int computerWon = 0;
//This field will store how many times human has won
private static int humanWon = 0;
//This field will store how many times game was draw
private static int drawGame = 0;
public MainClass ()
{
computerDic.Add ("ROCK", 0);
computerDic.Add ("PAPER", 0);
computerDic.Add ("SCISSOR", 0);
humanDic.Add ("ROCK", 0);
humanDic.Add ("PAPER", 0);
humanDic.Add ("SCISSOR", 0);
}
/**
* This method prints out the result
* how many times the game has been played
* how many times computer has won
* how many times human has won
* how many times the match has draw
*/
public void printOutput ()
{
int maxComputerMove = -1000;
int maxHumanMove = -1000;
foreach (KeyValuePair<string, int> pair in computerDic) {
if (pair.Value > maxComputerMove) {
maxComputerMove = pair.Value;
}
}
foreach (KeyValuePair<string, int> pair in humanDic) {
if (pair.Value > maxHumanMove) {
maxHumanMove = pair.Value;
}
}
Console.WriteLine ("Number of times game played:" + (drawGame + humanWon + computerWon));
Console.WriteLine ("Number of times game draw:" + drawGame);
Console.WriteLine ("Number of times game won by human:" + humanWon);
Console.WriteLine ("Number of times game won by computer:" + computerWon);
Console.WriteLine ("Most move made by computer during the game:");
foreach (KeyValuePair<string, int> pair in computerDic) {
if (pair.Value == maxComputerMove) {
Console.Write (pair.Key + ", ");
}
}
Console.WriteLine ();
Console.WriteLine ("Most move made by human during the game:");
foreach (KeyValuePair<string, int> pair in humanDic) {
if (pair.Value == maxHumanMove) {
Console.Write (pair.Key + ", ");
}
}
}
/**
*
* This method takes the entered input and compare with computer randomly generated move
* The computer move has been generated randomly using date and time
*/
public void playGame (String userSelection)
{
int userSelectionNumber = 0;
int count = 0;
int countComputer = 0;
switch (userSelection) {
case "ROCK":
count = humanDic ["ROCK"];
count++;
humanDic ["ROCK"] = count;
userSelectionNumber = 0;
break;
case "SCISSOR":
count = humanDic ["SCISSOR"];
count++;
humanDic ["SCISSOR"] = count;
userSelectionNumber = 1;
break;
case "PAPER":
count = humanDic ["PAPER"];
count++;
humanDic ["PAPER"] = count;
userSelectionNumber = 2;
break;
}
Random rnd = new Random (DateTime.Now.Millisecond);
int computerSelection = rnd.Next (3);
//computerSelection=0 ROCK
//computerSelection=1 SCISSOR
//computerSelection=2 PAPER
switch (computerSelection) {
case 0:
countComputer = computerDic ["ROCK"];
countComputer++;
computerDic ["ROCK"] = countComputer;
if (userSelectionNumber == 0) {
drawGame++;
Console.WriteLine ("Computer selected ROCK, DRAW");
} else if (userSelectionNumber == 1) {
computerWon++;
Console.WriteLine ("Computer selected ROCK, Computer WIN");
} else if (userSelectionNumber == 2) {
humanWon++;
Console.WriteLine ("Computer selected ROCK, User WIN");
}
break;
case 1:
countComputer = computerDic ["SCISSOR"];
countComputer++;
computerDic ["SCISSOR"] = countComputer;
if (userSelectionNumber == 0) {
humanWon++;
Console.WriteLine ("Computer selected SCISSOR, User WIN");
} else if (userSelectionNumber == 1) {
drawGame++;
Console.WriteLine ("Computer selected SCISSOR, DRAW");
} else if (userSelectionNumber == 2) {
computerWon++;
Console.WriteLine ("Computer selected SCISSOR, Computer WIN");
}
break;
case 2:
countComputer = computerDic ["PAPER"];
countComputer++;
computerDic ["PAPER"] = countComputer;
if (userSelectionNumber == 0) {
computerWon++;
Console.WriteLine ("Computer selected PAPER, Computer WIN");
} else if (userSelectionNumber == 1) {
humanWon++;
Console.WriteLine ("Computer selected PAPER, User Win");
} else if (userSelectionNumber == 2) {
drawGame++;
Console.WriteLine ("Computer selected PAPER, DRAW");
}
break;
}
Console.WriteLine ();
}
/**
*
* This main method ask user to enter appropriate move.
* A User also gets asked to continue game or not
*/
public static void Main (string[] args)
{
MainClass main = new MainClass ();
Console.WriteLine ("Play Game of ROCK, PAPER and SCISSOR");
bool continueGame = true;
while (continueGame) {
Console.WriteLine ("Please type either ROCK, PAPER OR SCISSOR");
string userSelection = (Console.ReadLine ()).ToUpper ();
while (!(userSelection.Equals ("ROCK") || userSelection.Equals ("PAPER") || userSelection.Equals ("SCISSOR"))) {
Console.WriteLine ("Please type either ROCK, PAPER OR SCISSOR");
userSelection = (Console.ReadLine ()).ToUpper ();
}
main.playGame (userSelection);
Console.WriteLine ();
Console.WriteLine ("Would you like to continue, type Y or N");
char playAgain = (char)Console.Read ();
while (!(playAgain.Equals ('Y') || playAgain.Equals ('y') || playAgain.Equals ('n') || playAgain.Equals ('N'))) {
Console.WriteLine ("Please type Y or N");
Console.WriteLine ();
playAgain = (char)Console.Read ();
}
if (playAgain.Equals ('N') || playAgain.Equals ('n')) {
continueGame = false;
}
Console.WriteLine ();
}
main.printOutput ();
}
}
}