-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
316 lines (259 loc) · 10 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>webTablut - Play Tablut Online against your PC!</title>
<meta name="description" content="webTablut - Play Tablut Online against your PC!">
<meta name="author" content="Federico Terzi">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/styles.css?v=1.0">
</head>
<body onload='initialize()'>
<img src="logo.png" class="logo-img"/>
<div id="canvasContainer"></div>
<h3>How to Play</h3>
<p>Click on a white pawn and then select one of the available moves.
In order to win, the white player has to bring the red king to one of the 16 winpoints.</p>
<h3>About</h3>
<p>
webTablut is a <b>very basic experiment</b> to bring the <a href="https://github.com/federico-terzi/osarracino/" target="_blank">O(sarracino) Tablut Engine</a>
to the web using WebAssembly. The source is available on <a href="https://github.com/federico-terzi/webTablut">GitHub</a>.
</p>
<script src="js/pixi.min.js"></script>
<script src="js/scaleToWindow.js"></script>
<script async type="text/javascript" src="js/osarracino.js"></script>
<script>
function initialize() {
// WASM functions
calculate_available_moves = Module.cwrap('calculate_available_moves', null, ['number', 'number', 'number', 'number','number']);
calculate_board = Module.cwrap('calculate_board', 'number', ['number', 'number', 'number', 'number','number', 'number']);
calculate_move = Module.cwrap('calculate_move', null, ['number', 'number', 'number']);
get_fx = Module.cwrap('get_fx', 'number', []);
get_fy = Module.cwrap('get_fy', 'number', []);
get_tx = Module.cwrap('get_tx', 'number', []);
get_ty = Module.cwrap('get_ty', 'number', []);
// Initialize PIXI Graphics
let app = new PIXI.Application({
width: 450, // default: 800
height: 450, // default: 600
antialias: true, // default: false
transparent: false, // default: false
resolution: 1 // default: 1
}
);
document.getElementById("canvasContainer").appendChild(app.view);
// scaleToWindow(app.view);
// window.addEventListener("resize", function(event){
// scaleToWindow(app.view);
// });
// Game configuration
let boardConfig = [[0, 2, 2, 1, 1, 1, 2, 2, 0],
[2, 0, 0, 0, 1, 0, 0, 0, 2],
[2, 0, 0, 0, 0, 0, 0, 0, 2],
[1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 0, 0, 1, 0, 0, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1],
[2, 0, 0, 0, 0, 0, 0, 0, 2],
[2, 0, 0, 0, 1, 0, 0, 0, 2],
[0, 2, 2, 1, 1, 1, 2, 2, 0]];
let initialBoard = [[0,0,0,2,2,2,0,0,0],
[0,0,0,0,2,0,0,0,0],
[0,0,0,0,1,0,0,0,0],
[2,0,0,0,1,0,0,0,2],
[2,2,1,1,3,1,1,2,2],
[2,0,0,0,1,0,0,0,2],
[0,0,0,0,1,0,0,0,0],
[0,0,0,0,2,0,0,0,0],
[0,0,0,2,2,2,0,0,0]];
let boardCells = [[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null, null]];
let boardColorMap = {0: 0x476C9B, 1: 0x273C56, 2: 0x468C99, 3: 0xBAB155};
let pawnColorMap = {1: 0xADD9F4, 2: 0x101419, 3: 0x984447};
// State variables
var board = null;
var pawns = [];
var selectedPawn = null;
var currentMoves = [];
var playerColor = 1;
// Draw the board
function drawCell(g, x, y, color) {
g.clear();
g.lineStyle(2, 0x101419, 1);
g.beginFill(color);
g.drawRect(0, 0, 50, 50);
g.endFill();
g.position.x = x*50;
g.position.y = y*50;
g.boardX = x;
g.boardY = y;
g.buttonMode = true;
g.interactive = true;
g.on('pointerdown', function() {
var found = false;
for (var i in currentMoves) {
if (currentMoves[i].x == this.boardX && currentMoves[i].y == this.boardY) {
found = true;
break;
}
}
if (found) {
makeMove(this.boardX, this.boardY, false);
}
});
}
function drawBoard(b) {
for (var x = 0; x<9; x++) {
for (var y = 0; y<9; y++) {
let color = boardColorMap[b[x][y]];
if (boardCells[x][y] == null) {
let g = new PIXI.Graphics();
boardCells[x][y] = g;
app.stage.addChild(g);
}
drawCell(boardCells[x][y], x, y, color);
}
}
}
function drawPawn(x, y, t) {
let g = new PIXI.Graphics();
g.lineStyle(0); // draw a circle, set the lineStyle to zero so the circle doesn't have an outline
g.beginFill(pawnColorMap[t], 1);
g.drawCircle(0, 0, 20);
g.endFill();
g.position.x = x*50+25;
g.position.y = y*50+25;
g.alpha = 1;
g.buttonMode = true;
g.interactive = true;
g.boardX = x;
g.boardY = y;
g.colorType = t;
g.on('pointerover', function() {
this.position.y = y*50+20;
}).on('pointerout', function() {
this.position.y = y*50+25;
}).on('pointerdown', function() {
if (this.colorType == playerColor || (this.colorType == 3 && playerColor == 1)) {
selectedPawn = this;
currentMoves = calculateAvailableMoves(this);
let newBoard = JSON.parse(JSON.stringify(boardConfig));
for (var i in currentMoves) {
newBoard[currentMoves[i].x][currentMoves[i].y] = 3;
}
drawBoard(newBoard);
}else{
selectedPawn = null;
currentMoves = [];
drawBoard(boardConfig);
}
});
return g;
}
function renderPawns() {
for (let i in pawns) {
app.stage.removeChild(pawns[i]);
}
pawns = [];
for (var x = 0; x<9; x++) {
for (var y = 0; y<9; y++) {
if (board[x][y] != 0) {
let g = drawPawn(x,y,board[x][y]);
app.stage.addChild(g);
pawns.push(g);
}
}
}
}
// Interop functions
function convertBoardToNative() {
var arr = new Int32Array(81);
for (let x = 0; x<9; x++) {
for (let y = 0; y<9; y++) {
arr[x+y*9] = board[x][y];
}
}
return arr;
}
function calculateAvailableMoves(pawn) {
let nativeBoard = convertBoardToNative();
var boardPtr = Module._malloc(nativeBoard.length * nativeBoard.BYTES_PER_ELEMENT);
var outputPtr = Module._malloc(100 * 4);
Module.HEAP32.set(nativeBoard, boardPtr/nativeBoard.BYTES_PER_ELEMENT);
calculate_available_moves(boardPtr, playerColor == 1, outputPtr, pawn.boardX, pawn.boardY);
let buffer = new Int32Array(Module.HEAP32.buffer, outputPtr, 100);
let moves = [];
var current = 0;
while(buffer[current] > -1) {
moves.push({x: buffer[current], y: buffer[current+1]});
current+=2;
}
Module._free(boardPtr);
Module._free(outputPtr);
return moves;
}
function makeMove(targetX, targetY, enemy) {
let nativeBoard = convertBoardToNative();
var boardPtr = Module._malloc(nativeBoard.length * nativeBoard.BYTES_PER_ELEMENT);
var outputPtr = Module._malloc(81 * 4);
Module.HEAP32.set(nativeBoard, boardPtr/nativeBoard.BYTES_PER_ELEMENT);
let result = calculate_board(boardPtr, outputPtr, selectedPawn.boardX, selectedPawn.boardY, targetX, targetY);
let buffer = new Int32Array(Module.HEAP32.buffer, outputPtr, 81);
for (let x = 0; x<9; x++) {
for (let y=0; y<9; y++) {
board[x][y] = buffer[x*9+y];
}
}
Module._free(boardPtr);
Module._free(outputPtr);
selectedPawn = null;
currentMoves = [];
renderPawns();
drawBoard(boardConfig);
if (result != 0) {
if (result == 1) {
alert("White WIN!");
}else{
alert("Black WIN!");
}
resetGame();
return;
}
if (!enemy) {
setTimeout(function() {
calculateEnemyMove();
}, 100);
}
}
function calculateEnemyMove() {
let nativeBoard = convertBoardToNative();
var boardPtr = Module._malloc(nativeBoard.length * nativeBoard.BYTES_PER_ELEMENT);
Module.HEAP32.set(nativeBoard, boardPtr/nativeBoard.BYTES_PER_ELEMENT);
calculate_move(boardPtr, playerColor != 1, 1);
var fx = get_fx();
var fy = get_fy();
var tx = get_tx();
var ty = get_ty();
selectedPawn = {boardX: fx, boardY: fy};
Module._free(boardPtr);
makeMove(tx, ty, true);
}
// Game mechanics
function resetGame() {
board = JSON.parse(JSON.stringify(initialBoard));
selectedPawn = null;
currentMoves = [];
drawBoard(boardConfig);
renderPawns();
}
resetGame();
};
</script>
</body>
</html>