This repository has been archived by the owner on Mar 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot-star.js
300 lines (285 loc) · 5.99 KB
/
dot-star.js
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
/*
TO DO!!!!
1. Redesign
2. when Game Over, give nicer way to reload
## Prompt user's name
*/
$(document).ready(function() {
initial();
promptu();
$("#toggle-log").click(function (){
$("#log").toggle();
});
});
game = {
turn: "a",
a: {
name : "A",
score : 0
},
b: {
name : "B",
score : 0
},
message: "",
max_score: 0,
tw: 0,
move: {
type: "", // x or y
x: "", // coord x
y: "" // coord y
}
};
function initial() {
/* Initialization */
game = {
turn: "a",
a: {
name : "A",
score : 0
},
b: {
name : "B",
score : 0
},
message: "",
max_score: 0,
tw: 0,
move: {
type: "", // x or y
x: "", // coord x
y: "" // coord y
}
};
game.message = "Loading";
//game.message = "It's " + game.a.name + "'s turn";
update();
$("#log").hide();
$("#asc").removeClass("turn");
$("#bsc").removeClass("turn");
$("#asc").addClass("turn");
}
function update() {
console.log(game);
$("#aturn").val(game.turn);
$("#message").html(game.message);
$("#asc").html(game.a.score);
$("#bsc").html(game.b.score);
$("#aname").html(game.a.name);
$("#bname").html(game.b.name);
}
function reloadpage() {
initial();
promptu();
}
function x(xa,ya, prev_send) {
if (connected == 0)
{
alert("Peer not yet connected");
return false;
}
else if ((game.turn != name) && (prev_send == null))
{
alert("It's your peer's turn!");
return false;
}
var txy="#linex"+xa+""+ya;
if ($(txy).hasClass("line")) {
game.message = "Please choose another line";
} else {
$(txy).addClass("line");
$(txy).removeClass("pline");
checkboxx(xa,ya);
}
update();
game.move.type = "x";
game.move.x = xa;
game.move.y = ya;
if (prev_send == null) {
send();
}
}
function y(xb,yb, prev_send) {
if (connected == 0)
{
alert("Peer not yet connected");
return false;
}
else if ((game.turn != name) && (prev_send == null))
{
alert("It's your peer's turn!");
return false;
}
var sxy="#liney"+xb+""+yb;
if ($(sxy).hasClass("line")) {
game.message = "Please choose another line";
} else {
$(sxy).addClass("line");
$(sxy).removeClass("pline");
checkboxy(xb,yb);
}
update();
game.move.type = "y";
game.move.x = xb;
game.move.y = yb;
if (prev_send == null) {
send();
}
}
function checkboxx(xc,yc) {
var cxy="#linex"+xc+""+(yc-1);
var dxy="#liney"+xc+""+(yc-1);
var exy="#liney"+(xc+1)+""+(yc-1);
var fxy="#linex"+xc+""+(yc+1);
var gxy="#liney"+(xc+1)+""+yc;
var hxy="#liney"+xc+""+yc;
if ((cbox(cxy) && cbox(dxy) && cbox(exy)) || (cbox(fxy) && cbox(gxy) && cbox(hxy))) {
if (cbox(cxy) && cbox(dxy) && cbox(exy)) {
var ixy="#box"+xc+(yc-1);
addColor(ixy);
}
if (cbox(fxy) && cbox(gxy) && cbox(hxy)) {
var ixy="#box"+xc+yc;
addColor(ixy);
}
} else {
invertTurn();
}
}
function checkboxy(xc,yc) {
var c1xy="#liney"+(xc-1)+""+yc;
var d1xy="#linex"+(xc-1)+""+yc;
var e1xy="#linex"+(xc-1)+""+(yc+1);
var f1xy="#liney"+(xc+1)+""+yc;
var g1xy="#linex"+xc+""+yc;
var h1xy="#linex"+xc+""+(yc+1);
if ((cbox(c1xy) && cbox(d1xy) && cbox(e1xy)) || (cbox(f1xy) && cbox(g1xy) && cbox(h1xy))) {
if (cbox(c1xy) && cbox(d1xy) && cbox(e1xy)) {
var i1xy="#box"+(xc-1)+yc;
addColor(i1xy);
}
if (cbox(f1xy) && cbox(g1xy) && cbox(h1xy)) {
var i1xy="#box"+xc+yc;
addColor(i1xy);
}
} else {
invertTurn();
}
}
function cbox(xyz) {
if ($(xyz).hasClass("line")) {
return true;
} else {
return false;
}
}
function addColor(xyz) {
var anam = game.a.name;
var bnam = game.b.name;
var turn = game.turn;
//var score;
if (turn == "a") {
$(xyz).addClass("abox");
game.a.score += 1;
game.message = "Good job! It's " + anam + "'s turn again";
} else if (turn=="b") {
$(xyz).addClass("bbox");
game.b.score += 1;
game.message = "Good job! It's " + bnam + "'s turn again";
}
//$('#message').html(game.message);
update();
var ascore = game.a.score;
var bscore = game.b.score;
var totalscore = ascore-(-bscore);
var maxscore = game.max_score;
if (totalscore >= maxscore) {
var temp = "Game Over: ";
if (ascore > bscore) {
temp += anam + " win! ";
} else if (ascore < bscore) {
temp += bnam + " win! ";
} else { //ascore == bscore
temp += "It's a tie! ";
}
game.message = temp;
update();
alert(temp);
}
}
function invertTurn() {
var anam = game.a.name;
var bnam = game.b.name;
var turn = game.turn;
if (turn=="a") {
$("#asc").removeClass("turn");
$("#bsc").addClass("turn");
turn = "b";
game.message = "It's " + bnam + "'s turn";
} else if (turn=="b") {
$("#bsc").removeClass("turn");
$("#asc").addClass("turn");
turn = "a";
game.message = "It's " + anam + "'s turn";
}
game.turn = turn;
update();
}
function promptu() {
var tw = 3;
var anam = 'A';
if (name == "a") anam = prompt('Input your name','A');
game.a.name = anam;
if (name == "a")
{
tw = 3;
if ((isNaN(tw))||(tw>10)||(tw<1)) {
while ((isNaN(tw))||(tw>10)||(tw<1)) {
tw = prompt('Input table width \n(1 ≤ width ≤ 10)', 3);
}
}
}
game.tw = tw;
var bnam = 'B';//prompt('Input player 2 name','B');
if (name == "b") bnam = prompt('Input your name','B');
game.b.name = bnam;
//send(1);
draw_table(game.tw);
game.max_score = tw*tw;
var sb="Scoreboard<br />"
$('#sboard').html(sb + "("+tw+"×"+tw+")");
$('#aname').html(anam);
$('#bname').html(bnam);
update();
}
function draw_table(tw) {
var maint="";
var ri = 0;
var rj = 0;
for (var i=1;i<=(tw*2+1);i++) {
maint=maint + "<tr>";
rj=0;
if (i%2==1) {
ri=ri+1;
}
for (var j=1;j<=(tw*2+1);j++) {
if (i%2==1) {
if (j%2==1) {
maint=maint+"<td class='sdot game_elem'> </td></td>";
} else {
rj=rj+1;
maint=maint+"<td class='pline hline game_elem' id='linex"+rj+ri+"' onclick='x("+rj+","+ri+")'> </td>";
}
} else {
if (j%2==1) {
rj=rj+1;
maint=maint+"<td class='pline vline game_elem' id='liney"+rj+ri+"' onclick='y("+rj+","+ri+")'> </td>";
} else {
maint=maint+"<td class='box game_elem' id='box"+rj+ri+"'> </td>";
}
}
}
maint=maint+"</tr>";
}
$("#maintable").html(maint);
}