1 const express = require("express");
2 const Game = require("./game.js");
4 const engine_name = "tictactoe";
6 const router = express.Router();
8 class TicTacToe extends Game {
12 this.board = Array(9).fill(null);
15 /* Returns Boolean indicating whether move was legal and added. */
17 /* Cannot move to an occupied square. */
18 if (this.board[square])
21 this.board[square] = 'X';
22 this.moves.push(square);
27 broadcast_move(square) {
28 this.broadcast_event("move", square);
32 router.get('/', (request, response) => {
33 const game = request.game;
35 if (! request.session.nickname)
36 response.render('choose-nickname.html', { game_name: "Tic Tac Toe" });
38 response.render('tictactoe-game.html');
41 router.post('/move', (request, response) => {
42 const game = request.game;
43 const square = request.body.square;
45 const legal = game.add_move(square);
47 /* Inform this client whether the move was legal. */
48 response.send(JSON.stringify(legal));
50 /* And only if legal, inform all clients. */
54 game.broadcast_move(square);
57 router.get('/events', (request, response) => {
58 const game = request.game;
60 game.handle_events(request, response);
63 exports.router = router;
64 exports.name = engine_name;
65 exports.Game = TicTacToe;