1 const express = require("express");
2 const Game = require("./game.js");
4 const router = express.Router();
6 class TicTacToe extends Game {
11 board: Array(9).fill(""),
16 /* Returns Boolean indicating whether move was legal and added. */
18 /* Cannot move to an occupied square. */
19 if (this.state.board[square])
22 this.state.board[square] = this.state.next_player;
23 this.state.moves.push(square);
25 if (this.state.next_player === "X")
26 this.state.next_player = "O";
28 this.state.next_player = "X";
33 broadcast_move(square) {
34 this.broadcast_event("move", square);
38 router.post('/move', (request, response) => {
39 const game = request.game;
40 const square = request.body.square;
42 const legal = game.add_move(square);
44 /* Inform this client whether the move was legal. */
45 response.send(JSON.stringify(legal));
47 /* And only if legal, inform all clients. */
51 game.broadcast_move(square);
54 exports.router = router;
55 exports.Game = TicTacToe;
59 identifier: "tictactoe"