/tic-tac-toe-cli

A toy project

Primary LanguageJavaScript

TicTacToe CLI

A toy project.

Usage

requires node >= v4.0.0

Start game

$ npm start

Choose players

? Choose type (Use arrow keys)
❯ Human
  Computer

There are two types of players:

  • Human: will take input from the keyboard
  • Computer: uses an algorithm to find the best move for the given board

Human input

It is X[H: Joe]s turn:
    a   b   c
   ───────────
1 │   │   │   │
  │───────────│
2 │   │   │   │
  │───────────│
3 │   │   │   │
   ───────────
? Choose empty cell (a1-c3)

Computer algorithm

The algorithm used to find the best move is based on the Negamax search:

"a variant form of minimax"

It is, however, optimized in several ways:

  • It looks for immediate winners: it checks all the available moves for an immediate winner, and only if it does not find one, it actually starts checking the full tree of possible moves using negamax.
  • When doing the search, it uses a form of pruning, meaning that, when it finds a winning move, it does not check further moves.
  • It uses standard openings, based on analyzing the outcome of the first two moves. This is because, for the first move in the opening round, any choice ensures a tie, and, for the the second move, there are some choices that result in a tie, all the others being loosers. So, if the best outcome when making the first move is a tie, it makes no sense to compute it all the time. So, for the first round, the moves are:
    • if first: any move is good, but it will choose a corner, since it leaves the opponent only one move that is not loosing (the center)
    • if second: depending on the first move, there are several choices available, but, in any case, choosing the center is a move that guarantees at least a tie. Except, of course, if the first move is itself in the center, in which case, the algorithm will choose any corner.

TODO

  • Format text using specific colors (for player names, signs, etc).
  • Make some game aspects configurable (like table box chars), maybe from a settings file.
  • Add other ways to take human input for moves.
  • Add some others UIs (e.g. HTML)