aboutsummaryrefslogtreecommitdiff
path: root/src/game.js
blob: 0820f8cff333f676ce32bc84d51ef8245ea76da7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { getInput } from "./utils.js";

let game = {
  player1: {
    symbol: "X",
    name: undefined,
  },
  player2: {
    symbol: "O",
    name: undefined,
  },
  turn: "player1",
  state: "setup",
  board: [" ", " ", " ", " ", " ", " ", " ", " ", " "],
};

const winningPositions = [
  [0, 1, 2],
  [0, 3, 6],
  [0, 4, 8],
  [1, 4, 7],
  [2, 5, 8],
  [2, 4, 6],
  [3, 4, 5],
  [6, 7, 8],
];

function checkForWin() {
  let winner = "";
  for (let position of winningPositions) {
    if (
      // ignore field empty
      game.board[position[0]] != " " &&
      game.board[position[1]] != " " &&
      game.board[position[2]] != " " &&
      // check if three in a row
      game.board[position[0]] == game.board[position[1]] &&
      game.board[position[1]] == game.board[position[2]]
    ) {
      winner =
        game.player1.symbol == game.board[position[0]]
          ? game.player1.name
          : game.player2.name;
    }
  }
  return winner;
}

async function gameLoop() {
  while (game.state == "running") {
    outputBoard();

    let winner = checkForWin();
    if (winner) {
      console.log(`${winner} has won!`);
      game.state = "finish";
      return;
    }

    await questionLoop();
  }

  async function questionLoop() {
    let playerMove = await getInput(
      `Where would you want to place (${game[game.turn].name})?: `,
    );

    while (game.board[playerMove - 1] != " ") {
      console.log("\nThere is already a Symbol. Try again.");
      playerMove = await getInput(
        `Where would you want to place (${game[game.turn].name})?: `,
      );
    }

    if (game.turn == "player2") {
      move(game.player2, playerMove);
      game.turn = "player1";
    } else {
      move(game.player1, playerMove);
      game.turn = "player2";
    }
  }
}

async function setupGame() {
  console.log("Let's Start a Game of Tic Tac Toe! \n");
  game.player1.name = await getInput("Name of Player 1: ");
  game.player2.name = await getInput("Name of Player 2: ");
  game.state = "running";
}

function move(player, position) {
  game.board[position - 1] = player.symbol;
}

export default async function start() {
  if (game.state == "setup") {
    await setupGame();
  }

  if (game.state == "running") {
    await gameLoop();
  }

  if (game.state == "finish") {
    process.exit();
  }
}

function outputBoard() {
  console.log("\n");
  console.log(`${game.board[0]} | ${game.board[1]} | ${game.board[2]}`);
  console.log("---------");
  console.log(`${game.board[3]} | ${game.board[4]} | ${game.board[5]}`);
  console.log("---------");
  console.log(`${game.board[6]} | ${game.board[7]} | ${game.board[8]}`);
  console.log("\n");
}