]> git.cworth.org Git - lmno-server/blob - tictactoe.js
Generalize the handling of the /move request
[lmno-server] / tictactoe.js
1 const express = require("express");
2 const Game = require("./game.js");
3
4 const router = express.Router();
5
6 class TicTacToe extends Game {
7   constructor(id) {
8     super(id);
9     this.state = {
10       moves: [],
11       board: Array(9).fill(""),
12       next_player: "X",
13     };
14   }
15
16   /* Returns Boolean indicating whether move was legal and added. */
17   add_move(square) {
18     /* Cannot move to an occupied square. */
19     if (this.state.board[square])
20       return false;
21
22     this.state.board[square] = this.state.next_player;
23     this.state.moves.push(square);
24
25     if (this.state.next_player === "X")
26       this.state.next_player = "O";
27     else
28       this.state.next_player = "X";
29
30     return true;
31   }
32 }
33
34 exports.router = router;
35 exports.Game = TicTacToe;
36
37 TicTacToe.meta = {
38   name: "Tic Tac Toe",
39   identifier: "tictactoe"
40 };