Tic-tac-toe in Erlang — introduction

This is part of an Erlang sample-code review built around a tic-tac-toe program. The program is stuffed into one file, called tic.erl and available here. The source code and this tutorial are organized into these sections:

Introduction

I recently programmed tic-tac-toe using Erlang, and I thought I’d offer to anyone looking for sample code. Here’s the source.

Tic-tac-toe is a good choice for an example program. It’s a simple yet still non-trivial game, so it’s interesting. Everyone knows the rules, which makes it easy to understand the architecture and code design. Deciding the next move involves some analysis and decision making. And it touches on a lot of features.

This version of tic-tac-toe has a standard-IO text interface, which means it reads and displays to the console. That’s right: no graphics, no windows, no mouse. It prints the board using fixed-width scrolling text, and it asks for keyboard commands. State of the art — in 1970.

It wouldn’t be hard to kick together a GS (Erlang Graphic System module) interface. Maybe I’ll do that sometime.

Running the program

To run this program first you have to install Erlang on your computer. You might also want to look at the tutorial.

When you can get an Erlang shell started you can run tic-tac-toe. Assuming I’ve saved tic.erl at c:/examples/erlang/tic.erl, here’s what it looks like to load the module (tic) and start the game.

Erlang (BEAM) emulator version 5.6.5 [smp:2] [async-threads:0]

Eshell V5.6.5  (abort with ^G)
1> c( 'c:/examples/erlang/tic').
{ok,tic}
2> tic:play( ).

Welcome to tic-tac-toe, the game that predicts
the outcomes of every move and lets you erase
X's and O's and skip turns.

Starting a new game.

               []               []
               []               []
       1       []       2       []       3
               []               []
               []               []
===============##===============##===============
               []               []
               []               []
       4       []       5       []       6
               []               []
               []               []
===============##===============##===============
               []               []
               []               []
       7       []       8       []       9
               []               []
               []               []               

It is X's turn to move.
What do you want to do (h=help, q=quit)?

At this point the game stops, waiting for me to type something and hit return. I’ll ask for help.

What do you want to do (h=help, q=quit)? h

Please enter one of the following:
  q - quit
  h - help, show this message
  g - start a new game
  b - show a simple board
  n - show the board with open spots numbered
  p - show a board with predicted outcomes
  1-9 (a single digit)
    - choose X's next move
  a - automatically choose X's next move
  s - skip X's next move

What do you want to do (h=help, q=quit)?

There are three generic commands: quit, help, and start a new new game. And then there are three options that affect how the board is printed. And finally there are 3 ways to make a move.

Let’s let the computer move first. We type ‘a’ and hit return.

It is X's turn to move.
What do you want to do (h=help, q=quit)? a
Automatically selecting X's next move.

               []               []
  1  Leads to  []  2  Loses in  []  3  Loses in
     CAT game  []  three turns  []  three turns
               []   after this  []   after this
               []               []
===============##===============##===============
   XXX   XXX   []               []
     XX XX     []  5  Leads to  []  6  Leads to
      XXX      []     CAT game  []     CAT game
     XX XX     []               []
   XXX   XXX   []               []
===============##===============##===============
               []               []
  7  Leads to  []  8  Loses in  []  9  Loses in
     CAT game  []  three turns  []  three turns
               []   after this  []   after this
               []               []

It is O's turn to move.
What do you want to do (h=help, q=quit)?

As you can see, this isn’t just simple tic-tac-toe. The computer is predicting the outcome of each of the possible next moves for ‘O’. If ‘O’ plays in the center (position 5) and neither ‘X’ nor ‘O’ make any mistakes, this will end up a cat game. But if ‘O’ plays position 2 (just above the center) then ‘X’ can win in three more moves.

Let’s put ‘O’ at position 2 and then let the computer choose all the future moves for both ‘X’ and ‘O’.

It is O's turn to move.
What do you want to do (h=help, q=quit)? 2
Marking O at position 2.

               []     OOOOO     []
  1   Wins in  []   OOO   OOO   []  3  Leads to
  three turns  []  OOO     OOO  []     CAT game
   after this  []   OOO   OOO   []
               []     OOOOO     []
===============##===============##===============
   XXX   XXX   []               []
     XX XX     []  5   Wins in  []  6  Loses in
      XXX      []  three turns  []  three turns
     XX XX     []   after this  []   after this
   XXX   XXX   []               []
===============##===============##===============
               []               []
  7  Loses in  []  8  Leads to  []  9  Leads to
  three turns  []     CAT game  []     CAT game
   after this  []               []
               []               []               

