File size: 8,541 Bytes
7fd09d7
 
a1fbef0
 
 
12a6324
 
23a091d
a1fbef0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7542fdd
 
 
 
 
 
 
 
 
 
 
 
a1fbef0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7542fdd
 
 
 
 
3e05545
a1fbef0
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
<!DOCTYPE html>
<html>

<head>
    <title>Connect-4 game</title>
    <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>

</head>

<body>
    <p style="user-select: none; font-family: sans-serif;">
        Try to connect four stones in a row, a column, or a diagonal.
    </p>
    <p><button type="button" style="user-select: none;" id="ai-first">AI goes first</button></p>
    <canvas id="game-board" width="600px" height="600px"></canvas>
    <script>
        function BoardGame(num_rows, num_cols) {
            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 = 30;
            document.getElementById("game-board").height = this.board_scale * (num_rows + 1);
            document.getElementById("game-board").width = this.board_scale * (num_cols + 1);
            this.canvas_ctx.scale(this.board_scale, this.board_scale);
            this.canvas_ctx.translate(1, 1);
            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.render_text = function (text) {
                this.canvas_ctx.fillStyle = "black";
                this.canvas_ctx.font = '1px monospace';
                this.canvas_ctx.fillText(text, 1, 1);
            };
            this.submit_board = async function (action_) {
                const obs = tf.tensor(this_.board, [this_.num_rows, this_.num_cols], 'float32');
                const normalized_obs = tf.mul(obs, this_.ai_player);
                normalized_obs.print();
                const [action_logits, value] = this_.agent.predict(normalized_obs);
                action_logits.print();
                const action = await tf.argMax(action_logits).array();
                return {
                    "terminated": false,
                    "action": action,
                };
            };
            this.end_game = function () {
                this.game_ended = true;
                setTimeout(function() {this_.reset();}, 3000);
            };
            this.ai_play = function() {
                this_.submit_board().then(
                    function (info) {
                        document.body.style.cursor = 'default';
                        console.log(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_.who_play = -this_.who_play;
                            this_.render();
                        }
                        if (info["terminated"] == true) {
                            this_.end_game();
                        }
                    }
                ).catch(function (e) { });            
            };
            document.getElementById("ai-first").onclick = function() {
                this_.reset();
                this_.ai_player = 1;
                this_.ai_play();                
            };
            document.getElementById("game-board").addEventListener('click', function (e) {
                var rect = this.getBoundingClientRect();
                var x = e.clientX - rect.left;
                var y = e.clientY - rect.top;
                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_.who_play = -this_.who_play;
                    this_.render();
                    this_.ai_play();
                }
            }, 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 = "black";
                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.fillStyle = "#b8891c";
                ctx.fillRect(-1, -1, num_cols + 1, num_rows + 1);
                ctx.fillStyle = "#b8891c";
                ctx.fillRect(0, 0, num_cols - 1, num_rows - 1);
                ctx.lineWidth = 0.1 / 5;
                for (let i = 0; i < this.num_cols; i++) {
                    ctx.beginPath();
                    ctx.moveTo(i, 0);
                    ctx.lineTo(i, this.num_rows - 1);
                    ctx.strokeStyle = "black";
                    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.strokeStyle = "black";
                    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) ? "#3a352d" : "#f0ece4";
                    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;
                    if (this.who_play == -1)
                        this.draw_stone(x, y, "#bda051");
                    else
                        this.draw_stone(x, y, "#6d6051");
                }
            };
            document.getElementById("game-board").onmousemove = function (e) {
                // important: correct mouse position:
                var rect = this.getBoundingClientRect();
                var x = e.clientX - rect.left;
                var y = e.clientY - rect.top;
                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;
                setTimeout(function () { this_.render(); });
            };
        };
        const modelUrl = 'policy/model.json';
        const init_fn = async function() {
            const model = await tf.loadGraphModel(modelUrl);
            await tf.setBackend('wasm');
            return model;
        };
        document.addEventListener("DOMContentLoaded", function (event) {
            init_fn().then(function (agent) {
                game = new BoardGame(agent, 6, 7);
                game.render();                
            });
        });
    </script>
</body>

</html>