-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
225 lines (195 loc) · 6.4 KB
/
index.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
224
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Dōbutsu shōgi</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript" src="jquery-hacks.js"></script>
<style type='text/css'>
.board {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
cursor:pointer;
}
.capture-player, .capture-opponent {
width: 120px;
height: 40px;
vertical-align: center;
text-align: center;
border: 1px dotted black;
}
.square {
width: 40px;
height: 40px;
vertical-align: center;
text-align: center;
border: 1px dotted rgba(255,0,0,0.2);
}
.legal-move {
border: 1px solid blue;
}
.piece {
font-size: 30px;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
display: inline-block;
}
.opponent {
-webkit-transform: rotate(180deg);
-moz-transform: rotate(180deg);
-o-transform: rotate(180deg);
-ms-transform: rotate(180deg);
transform: rotate(180deg);
}
.row:nth-child(odd) .square:nth-child(even), .row:nth-child(even) .square:nth-child(odd) {
background-color: rgba(0,0,255, 0.1);
}
.row:nth-child(odd) .square:nth-child(odd), .row:nth-child(even) .square:nth-child(even){
background-color: rgba(0,80,200, 0.2);
}
</style>
<script type='text/javascript'>//<![CDATA[
var pieces = {
'pawn' : '🐱',
'rook' : '🐰',
'bishop' : '🐙' ,
'king' : '🐶',
'queen' : '🐯',
}
$(window).load(function(){
function Board(selector) {
$('<tr>').addClass('row').append(
$('<td>').attr('colspan', 3).addClass('capture-opponent').text(' ')
).appendTo(selector)
for (var r = 1; r<=4; r++) {
row = $('<tr>').addClass('row')
for (var c = 1; c<=3; c++) {
var td = $('<td>').addClass('square').addClass('r'+r+'c'+c);
td.prop('pos', [r,c])
row.append(td);
}
$(selector).append(row);
}
$('<tr>').addClass('row').append(
$('<td>').attr('colspan', 3).addClass('capture-player')
).appendTo(selector)
$('.square').droppable({
tolerance: "intersect",
accept: function(ui) {
var sq = $(this).prop('pos');
var pc = $(ui).prop('pos');
var opponent = $(ui).prop('opponent');
var direction = opponent ? +1 : -1;
var name = $(ui).prop('name')
var sq_r=sq[0], sq_c=sq[1];
var pc_r=pc[0], pc_c=pc[1];
var others = $(this).children('.piece')
var ret = false;
if (others.length > 0 && others.prop('opponent') == opponent) {
ret = false;
} else if (pc == "captured" && others.length ==0) {
ret = true;
} else if (Math.abs(sq_c-pc_c) > 1 || Math.abs(sq_r-pc_r) > 1) {
ret == false;
} else if (name == 'pawn') {
ret = (sq_c == pc_c) && ((sq_r == pc_r) || (sq_r == pc_r + direction));
} else if (name == 'queen') {
ret = (sq_r == pc_r) || (sq_r == pc_r + direction) || (sq_c == pc_c);
} else if (name == 'rook') {
ret = (sq_r == pc_r) || (sq_c == pc_c);
} else if (name == 'bishop') {
ret = (sq_r != pc_r) && (sq_c != pc_c);
} else if (name == 'king') {
ret = true;
}
if (ret) {
$(this).addClass('legal-move')
} else {
$(this).removeClass('legal-move')
}
return ret;
},
drop: function(ev, ui) {
var piece = ui.draggable;
var name = piece.prop('name')
var opponent = piece.prop('opponent')
var others = $(this).children('.piece').eq(0);
var pos = piece.prop('pos')
var final_row = opponent ? 4 : 1
if (name == "pawn" && final_row == $(this).prop('pos')[0] && pos != "captured") {
piece.text(pieces["queen"]);
piece.prop('name', "queen");
}
piece.prop('pos', $(this).prop('pos'));
piece.detach().css({top: 0, left: 0});
if (others) {
console.log('taking')
var other = others.detach().css({top: 0, left: 0});
other.prop('opponent', opponent);
if (opponent) {
other.addClass('opponent');
} else {
other.removeClass('opponent');
}
other.prop('pos', 'captured');
if (other.prop('name') == "queen") {
other.text(pieces["pawn"]);
other.prop('name', "pawn");
}
$(opponent?'.capture-opponent':'.capture-player').append(other)
}
piece.appendTo(this);
// promotion, capture, if move actually happened
}
});
}
function setup(board) {
function place(row, col, name, opponent) {
var p = $('<div>').addClass('piece').text(pieces[name]);
p.prop('name', name);
p.prop('opponent', opponent);
p.prop('pos',[row,col]);
if (opponent) {p.addClass('opponent')}
$(board+' .r'+row+'c'+col).append(p)
}
$(board+' .piece').remove();
place(1,1, 'rook', true);
place(1,2,'king', true);
place( 1,3,'bishop', true);
place( 2,2,'pawn', true);
place(4,3,'rook', false);
place(4,2,'king', false);
place(4,1,'bishop', false);
place(3,2,'pawn', false);
$('.piece').draggable({
revert: 'invalid',
snap: "inner",
stack: "div",
// todo: highlight where each piece can move
start: function () {
},
stop: function () {
$('.legal-move').removeClass('legal-move')
},
});
}
Board('.board');
setup('.board');
});//]]>
</script>
</head>
<body>
<table class="board">
</table>
</body>
</html>