MyNixOS website logo
Description

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.

Build Status CRAN Status

tictactoe

Play and learn Tic-Tac-Toe Game on R

installation and import

Install from CRAN

install.packages("tictactoe")

Or you may install the recent development version from github

devtools::install_github("kota7/tictactoe")

To use,

library(tictactoe)

Play a Game

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

ttt(ttt_human(), ttt_ai())

This would give you a prompt as below.

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

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

Type a 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 (from 0 (weekest) to 5 (strongest)).

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

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()).

Simulation

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)
prop.table(table(res))
#> res
#>    0    1    2 
#> 0.13 0.57 0.30

Q-learning

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)
prop.table(table(res))
#> res
#>    0    1    2 
#> 0.15 0.25 0.60

References

  • Sutton, Richard S and Barto, Andrew G. Reinforcement Learning: An Introduction. The MIT Press (1998)
Metadata

Version

0.2.2

License

Unknown

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows