File size: 15,868 Bytes
7fd09d7
 
a1fbef0
 
80d213e
 
12a6324
 
80d213e
 
 
 
 
 
 
 
 
fc163a4
 
80d213e
23a091d
80d213e
64bc0b8
 
 
 
fc163a4
80d213e
 
 
 
 
 
 
fc163a4
80d213e
 
 
 
 
 
fc163a4
80d213e
 
 
 
fc163a4
 
 
 
64bc0b8
879c231
 
fc163a4
018fff2
80d213e
 
fc163a4
80d213e
fc163a4
 
80d213e
64bc0b8
80d213e
 
 
fc163a4
 
 
64bc0b8
80d213e
fc163a4
64bc0b8
 
80d213e
 
fc163a4
80d213e
64bc0b8
80d213e
64bc0b8
fc163a4
80d213e
 
 
fc163a4
80d213e
 
 
 
 
 
 
 
64bc0b8
 
 
 
 
 
 
80d213e
 
a1fbef0
 
 
80d213e
 
 
 
fc163a4
80d213e
 
a1fbef0
fc163a4
 
c9a50d4
 
fc163a4
a1fbef0
 
 
 
fc163a4
80d213e
fc163a4
80d213e
fc163a4
 
80d213e
 
fc163a4
80d213e
 
fc163a4
 
 
80d213e
fc163a4
 
 
 
80d213e
fc163a4
 
80d213e
fc163a4
 
 
 
 
80d213e
 
a1fbef0
 
 
 
 
 
 
 
 
fc163a4
29e10a0
 
fc163a4
29e10a0
3500001
29e10a0
 
 
8227ece
744c3ab
29e10a0
ed300ef
29e10a0
 
 
 
 
 
 
 
 
 
 
 
a1fbef0
29e10a0
64bc0b8
7bf1b05
29e10a0
7542fdd
 
e34beee
7542fdd
64bc0b8
7542fdd
e610563
7542fdd
 
 
a1fbef0
 
fc163a4
a1fbef0
29e10a0
a1fbef0
 
 
 
 
 
 
e02578c
a1fbef0
 
 
018fff2
a1fbef0
 
 
80d213e
 
64bc0b8
a1fbef0
fc163a4
 
 
 
 
80d213e
fc163a4
a1fbef0
 
 
 
29e10a0
a1fbef0
 
 
 
fc163a4
 
a1fbef0
fc163a4
a1fbef0
fc163a4
a1fbef0
e02578c
a1fbef0
 
018fff2
a1fbef0
fc163a4
 
80d213e
fc163a4
80d213e
 
 
 
fc163a4
80d213e
 
 
 
 
 
 
 
a1fbef0
80d213e
fc163a4
 
a1fbef0
fc163a4
a1fbef0
 
 
 
 
 
fc163a4
a1fbef0
 
 
 
 
 
 
 
 
 
 
64bc0b8
fc163a4
80d213e
 
 
fc163a4
a1fbef0
 
 
64bc0b8
a1fbef0
 
 
 
 
 
 
 
80d213e
a1fbef0
 
 
 
fc163a4
a1fbef0
 
64bc0b8
a1fbef0
 
 
 
 
 
 
 
 
 
fc163a4
80d213e
a1fbef0
 
80d213e
fc163a4
80d213e
 
 
 
 
 
 
 
64bc0b8
80d213e
 
 
64bc0b8
80d213e
 
64bc0b8
80d213e
 
a1fbef0
 
 
64bc0b8
80d213e
 
 
 
64bc0b8
a1fbef0
80d213e
fc163a4
80d213e
fc163a4
879c231
 
fc163a4
 
 
 
 
64bc0b8
80d213e
29e10a0
7542fdd
eebe216
7542fdd
3e05545
a1fbef0
7542fdd
 
fc163a4
80d213e
 
 
 
64bc0b8
7542fdd
a1fbef0
 
 
 
 
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<!DOCTYPE html>
<html>

