Tic Tac Toe bot in Bash

Bash’s tic tac toe code that translates the code from Perl’s bot.

I got inspiration from http://curtisquarterly.blogspot.com/2005/04/writing-tic-tac-toe-bot-in-perl.html

Basically the bash code recreates the strategies for:

Making a winning move.

Preventing the opponent from making a winning move

Obtaining a “fork”

Preventing the opponent from being able to fork

Taking the center square

The challenges I faced are briefly summarized below:

  • Finding corresponding functionality in bash for perl’s grep and substitution.
  • Updating the variables, since they don’t get automatically changed like in perl. E.g. in Perl the expression grep { /$piece/ && /$move/ && /\d${piece}?\d/ } @wins would at the same time filter @wins and alter it.
  • Placing the returns in the appropriate places and to clear the variables used for accumulation.

The script: tttbot_blog

Note: You can use cygwin64 to run this script in windows.  However, DO NOT copy and paste the script, as windows introduces \r characters.  Rather, rename the script from tttbot_blog.doc to tttbot_blog.sh or tttbot_blog and run it in bash.

To-do: The performance of the bash program is slow. Please wait for up to 15 seconds in the second player’s move and 10 or less for subsequent moves.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: