-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseball.html
More file actions
261 lines (237 loc) · 9.1 KB
/
baseball.html
File metadata and controls
261 lines (237 loc) · 9.1 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<!DOCTYPE html>
<html>
<head>
<title>Baseball Game</title>
<style type="text/css">
body{
background-color: lightgray;
}
#entire{
background-color: white;
width:690px;
height: 420px;
margin: auto;
margin-top: 100px;
}
#logo{
float:left;
padding:10px;
}
#game{
float:right;
width: 250px;
height: 400px;
padding:10px;
}
</style>
</head>
<body>
<div id="entire">
<img id="logo" src="http://rlv.zcache.com/baseball_word_ball_logo_photo_statuette-r436daa66a3dc4b55b9b90203407d2e51_x7saw_8byvr_324.jpg" height="400" width="400"/>
<div id="game">
<input type="button" id="start" value="start game" />
<span>Time :</span>
<span id="timer">0</span>
<input type="button" id="end" value="end game"/>
<div id="gamePanel">
<div id="guessField">
<p>guess:
<input type="text" name="guess" value="" size="1" maxlength="1" />
<input type="text" name="guess" value="" size="1" maxlength="1" />
<input type="text" name="guess" value="" size="1" maxlength="1" />
<input type="button" id="try" value="try!" /></p>
</div>
<table border="1" summary="information board">
<caption>Information Board</caption>
<thead>
<tr>
<th>#Trial</th>
<th>Combination</th>
<th>Strike</th>
<th>Ball</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.0.0.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script>
<script>
/* variables */
var isGameStarted = false;
var targetList = [-1,-1,-1];
var guessList = [];
var attemptNum = 0;
var t = null;
var sec = 0;
var trd = "<tr><td>";
var tdd = "</td><td>";
var tdr = "</td></tr>";
var acc = "";
$("#gamePanel").css("display", "none");
/* Count : timer */
function Count(){
t = setTimeout(Count, 1000);
sec++;
$("#timer").html(sec);
}
/* start_handler : start the game. */
$("#start").click(function(){
if(isGameStarted){
if(confirm("game in progress. do you want to start a new game?")){
clearTimeout(t);
init();
}
else $("input[name='guess']")[0].select();
}
else{
init();
}
});
/* init : start a new game */
function init(){
/* reset variables */
isGameStarted = true;
targetList = [-1,-1,-1];
guessList = [];
attemptNum = 0;
sec = 0;
acc = "";
$("tbody").empty();
$("input[name='guess']").val("");
$("#gamePanel").css("display", "block"); // show the game panel
$("input[name='guess']")[0].select(); // set the focus to the first guess
getRand(); // set the target combination
Count(); // start the timer
}
/* getRand : produce a random 3 digit number (target) */
function getRand(){
targetList[0] = Math.ceil(Math.random() * 10) - 1;
for(var i = 1; i <= 2; i++){
while(1){
var temp = Math.ceil(Math.random() * 10) - 1;
if(temp != targetList[0] && temp != targetList[1] && temp != targetList[2]){
targetList[i] = temp;
break;
}
}
}
}
/* try_handler : game operation */
$("#try").click(function(){
var strike = 0;
var ball = 0;
var a = $("input[name='guess']")[0].value;
var b = $("input[name='guess']")[1].value;
var c = $("input[name='guess']")[2].value;
var isValid = input_check();
if(isValid){
guessList[attemptNum] = [a,b,c];
attemptNum++;
/* baseball game logic */
for(var i=0; i<=2; i++){
for(var j=0; j<=2; j++){
if($("input[name='guess']")[i].value == targetList[j]){
if(i == j) strike++;
else ball++;
break;
}
}
}
/* print out the progress */
var temp = trd + attemptNum + tdd + a + b + c + tdd + strike + tdd + ball + tdr;
acc = acc + temp;
$("tbody").html(acc);
/* if correctly guessed */
if(strike == 3){
clearTimeout(t);
if(confirm("congratuation! you have correctly guessed " + a + " " + b + " " + c + " in " + attemptNum + " attempts. do you want to start a new game?")) init();
else $("#gamePanel").css("display", "none");
}
/* if a user used up all the attempts */
else if(attemptNum == 10){
clearTimeout(t);
if(confirm("game over! you have reached max number of attempts. the answer was " + targetList[0] + " " + targetList[1] + " " + targetList[2] + ". do you want to start a new game?")) init();
else{
$("#gamePanel").css("display", "none");
sec=0;
$("#timer").html(sec);
}
}
/* if time out */
else if(sec >= 999){
clearTimeout(t);
if(confirm("time out! it's taking too long. do you want to start a new game?")) init();
else{
gamePanel.style.display="none";
sec=0;
$("#timer").html(sec);
}
}
$("input[name='guess']").val("");
$("input[name='guess']")[0].select();
}
});
/* input_check : check the validity of input */
function input_check(){
var a = $("input[name='guess']")[0].value;
var b = $("input[name='guess']")[1].value;
var c = $("input[name='guess']")[2].value;
var isValid = true;
/* if inputs are not numbers */
if(!Number.isInteger(parseInt(a)) || !Number.isInteger(parseInt(b)) || !Number.isInteger(parseInt(c))){
alert("input must be numbers");
isValid = false;
}
/* if there are missing input */
else if(a == "" || b == "" || c == ""){
alert("you have to guess all 3 digits");
isValid = false;
}
/* if input numbers are not distinct */
else if(a == b || a == c|| b == c){
alert("input numbers should be distinct");
isValid = false;
}
/* if input numbers are out of range */
else if(a > 9 || a < 0 || b > 9 || b < 0 || c > 9 || c < 0){
alert("input numbers should be between 0 and 9");
isValid = false;
}
/* if the user already guessed input */
else {
for(x in guessList){
if(guessList[x][0] == a && guessList[x][1] == b && guessList[x][2] == c){
alert("you already have tried that combination");
isValid = false;
}
}
}
/* clear out the field if error occured */
if(!isValid){
$("input[name='guess']").val("");
$("input[name='guess']")[0].select();
}
return isValid;
}
/* end_handler : end game */
$("#end").click(function(){
if(isGameStarted){
if(confirm("game in progress. do you want to end current game?")){
clearTimeout(t);
$("#gamePanel").css("display", "none"); // hide game panel until start
isGameStarted = false;
sec=0;
$("#timer").html(sec);
}
else $("input[name='guess']")[0].select();
}
});
/* cursor move event */
$("input[name='guess']")[0].onkeyup = function(e){ $("input[name='guess']")[1].select(); }
$("input[name='guess']")[1].onkeyup = function(e){ $("input[name='guess']")[2].select(); }
</script>
</body>
</html>