<head>
    <title>Connect-4</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm/dist/tf-backend-wasm.js"></script>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            margin: 0;
            background-color: #f5f5f5; /* Very Light Gray */
            overflow: hidden;
        }

        #game-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 100%;
            max-width: 600px;
        }

        #game-title {
            font-size: 2em;
            font-weight: bold;
            margin-bottom: 0.5em;
            text-align: center;
            color: #444; /* Darker Gray */
            user-select: none;
        }

        #game-instructions {
            text-align: center;
            margin-bottom: 1em;
            color: #666; /* Medium Gray */
            user-select: none;
        }

        #game-board {
            border-radius: 8px; /* Slightly Rounded Corners */
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Very Subtle Shadow */
            border: 1px solid #ddd; /* Light Gray Border */
            background-color: #fff; /* White Background */
            touch-action: none;
        }

        #ai-first {
            margin-top: 1em;
            padding: 10px 20px;
            font-size: 1em;
            border: 1px solid #ccc; /* Light Gray Border */
            border-radius: 5px;
            background-color: #f9f9f9; /* Almost White */
            color: #555; /* Dark Gray Text */
            cursor: pointer;
            transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
            user-select: none;
        }

        #ai-first:hover {
            background-color: #eee; /* Slightly Darker on Hover */
            border-color: #bbb; /* Darker Border on Hover */
            color: #333;
        }
        #ai-first:active {
            transform: translateY(1px);
            background-color: #ddd;
        }

        /* Loading indicator */
        #loading-indicator {
            display: none;
            margin-top: 20px;
            border: 6px solid #f3f3f3;
            border-top: 6px solid #999; /* Medium Gray */
            border-radius: 50%;
            width: 50px;
            height: 50px;
            animation: spin 1s linear infinite; /* Faster Spin */
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        @media (max-width: 600px) {
            #game-title {
                font-size: 1.5em;
            }

            #game-instructions {
                font-size: 0.9em;
            }
        }
    </style>
</head>

