Shall We Play a Game?


It’s pretty easy to write a Tic-Tac-Toe game for a computer.  The first one I wrote for my TRS-80 was maybe 15 lines long.  A couple of lines drew the board, another couple of lines managed the input of the X’s and O’s, and the rest was random move generators for the computer player.

However, while it was easy to write, it was also easy to win.  The problem was the “strategy”.  There are nine possible positions on the board.  A number generator would pick one at random, check to make sure the space hadn’t already been played and draw it on the screen.  There was no intelligence.  I might have two X’s going across the top row and the computer would pick the lower right corner.  I win.

I started trying to build a better Tic-Tac-Toe player.  The resulting program ended up being quite involved, about thirty times longer than my original Tic-Tac-Toe attempt.  It was also a bit slow.  However, it was pretty hard to beat and most games ended in a tie.

Since there are only nine possible plays in the game, I figured it wouldn’t be that hard.  The first thing I did was figure out the logic of why a certain spot on the board should be picked.  I assigned each space a number.  Originally I had them as one through nine, but that turned out to be an issue.  I ended up doing them in powers of 3.  The first space was 1; the next was 3, then 9, and so on.  Each potential winning row had its own unique value if you added the spaces together.  Therefore the computer could “predict” where I might try next and block me.

20000708d1If there was no chance of me winning in the next move, the computer had a choice of offensive moves.  I had a number of lines looking at possible winning scenarios and checking them against whether or not they were still available based on what I had picked.  This took the most time to write.  The original program just ignored it when I was in the way.  It would cheat to win!

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