Tic-Tac-Toe Game

Implements tic-tac-toe game to play on console, either with human or AI players. Various levels of AI players are trained through the Q-learning algorithm.

Play and learn Tic-Tac-Toe Game on R

To install,


To use,


You can play tic-tac-toe on R console.

ttt(ttt_human(), ttt_ai())

This would give you a prompt like:

    A B C
 1| . . .
 2| . . .
 3| . . .

 Player 1 (no name) to play
choose move (e.g. A1) > 

Type the move, then the oppoenet will respond. To finish the game in the middle, type "exit".

The default AI player is very week (in fact, he plays randomly). To play against a more sophisticated player, set the level argument.

ttt(ttt_human(), ttt_ai(level = 4))

level is from 0 (weekest) to 5 (strongest).

You may play as the second mover by ttt(ttt_ai(), ttt_human()). You may watch games between AI players by ttt(ttt_ai(), ttt_ai()).

To conduct a large scale simulation between AI players, use ttt_simulate function. The code below conducts 100 simulation games between random AIs. The result 0, 1, and 2 indicate draw, won by player 1, and won by player 2 respectively.

res <- ttt_simulate(ttt_ai(), ttt_ai(), N = 100, verbose = FALSE)
#>    0    1    2 
#> 0.13 0.57 0.30

Q-learning is implemented to train AI players. The code below trains a random AI through Q-learninig of 500 episodes.

p <- ttt_ai()
o <- ttt_qlearn(p, N = 500, verbose = FALSE)

Now this player is much stronger than the random player.

res <- ttt_simulate(ttt_ai(), p, N = 100, verbose = FALSE)
#> res
#>    0    1    2 
#> 0.15 0.25 0.60
  • Sutton, Richard S and Barto, Andrew G. Reinforcement Learning: An Introduction. The MIT Press (1998)


Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.2.2 by Kota Mori, 4 months ago


Report a bug at https://github.com/kota7/tictactoe/issues

Browse source code at https://github.com/cran/tictactoe

Authors: Kota Mori [aut, cre]

Documentation:   PDF Manual  

MIT + file LICENSE license

Imports hash, stats

Suggests testthat, combiter, dplyr, tidyr, reshape2, ggplot2

See at CRAN