It is X's turn to move.
What do you want to do (h=help, q=quit)? a
Automatically selecting X's next move.

   XXX   XXX   []     OOOOO     []
     XX XX     []   OOO   OOO   []  3     Loses
      XXX      []  OOO     OOO  []   after this
     XX XX     []   OOO   OOO   []
   XXX   XXX   []     OOOOO     []
===============##===============##===============
   XXX   XXX   []               []
     XX XX     []  5     Loses  []  6     Loses
      XXX      []   after this  []   after this
     XX XX     []               []
   XXX   XXX   []               []
===============##===============##===============
               []               []
  7  Loses in  []  8     Loses  []  9     Loses
    two turns  []   after this  []   after this
   after this  []               []
               []               []

It is O's turn to move.
What do you want to do (h=help, q=quit)? a
Automatically selecting O's next move.

   XXX   XXX   []     OOOOO     []
     XX XX     []   OOO   OOO   []  3  Loses in
      XXX      []  OOO     OOO  []    two turns
     XX XX     []   OOO   OOO   []   after this
   XXX   XXX   []     OOOOO     []
===============##===============##===============
   XXX   XXX   []               []
     XX XX     []  5   Wins in  []  6  Loses in
      XXX      []    next turn  []    two turns
     XX XX     []   after this  []   after this
   XXX   XXX   []               []
===============##===============##===============
     OOOOO     []               []
   OOO   OOO   []  8  Leads to  []  9  Loses in
  OOO     OOO  []     CAT game  []    two turns
   OOO   OOO   []               []   after this
     OOOOO     []               []               

It is X's turn to move.
What do you want to do (h=help, q=quit)? a
Automatically selecting X's next move.

   XXX   XXX   []     OOOOO     []
     XX XX     []   OOO   OOO   []  3     Loses
      XXX      []  OOO     OOO  []   after this
     XX XX     []   OOO   OOO   []
   XXX   XXX   []     OOOOO     []
===============##===============##===============
   XXX   XXX   []   XXX   XXX   []
     XX XX     []     XX XX     []  6     Loses
      XXX      []      XXX      []   after this
     XX XX     []     XX XX     []
   XXX   XXX   []   XXX   XXX   []
===============##===============##===============
     OOOOO     []               []
   OOO   OOO   []  8     Loses  []  9     Loses
  OOO     OOO  []   after this  []   after this
   OOO   OOO   []               []
     OOOOO     []               [] 

It is O's turn to move.
What do you want to do (h=help, q=quit)? a
Automatically selecting O's next move.

   XXX   XXX   []     OOOOO     []
     XX XX     []   OOO   OOO   []  3     Loses
      XXX      []  OOO     OOO  []   after this
     XX XX     []   OOO   OOO   []
   XXX   XXX   []     OOOOO     []
===============##===============##===============
   XXX   XXX   []   XXX   XXX   []
     XX XX     []     XX XX     []  6   WINNING
      XXX      []      XXX      []        MOVE!
     XX XX     []     XX XX     []
   XXX   XXX   []   XXX   XXX   []
===============##===============##===============
     OOOOO     []     OOOOO     []
   OOO   OOO   []   OOO   OOO   []  9   WINNING
  OOO     OOO  []  OOO     OOO  []        MOVE!
   OOO   OOO   []   OOO   OOO   []
     OOOOO     []     OOOOO     []               

It is X's turn to move.
What do you want to do (h=help, q=quit)? a
Automatically selecting X's next move.

   XXX   XXX   []     OOOOO     []
     XX XX     []   OOO   OOO   []
      XXX      []  OOO     OOO  []       3
     XX XX     []   OOO   OOO   []
   XXX   XXX   []     OOOOO     []
===============##===============##===============
   XXX   XXX   []   XXX   XXX   []   XXX   XXX
     XX XX     []     XX XX     []     XX XX
      XXX      []      XXX      []      XXX
     XX XX     []     XX XX     []     XX XX
   XXX   XXX   []   XXX   XXX   []   XXX   XXX
===============##===============##===============
     OOOOO     []     OOOOO     []
   OOO   OOO   []   OOO   OOO   []
  OOO     OOO  []  OOO     OOO  []       9
   OOO   OOO   []   OOO   OOO   []
     OOOOO     []     OOOOO     []               

X is the winner.
What do you want to do (h=help, q=quit)? q

Thanks for playing!

ok
3>

Pretty brilliant, eh? OK, maybe not. I know tic-tac-toe isn’t exactly cutting-edge game theory, but I enjoyed programming it, and I hope you enjoy it too.

Thanks for reading!

Comments

One Response to “Tic-tac-toe in Erlang — introduction”

  1. Jared on April 27th, 2009 6:25 pm

    A strange game. The only winning move is not to play. How about a nice game of chess?

Leave a Reply