]> git.cworth.org Git - lmno-server/blob - tictactoe.js
tictactoe: Reword the comment describing the add_move method
[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   }
13
14   /* Returns true if move was legal and added, false otherwise. */
15   add_move(square) {
16     /* Cannot move to an occupied square. */
17     if (this.state.board[square])
18       return false;
19
20     this.state.board[square] = this.state.next_player;
21     this.state.moves.push(square);
22
23     if (this.state.next_player === "X")
24       this.state.next_player = "O";
25     else
26       this.state.next_player = "X";
27
28     return true;
29   }
30 }
31
32 TicTacToe.router = express.Router();
33
34 TicTacToe.meta = {
35   name: "Tic Tac Toe",
36   identifier: "tictactoe"
37 };
38
39 exports.Game = TicTacToe;