]> git.cworth.org Git - lmno-server/blob - tictactoe.js
Rename "next_player" property to "team_to_play"
[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       team_to_play: "X",
11     };
12     this.teams = ["X", "O"];
13   }
14
15   /* Returns true if move was legal and added, false otherwise. */
16   add_move(player, square) {
17
18     /* Cannot move if you are not on a team. */
19     if (player.team === "")
20     {
21       return { legal: false,
22                message: "You must be on a team to take a turn" };
23     }
24
25     /* Cannot move if it's not this player's team's turn. */
26     if (player.team !== this.state.team_to_play)
27     {
28       return { legal: false,
29                message: "It's not your turn to move" };
30     }
31
32     /* Cannot move to an occupied square. */
33     if (this.state.board[square])
34     {
35       return { legal: false,
36                message: "Square is already occupied" };
37     }
38
39     this.state.board[square] = this.state.team_to_play;
40     this.state.moves.push(square);
41
42     if (this.state.team_to_play === "X")
43       this.state.team_to_play = "O";
44     else
45       this.state.team_to_play = "X";
46
47     return { legal: true };
48   }
49 }
50
51 TicTacToe.router = express.Router();
52
53 TicTacToe.meta = {
54   name: "Tic Tac Toe",
55   identifier: "tictactoe"
56 };
57
58 exports.Game = TicTacToe;