]> git.cworth.org Git - lmno-server/blob - tictactoe.js
Add some autofocus attributes to several forms
[lmno-server] / tictactoe.js
1 const express = require("express");
2 const Game = require("./game.js");
3
4 class TicTacToe extends Game {
5   constructor(id) {
6     super(id);
7     this.teams = [
8       {
9         id: 0,
10         name: "X"
11       },
12       {
13         id: 1,
14         name: "O"
15       }];
16     this.state = {
17       moves: [],
18       board: Array(9).fill(""),
19       team_to_play: this.teams[0],
20     };
21   }
22
23   /* Returns true if move was legal and added, false otherwise. */
24   add_move(player, square) {
25
26     const state = this.state;
27     const result = super.add_move(player, square);
28
29     /* If the generic Game class can reject this move, then we don't
30      * need to look at it any further. */
31     if (! result.legal)
32       return result;
33
34     /* Cannot move to an occupied square. */
35     if (state.board[square])
36     {
37       return { legal: false,
38                message: "Square is already occupied" };
39     }
40
41     state.board[square] = state.team_to_play;
42     state.moves.push(square);
43
44     if (state.team_to_play.id === 0)
45       state.team_to_play = this.teams[1];
46     else
47       state.team_to_play = this.teams[0];
48
49     return { legal: true };
50   }
51 }
52
53 TicTacToe.router = express.Router();
54
55 TicTacToe.meta = {
56   name: "Tic Tac Toe",
57   identifier: "tictactoe",
58   options: {
59     allow_guest: true
60   }
61 };
62
63 exports.Game = TicTacToe;