<body>
    <div id="game-container">
        <h1 id="game-title">Connect Four</h1>
        <p id="game-instructions">Try to connect four stones in a row, a column, or a diagonal.</p>
        <canvas id="game-board"></canvas>
        <button type="button" id="ai-first">AI goes first</button>
        <div id="loading-indicator"></div>
    </div>
    <script>
        // ... (rest of your JavaScript code, with changes below) ...

        function BoardGame(agent, num_rows, num_cols) {
            this.agent = agent;
            this.audio = new Audio('/stone.ogg'); //  Consider a more subtle sound.
            this.num_cols = num_cols;
            this.num_rows = num_rows;
            var this_ = this;
            this.canvas_ctx = document.getElementById("game-board").getContext("2d");
            this.board_scale = 1; // Initial scale

            this.calculateBoardScale = function() {
                const containerWidth = document.getElementById("game-container").offsetWidth;
                const containerHeight = window.innerHeight - document.getElementById("game-instructions").offsetHeight
                                        - document.getElementById("ai-first").offsetHeight - 80;


                const canvasWidth = containerWidth * 0.95; // Use 95% of container width
                const canvasHeight = canvasWidth * (num_rows + 1) / (num_cols + 1);

                if(canvasHeight > containerHeight) {
                    const adjustedCanvasHeight = containerHeight * 0.9;
                    const adjustedCanvasWidth = adjustedCanvasHeight * (num_cols+1) / (num_rows+1);
                    this.board_scale = adjustedCanvasHeight / (num_rows + 1);
                    this_.canvas_ctx.canvas.width = adjustedCanvasWidth;
                    this_.canvas_ctx.canvas.height = adjustedCanvasHeight;
                }
                else{
                    this.board_scale = canvasWidth / (num_cols + 1);
                    this_.canvas_ctx.canvas.width = canvasWidth;
                    this_.canvas_ctx.canvas.height = canvasHeight;
                }
                this_.canvas_ctx.setTransform(1, 0, 0, 1, 0, 0);  // Reset transform
                this_.canvas_ctx.scale(this.board_scale, this.board_scale);
                this_.canvas_ctx.translate(0.5, 0.5);
            }
             this.calculateBoardScale();


            this.reset = function () {
                this.board = new Array(num_rows * num_cols);
                for (let i = 0; i < this.board.length; i++) this.board[i] = 0;
                this.mouse_x = -1;
                this.mouse_y = -1;
                this.who_play = 1;
                this.ai_player = -1;
                this.game_ended = false;
            };
            this.reset();
            this.get = function (row, col) {
                return this.board[this.num_cols * row + col];
            }
            this.is_terminated = function () {
                if (this.board.some((x) => x == 0) == false) return true;
                for (let i = 0; i < this.num_rows; i++) {
                    for (let j = 0; j < this.num_cols; j++) {
                        var p = this.get(i, j);
                        if (p == 0) continue;
                        for (let [dx, dy] of [[1, 0], [0, 1], [1, 1], [-1, 1]]) {
                            var count = 0;
                            for (let k = 0; k < 4; k++) {
                                const u = i + dx * k;
                                const v = j + dy * k;
                                if (u < 0 || u >= this.num_rows) break;
                                if (v < 0 || v >= this.num_cols) break;
                                if (this.get(u, v) != p) break;
                                count = count + 1;
                            }
                            if (count >= 4) return true;
                        }
                    }
                }
                return false;
            };
            this.submit_board = async function () {
                document.getElementById("loading-indicator").style.display = "block";
                await new Promise(r => setTimeout(r, 1000));
                if (this_.is_terminated()) return { "terminated": true, "action": -1 };
                const obs = tf.tensor(this_.board, [this_.num_rows, this_.num_cols], 'float32');
                const normalized_obs = tf.mul(obs, this_.ai_player);
                const [action_logits, value] = this_.agent.execute(normalized_obs, ["output_0", "output_1"]);
                const action = await tf.argMax(action_logits).array();
                document.getElementById("loading-indicator").style.display = "none";
                return {
                    "terminated": false,
                    "action": action,
                };
            };
            this.end_game = function () {
                this.game_ended = true;
                setTimeout(function () { this_.reset(); this_.render(); }, 3000);
            };
            this.ai_play = function () {
                this_.submit_board().then(
                    function (info) {
                        let x = info["action"];
                        if (x != -1) {
                            let [_, y] = this_.get_candidate(x);
                            let i = y * this_.num_cols + x;
                            this_.board[i] = this_.who_play;
                            this_.audio.play();
                            this_.who_play = -this_.who_play;
                            this_.render();
                        }
                        if (this_.is_terminated() == true) {
                            this_.end_game();
                        }
                    }
                ).catch(function (e) {
                    console.error("AI play error:", e);
                });
            };
            document.getElementById("ai-first").onclick = function () {
                this_.reset();
                this_.ai_player = 1;
                this_.ai_play();
            };

            this.handleClick = function(x, y) {
                var loc_x = Math.floor(x / this_.board_scale - 0.5);
                var loc_y = Math.floor(y / this_.board_scale - 0.5);
                this_.mouse_x = loc_x;
                this_.mouse_y = this_.get_candidate(this_.mouse_x)[1];

                if (
                    this_.mouse_x >= 0 &&
                    this_.mouse_y >= 0 &&
                    this_.mouse_x < this_.num_cols &&
                    this_.mouse_y < this_.num_rows &&
                    this_.game_ended == false
                ) {
                    if (this_.who_play == this_.ai_player) return false;
                    let i = this_.mouse_y * this_.num_cols + this_.mouse_x;
                    if (this_.board[i] != 0) return false;
                    this_.board[i] = this_.who_play;
                    this_.audio.play();
                    this_.who_play = -this_.who_play;
                    this_.render();
                    this_.ai_play();
                }
            }

            document.getElementById("game-board").addEventListener('touchstart', function(e) {
                e.preventDefault();
                var rect = this.getBoundingClientRect();
                var touch = e.touches[0];
                var x = touch.clientX - rect.left;
                var y = touch.clientY - rect.top;
                this_.handleClick(x, y);
            }, false);


            document.getElementById("game-board").addEventListener('click', function (e) {
                var rect = this.getBoundingClientRect();
                var x = e.clientX - rect.left;
                var y = e.clientY - rect.top;
                this_.handleClick(x,y);
            }, false);



            this.draw_stone = function (x, y, color) {
                let ctx = this.canvas_ctx;
                y = this.num_rows - 1 - y;
                ctx.beginPath();
                ctx.arc(x, y, 0.40, 0, 2 * Math.PI, false);
                ctx.fillStyle = color;
                ctx.fill();
                ctx.lineWidth = 0.02;
                ctx.strokeStyle = "rgba(0, 0, 0, 0.1)"; // Very Light Border
                ctx.stroke();
            };
            this.get_candidate = function (x) {
                for (let i = 0; i < this.num_rows; i++) {
                    let idx = i * this.num_cols + x;
                    if (this.board[idx] == 0) return [x, i];
                }
                return [-1, -1];
            };
            this.render = function () {
                let ctx = this.canvas_ctx;
                ctx.clearRect(-1, -1, num_cols + 1, num_rows + 1);
                ctx.fillStyle = "#fff";  // White board
                ctx.fillRect(-0.5, -0.5, num_cols, num_rows);

                ctx.lineWidth = 0.1 / 2;
                ctx.strokeStyle = "rgba(0, 0, 0, 0.1)"; // Very subtle grid lines
                for (let i = 0; i < this.num_cols; i++) {
                    ctx.beginPath();
                    ctx.moveTo(i, 0);
                    ctx.lineTo(i, this.num_rows-1);
                    ctx.stroke();
                }
                for (let i = 0; i < this.num_rows; i++) {
                    ctx.beginPath();
                    ctx.moveTo(0, i);
                    ctx.lineTo(this.num_cols - 1, i);
                    ctx.stroke();
                }

                for (let i = 0; i < this.board.length; i++) {
                    let x = i % this.num_cols;
                    let y = Math.floor(i / this.num_cols);
                    if (this.board[i] == 0) continue;
                    let color = (this.board[i] == 1) ? "#555" : "#bbb"; // Dark Gray and Light Gray
                    this.draw_stone(x, y, color);
                }

                if (
                    this.mouse_x >= 0 &&
                    this.mouse_y >= 0 &&
                    this.mouse_x < this.num_cols &&
                    this.mouse_y < this.num_rows
                ) {
                    let [x, y] = this.get_candidate(this.mouse_x);
                    if (x == -1) return;
                    this.mouse_x = x;
                    this.mouse_y = y;
                    let previewColor = (this.who_play == -1) ? "rgba(187, 187, 187, 0.5)" : "rgba(85, 85, 85, 0.5)"; // Semi-transparent grays
                    this.draw_stone(x, y, previewColor);
                }
            };

            this.handleMove = function(e) {
              let rect = this.canvas_ctx.canvas.getBoundingClientRect();
              let x, y;

              if (e.type === 'mousemove') {
                x = e.clientX - rect.left;
                y = e.clientY - rect.top;
              } else if (e.type === 'touchmove') {
                e.preventDefault();
                if (e.touches.length > 0) {
                  x = e.touches[0].clientX - rect.left;
                  y = e.touches[0].clientY - rect.top;
                } else {
                  return;
                }
              } else {
                return;
              }

                var loc_x = Math.floor(x / this_.board_scale - 0.5);
                var loc_y = Math.floor(y / this_.board_scale - 0.5);
                this_.mouse_x = loc_x;
                this_.mouse_y = loc_y;
                this_.render();
            }

            document.getElementById("game-board").onmousemove = function (e) {
                this_.handleMove(e);
            };

            document.getElementById("game-board").addEventListener('touchmove', function(e) {
                this_.handleMove(e);
            }, false);


            window.addEventListener('resize', function() {
                this_.calculateBoardScale();
                this_.render();
            });
        };
        const modelUrl = '/model.json';

        const init_fn = async function () {
            await tf.setBackend('wasm');
            const model = await tf.loadGraphModel(modelUrl);
            return model;
        };
        document.addEventListener("DOMContentLoaded", function (event) {
            init_fn().then(function (agent) {
                game = new BoardGame(agent, 6, 7);
                game.render();

            }).catch(error => {
                console.error("Error loading model:", error);
                document.getElementById("game-instructions").textContent = "Failed to load the AI. Please try refreshing the page.";
                document.getElementById("ai-first").disabled = true;
            });
        });
    </script>
</body>

</html>