-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
227 lines (201 loc) · 6.13 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
225
226
227
<!doctype html>
<html>
<head>
<style>
.piece {
display: block;
position: absolute;
border: 3px solid black;
border-radius: 50%;
text-align: center;
}
.size3 {
width: 44px;
height: 44px;
}
.size2 {
width: 34px;
height: 34px;
}
.size1 {
width: 24px;
height: 24px;
}
.size0 {
width: 14px;
height: 14px;
}
.team0 {
background-color: rgb(49, 49, 49);
}
.team1 {
background-color: #f3f6ff;
}
table {
border-collapse: collapse;
}
td, li {
display: inline-flex;
align-items: center;
justify-content: center;
position: relative;
width: 64px;
height: 64px;
border: 1px solid black;
}
.dragging {
border-color: blue;
opacity: 0.5;
}
</style>
</head>
<body>
</body>
<script>
'use strict';
(function gorblet() {
let board = Uint8Array.from(atob('AAAAAAAAAAAAAAAAAAAAAA8PD////w=='), c => c.charCodeAt(0));
// let board = Uint8Array.from(atob('ACoAAAgIAAAAzAAAAAAAAA8PD////w=='), c => c.charCodeAt(0));
let activeDrag = null;
let myTeam = 0; // I'm playing black
let whoseTurn = 0; // it's black's turn
class Piece {
constructor(size, isWhite) {
this.size = size;
this.white = isWhite;
}
}
function piecesAt(index) {
const square = board[index];
const result = [];
for (let size = 0; size <= 3; size++) {
if ((square & (1 << size)) !== 0) {
let isWhiteIndex = size + 4;
result.push(new Piece(size, (square & 1 << isWhiteIndex) !== 0));
}
}
return result;
}
let id = 0;
function createHome() {
const ul = document.createElement('ul');
for (let i = 0; i < 3; i++) {
let li = document.createElement('li');
li.classList.add('external')
let newId = id++;
li.id = 's' + newId;
li.append(...piecesAt(newId).map((piece) => {
let span = document.createElement('span');
span.classList.add('piece', 'size' + piece.size, piece.white ? 'team1' : 'team0');
span.size = piece.size;
span.draggable = true;
span.addEventListener('dragstart', (event) => {
let piece = event.currentTarget;
let err = null;
if (whoseTurn === -1) {
err = 'The game is over';
} else if (!piece.classList.contains('team' + myTeam)) {
err = 'You cannot move the other player\s piece';
} else if (whoseTurn !== myTeam) {
err = 'It\'s not your turn yet...';
} else if (activeDrag && piece !== activeDrag) {
err = 'You must finish the move you have already started.';
}
if (err) {
alert(err);
return event.preventDefault();
}
console.log('Beginning move', piece);
activeDrag = piece;
activeDrag.classList.add('dragging');
})
return span;
}))
ul.append(li)
}
return ul;
}
const table = document.createElement('table');
function canCaptureFromExternal(destination) {
return false; // FIX! if destination is part of an opponent's three-in-a-row it is allowed
}
function winCondition(lastSquare, direction, white, count) {
if (count === 4) {
return true;
}
let nextSquare = lastSquare + direction;
let nextPiece = piecesAt(nextSquare).pop();
if (nextPiece && nextPiece.white === white) {
return winCondition(nextSquare, direction, white, count + 1);
} else {
return false;
}
}
for (let r = 0; r < 4; r++) {
let tr = document.createElement('tr');
for (let c = 0; c < 4; c++) {
let td = document.createElement('td');
let newIndex = id++;
td.id = '' + newIndex;
td.addEventListener('dragover', (ev) => {
ev.preventDefault();
})
td.addEventListener('drop', (ev) => {
ev.preventDefault();
let destination = ev.currentTarget;
let err = null;
if (!activeDrag) {
err = 'No active move';
} else if (activeDrag.parentElement === destination) {
err = 'You cannot move a piece to its own square';
} else {
let existingPieces = piecesAt(parseInt(destination.id));
if (existingPieces.length && activeDrag.parentElement.classList.contains('external') && !canCaptureFromExternal(destination)) {
err = 'You may only capture with pieces already on the board (unless capturing to prevent an imminent loss)';
} else if (existingPieces.length && existingPieces[existingPieces.length - 1].size >= activeDrag.size) {
err = 'You can only capture smaller pieces';
}
}
if (err) {
return alert(err);
}
console.log('Dropped', activeDrag, 'to ' + td.id);
// remove from previous square
board[parseInt(activeDrag.parentElement.id)] ^= (1 << activeDrag.size);
board[parseInt(activeDrag.parentElement.id)] &= ~(1 << (activeDrag.size + 4));
destination.append(activeDrag);
board[parseInt(activeDrag.parentElement.id)] |= (1 << activeDrag.size);
if (activeDrag.classList.contains('team1')) { // set the color bit
board[parseInt(activeDrag.parentElement.id)] |= (1 << (activeDrag.size + 4));
}
activeDrag.classList.remove('dragging');
activeDrag = null;
whoseTurn = whoseTurn ? 0 : 1;
myTeam = myTeam ? 0 : 1; // FIX!!! take out in multiplayer mode
console.log(board);
// console.log(btoa(String.fromCharCode.apply(null, board)));
// check for win
let pairs = [
[0, 4], [1, 4], [2, 4], [3, 4], [0, 1], [4, 1], [8, 1], [12, 1], [0, 5], [3, 3]
];
for (let i = 0; i < pairs.length; i++) {
let eachPair = pairs[i];
let maybePiece = piecesAt(eachPair[0]).pop();
if (maybePiece && winCondition(eachPair[0], eachPair[1], maybePiece.white, 1)) {
whoseTurn = -1;
alert('Game over!');
}
}
});
tr.append(td);
}
table.append(tr);
}
document.body.append(table);
// black home
document.body.append(createHome());
// white home
document.body.append(createHome());
})();
</script>
</html>