]> git.cworth.org Git - lmno-server/blob - tictactoe.js
game/tictactoe: Expand player to include a team property
[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.state = {
8       moves: [],
9       board: Array(9).fill(""),
10       next_player: "X",
11     };
12     this.teams = ["X", "O"];
13   }
14
15   /* Returns true if move was legal and added, false otherwise. */
16   add_move(square) {
17     /* Cannot move to an occupied square. */
18     if (this.state.board[square])
19       return { legal: false, message: "Square is already occupied" };
20
21     this.state.board[square] = this.state.next_player;
22     this.state.moves.push(square);
23
24     if (this.state.next_player === "X")
25       this.state.next_player = "O";
26     else
27       this.state.next_player = "X";
28
29     return { legal: true };
30   }
31 }
32
33 TicTacToe.router = express.Router();
34
35 TicTacToe.meta = {
36   name: "Tic Tac Toe",
37   identifier: "tictactoe"
38 };
39
40 exports.Game = TicTacToe;