forked from cameronmcnz/rock-paper-scissors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.html
224 lines (176 loc) · 5.18 KB
/
game.html
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
<html>
<head>
<title>Rock Paper Scissors!!!</title> <html>
</head>
<body>
Which one will you choose?<br/>
<a href="#" onclick="playRoshambo('rock')"> rock </a>
<a href="#" onclick="playRoshambo('paper')"> paper </a>
<a href="#" onclick="playRoshambo('scissors')"> scissors </a>
<br/>
<div id="results"></div>
<div id="wins"></div>
<div id="losses"></div>
<div id="ties"></div>
<div id="history"></div>
</body>
</html>
<script>
playRoshambo = function(clientGesture) {
console.log("************** Playing Roshambo *****************");
let gameService = new GameService();
let gameSummary = gameService.playGame(clientGesture);
let theScore = gameService.getScore();
console.log("Here is the returned gs: " + gameSummary + " :---:");
document.getElementById('results').innerHTML = gameSummary.result;
document.getElementById('wins').innerHTML = theScore.wins;
document.getElementById('losses').innerHTML = theScore.losses;
document.getElementById('ties').innerHTML = theScore.ties;
renderGameHistory(gameService.getGameHistory());
}
renderGameHistory = function(gameHistory){
console.log("************** RENDERING GAME HISTORY *****************");
let output = "<table><tr><th>Client</th><th>Server</th><th>Result</th><th>Date</th></tr>";
for (let i=0; i < gameHistory.length; i++){
let gameSummary = gameHistory[i];
let date = gameSummary.date;
console.log(gameSummary);
output = output + " <tr>";
output = output + " <td> " + gameSummary.clientGesture + " </td> ";
output = output + " <td> " + gameSummary.serverGesture + " </td> ";
output = output + " <td> " + gameSummary.result + " </td> ";
output = output + " <td> " + date + " </td> ";
output = output + " </tr>";
console.log(output);
}
output = output + "</table>";
document.getElementById('history').innerHTML = output;
}
</script>
<script>
function Score() {
this.wins=0;
this.losses=0;
this.ties=0;
this.increaseWins = function(){
this.wins++;
}
this.increaseLosses = function(){
this.losses++;
}
this.increaseTies = function(){
this.ties++;
}
this.toString = function(){
output = "Wins: " + this.wins + " Ties: " + this.ties + " Losses: " + this.losses;
return output;
}
}
</script>
<script>
function GameSummary(client, server, result) {
this.clientGesture=client;
this.serverGesture=server;
this.result=result;
this.date=new Date();
this.getClientGesture = function(){
this.clientGesture;
}
this.getServerGesture = function(){
this.serverGesture;
}
this.getResult = function(){
this.result;
}
this.getDate = function(){
this.date;
}
this.toString = function() {
let output = "Client :: " + this.clientGesture;
let simpleDate = this.date.toLocaleDateString("en-US");
output = output + " :: Server :: " + this.serverGesture;
output = output + " :: Result :: " + this.result;
output = output + " :: Time played :: " + simpleDate;
return output;
}
}
</script>
<script>
let theScore = new Score();
var gameHistory =[];
function GameService() {
this.getScore = function() {
return theScore;
}
this.getGameHistory = function() {
return gameHistory;
}
this.playGame = function(input) {
let result = "error";
if (input==("scissors")) {
result = "lose";
theScore.increaseLosses();
}
if (input==("paper")) {
result = "win";
theScore.increaseWins();
}
if (input==("rock")) {
result = "tie";
theScore.increaseTies();
}
if (result == "error") { return; }
console.log("The is the result: " + result);
let gameSummary = new GameSummary(input, "rock", result);
gameHistory.unshift(gameSummary);
this.printGameHistory(gameHistory);
console.log(theScore.toString());
console.log("Number of wins: " + theScore.wins);
console.log(gameSummary + " :: ");
return gameSummary;
}
this.printGameHistory = function(gameHistory){
console.log("************** GAME HISTORY *****************");
let aggregate = "";
for (let i=0; i < gameHistory.length; i++){
let gameSummary = gameHistory[i];
console.log(gameSummary);
let output = "Client :: " + gameSummary.clientGesture;
output = output + " :: Server :: " + gameSummary.serverGesture;
output = output + " :: Result :: " + gameSummary.result;
output = output + " :: Time played :: " + gameSummary.date;
console.log(output);
aggregate = aggregate + output + "<br/>";
}
document.getElementById('history').innerHTML = aggregate;
console.log("***********END OF GAME HISTORY *****************");
}
}
</script>
<script>
/*
let gs = new GameService();
gs.playGame("paper");
var gameHistory =[];
{
let gameSummary = new GameSummary("rock","rock","tie");
gameHistory.unshift(gameSummary);
}
{
let gameSummary = new GameSummary("paper","rock","loss");
gameHistory.unshift(gameSummary);
}
{
let gameSummary = new GameSummary("scissors","paper","win");
gameHistory.unshift(gameSummary);
}
console.log(gameHistory);
var score = new Score();
score.increaseWins();
score.increaseWins();
score.increaseTies();
score.increaseLosses();
console.log(score.wins);
console.log(score.toString());
*/
</script>