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

50 Responses 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?

  2. Olle on January 28th, 2011 8:44 am

    Very nice blog post!

  3. http://criminalbackgroundchecked.com/ on July 4th, 2012 12:15 pm

    home repair can be hard specially if you do not have any other idea at
    all about what you have been doing,,

  4. this website ourmidland on July 7th, 2012 8:56 am

    company management can make or break the success of a
    corporation that’s why it’s very very critical

  5. scholarships for single mothers on July 8th, 2012 3:40 am

    Excellent work, wonderful blog really enjoy it and put into my
    social bookmarks. Keep up that the first-rate work

  6. scholarships for college students|scholarships for college students in texas|2011 scholarships for college students|scholarships for college students in california|scholarships for college students 2011|easy scholarships for college students|grants and sc on July 8th, 2012 2:18 pm

    Fine goods from you, man. I have understand your stuff previous to and youre just
    extremely wonderful. I actually just like what youve acquired here, certainly for example

  7. small business grants for single mothers on July 8th, 2012 3:33 pm

    Heya im for that the first time here. I found this board and I get a hold of
    It truly useful & it helped me out a lot.
    I hope to give away something back and help others along the lines of you aided me.

  8. free money for single mothers on July 8th, 2012 4:23 pm

    Typically there have been some fascinating time limits
    in this post but I dont know if they all center to heart.
    It is easy to find some validity on that the other hand I will take hold opinion until I explore it further.
    Fine article , thanks therefore we want extra! Put into FeedBurner as properly

  9. scholarships grants for single mothers on July 8th, 2012 5:02 pm

    all we want is of course a firm skin which can be very smooth.
    great skin comes with great genetics and proper maintennance

  10. reverse phone lookup on July 9th, 2012 4:37 pm

    What i dont understood is actually how you have been not really much more wellliked
    than it is easy to be now. Youre very intelligent.
    You realize therefore considerably relating to this subject, produced me personally consider it out of
    so plenty of varied angles. Its something like women and men arent fascinated unless
    it’s one thing to do with Lady gaga! Your own stuffs good. Always maintain it up!

  11. cell phone lookup reverse on July 10th, 2012 4:25 am

    Its perfect time to make some plans for the future and
    it can be time to be happy. Ive read this post and if I could I desire to suggest you few interesting things or tips.
    Maybe you could write next articles referring to
    this article. I want to read more things about it!

  12. scholarships for single mothers on July 11th, 2012 4:33 am

    i love funny stuffs, but i specially such as funny movies and funny videos online

  13. http://www.prlog.org/11306717-phone-number-lookup-is-your-partner-cheating-on-you.html on July 16th, 2012 12:21 pm

    Howdy! I recognize this is kinda off topic but Id figured Id ask.
    Would you be interested in exchanging links or maybe guest authoring a blog post or viceversa My website addresses lots of the same topics because yours and I believe we could vastly benefit from each
    other. When you have been interested feel free of charge to shoot
    me an email. I look forward to hearing from you! Superb blog by that the system!

  14. http://officialreversephonelookup.com/ on July 16th, 2012 12:48 pm

    I happen to be writing to let you understand what a incredible experience our
    girl encountered studying that the blog. She came to lookup for a whole lot of things,
    which include what its for instance to possess an fine giving heart to make men and women quite only find out about certain grueling matters.

    You actually did more than our own expected results.

    Quite a ton of thanks for showing these helpful, trustworthy,
    revealing and also unique guidance on the topic to Sandra.

  15. cell phone number lookup on July 16th, 2012 2:10 pm

    Thanks for making the honest attempt to discuss this.
    I feel very strong approximately it and wish to read more.
    If its OK, because you acquire more in depth knowledge, may you thoughts adding extra articles similar to this one with additional info It could
    be extraordinarily useful and useful for me and my friends.

  16. http://ezinearticles.com/?Free-Reverse-Phone-Lookup---Answer-To-The-Myth-About-Free-Cell-Phone-Number-Lookups&id=5986059 on July 16th, 2012 2:28 pm

    Woah! Im actually digging that the template/theme of this blog.
    It can be straightforward, yet effective. A whole lot of times it is tough to search out that “perfect balance between usability and visual appeal.
    I must say youve done a great job with this.
    Additionally, that the blog loads extremely quick for me on Firefox.
    Outstanding Blog!

  17. http://www.prlog.org/11674605-reverse-phone-lookup-finders.html on July 16th, 2012 3:33 pm

    constantly i used to read smaller articles or reviews that also clear their
    motive, and that is also happening with this post which I am reading now.

  18. http://www.youtube.com/watch?v=c3-A_ZxBlSQ on July 16th, 2012 4:58 pm

    on the net health stores always give some promo and discounts that is
    why i always order from them

  19. http://www.prlog.org/11306749-phone-number-lookup-got-cheating-wife-on-your-hands.html on July 16th, 2012 6:08 pm

    While seeking on Dating I found your blog, there are some good posts here
    and Ill be checking back

  20. http://ezinearticles.com/?Cell-Phone-Number-Directories---Can-You-Use-It-to-Catch-a-Cheating-Partner?&id=5985845 on July 16th, 2012 8:23 pm

    obviously something like your website then again you should test the spelling on a few of your posts.
    Lots of of them have been rife with spelling issues and I
    find it very troublesome to inform that the truth in spite of this Ill certainly come again again.

  21. reverse phone directory on July 16th, 2012 8:24 pm

    Having read this I thought it was rather enlightening.
    I appreciate you spending some time and energy
    to put this information together. I once again find myself
    personally spending a significant amount of time both reading and posting comments.
    But so what, it was still worth it!

  22. http://ezinearticles.com/?Reverse-Address-Lookup---Get-the-Information-You-Need-the-Easy-Way&id=5909554 on July 16th, 2012 8:55 pm

    i always make sure that our kitchen appliances have
    been very clean and shiny before using them.

  23. http://officialreversephonelookup.com/ on July 16th, 2012 9:13 pm

    Thank you for sharing your info. I truly appreciate your
    efforts and I will be waiting for your next write ups thanks
    once again.

  24. http://www.prlog.org/11684341-reverse-cell-phone-lookup-number.html on July 16th, 2012 10:28 pm

    Magnificent beat ! I would like to apprentice at the same time as you amend your website, how
    can i subscribe for a weblog web site? The account helped me a applicable deal.

    I have been tiny bit familiar of this your broadcast provided shiny clear idea

  25. http://ezinearticles.com/?Reverse-Phone-Lookup---Easy-Way-to-Know-the-Truth&id=5909597 on July 17th, 2012 2:09 am

    Ive been absent for some time, but now I remember why
    I used to love this blog. Thank you, I will try and check back more often.
    How frequently you update your website

  26. people search on July 17th, 2012 2:28 am

    all we want is of course a firm skin which
    is very smooth. great skin comes with great genetics and proper maintennance

  27. http://ezinearticles.com/?Reverse-Phone-Number-Lookup---Find-Out-If-Your-Boyfriend-Is-a-Serial-Cheater&id=5909604 on July 17th, 2012 2:41 am

    I discovered your site web site on bing and check many of your early
    posts. Maintain up that the actually good operate. I just additional your
    Feed to my MSN News Reader. Seeking for forward
    to reading a ton more from you finding out later on!

  28. http://www.cellphonelookuped.com/ on July 17th, 2012 11:40 pm

    I believe this site contains some real fantastic information for everyone .

  29. ravinder singh on July 18th, 2012 2:52 am

    hi hukfjfjh

  30. http://www.prlog.org/11261550-phone-number-lookup-catch-cheater-quickly.html on July 25th, 2012 1:59 am

    Hello there, just became alert to your blog through Google,
    and found that it’s truly informative. Im gonna watch out for brussels. Ill be grateful whenever you continue this in future. Quite a lot of people will be benefited out of your writing. Cheers!

  31. make money online on July 27th, 2012 6:35 am

    I am also writing to let you be aware of what
    a cool discovery my wifes daughter went through studying your web page.
    She realized a couple of things, which include what it can be for example to possess
    an amazing giving spirit to look for that the others without problems grasp chosen
    complex subject areas. You undoubtedly exceeded visitors expected results.
    Thank you for coming up with those effective, trustworthy,
    informative and because well as cool thoughts on that topic to Emily.

  32. http://www.prlog.org/11936100-reverse-phone-lookup-to-trace-any-number.html on July 29th, 2012 9:03 am

    Right now it appears like Drupal is the preferred blogging platform available right now.
    from what Ive read Is that what youre using on your blog

  33. Tory on July 30th, 2012 11:32 pm

    Thanks for this web site post and discussing your own results together with us.
    Very well performed! I think many people get a hold of it
    challenging to understand paying attention to a couple
    of controversial things associated with this topic, but your own results
    talk for themselves. I think a quantity of additional takeaways are the importance of following
    each of that the suggestions you presented in this article
    and being prepared to be ultra distinct about which one could really
    work for you greatest. Nice job.

  34. http://www.reversephonelookuped.com/ on August 5th, 2012 4:12 pm

    Utterly indited content material , Actually enjoyed examining .

  35. reverse cell phone lookup on August 9th, 2012 6:26 pm

    Hi there, just became aware of your blog through Google, and found that it can be actually informative.
    I¡¦m going to watch out for brussels. I will
    appreciate whenever you continue this in future. A whole lot of people will be benefited out of your
    writing. Cheers!

  36. reverse cell phone lookup on August 9th, 2012 6:38 pm

    i am addicted to computer games that is why i bought a gamecube for me and my brother,.

  37. cell phone lookup on August 9th, 2012 6:54 pm

    Very interesting points you have observed , thankyou for posting .

  38. cell phone lookup on August 9th, 2012 7:06 pm

    You’ll want to experience a tournament for just one of that the best internet sites on the web. Let me suggest this website!

  39. reverse cell phone lookup on August 9th, 2012 7:07 pm

    This is a terrific web site, could you be interested in doing an interview regarding how you designed it If so email me!

  40. reverse cell phone lookup on August 9th, 2012 7:19 pm

    An interesting discussion may be worth comment. Theres no doubt that you must write on this topic,
    it will not be a taboo subject then again usually folks are inadequate
    to speak on such topics. Yet another. Cheers

  41. reverse cell phone lookup on August 9th, 2012 7:26 pm

    Can I just say what a relief to obtain an person who really knows what theyre talking about on that the internet.
    You unquestionably recognize how it is easy to bring an question to light and make it important.

    A lot more individuals need to read this and realize
    this side of the story. I cant believe youre not additional wide-ranging only because you definitely have the gift.

  42. reverse cell phone lookup on August 13th, 2012 10:55 am

    botox can be very helpful with skin wrinkles and it can
    also help treat bruxism“

  43. Susan on August 13th, 2012 11:56 am

    You, my pal, ROCK! I found just that the info I already
    searched all over the place and just could not locate it.
    What a perfect web site.

  44. cell phone lookup on August 16th, 2012 3:19 pm

    I adore seeking at and I think this website got some genuinely
    utilitarian stuff on it! .

  45. cell phone lookup on August 19th, 2012 9:35 pm

    girls in most cases love to hear celebrity gossips, they are always into it

  46. reverse phone lookup on August 23rd, 2012 4:29 am

    Considerably, the post is really the greatest on this worthy topic.

    I agree with your conclusions and can eagerly look forward
    to your future updates. Merely just saying thanks can not
    merely be sufficient, for the fantasti c clarity in your writing.
    I will perfect away grab your rss feed to stay abreast of any updates.
    Genuine work and also much success in your business dealings!

  47. reverse phone lookup on August 23rd, 2012 5:01 am

    There is noticeably quite a large amount of cash to be acquainted with about
    this. I suppose you made particular nice points in functions also.

  48. reverse phone lookup cell on August 23rd, 2012 6:59 pm

    Excellent site! I really love how its nice on my eyes it can be.
    I am wondering how I might be notified whenever a new post has been made.
    Ive subscribed to your RSS feed which may do the trick Have a nice day!

  49. Tic-tac-toe in Erlang — alternative rules (Y pattern) : Code Obscurata on August 24th, 2012 6:20 pm

    […] Introduction […]

  50. Tic-tac-toe in Erlang — top-level loop : Code Obscurata on August 24th, 2012 6:23 pm

    […] Introduction […]