Wednesday, January 21, 2009

Tic Tac Toe in Silverlight

When I first started looking Silverlight, I looked around for tutorials that focused on topics I find interesting. I have found that I typically learn better if I'm applying learning to topics for which I have a passion.

I love games and one of the simplest is Tic Tac Toe. I thought if I could find a tutorial on implementing Tic Tac Toe in Silverlight, it would not only teach the basics of Silverlight development but would also teach how to interact with graphics. Unfortunately I could not find such a tutorial.

I eventually waded my way through much less entertaining tutorials and got my programming feet wet with Silverlight. As a test of my learning I wrote a two player Tic Tac Toe game.

After finishing my first AI post on Rock/Paper/Scissors I started looking for my next AI project. Well I had that two player Silverlight game laying around, how about I write an AI opponent for it?

What could be easier?

Well a lot of things could be easier, like "Hello World", but once I get my teeth into a project I see it to the end. Well, first things first, how have others designed AI opponents for Tic Tac Toe? A quick google search proved that I was definitely not blazing an new trails here. I found tons of examples for just about every type of AI for Tic Tac Toe.

But this webpage gives a simple set of patterns for matching. Best of all, they could be implemented using Regular Expressions!

So, here is my Silverlight AI Tic Tac Toe game where the pattern matching AI is implemented using Regular Expressions.



And here is a zip file containing the entire Visual Studio 2008 Project.

17 comments:

Anonymous said...

Cool, but can anyone win at it?

Anonymous said...

Not if I coded the AI right! :-)

Tic Tac Toe is a solved game. A complete AI should be unbeatable.

Lee

Anonymous said...

Thats not much fun :) Nice little app tho.

Anonymous said...

I won one game! Nice game anyway.

Anonymous said...

Cldn't find it interesting..

Inc Magazine said...

wow,

It beat me everytime.

nice job -- I was just researching silverlight a little bit.

Anonymous said...

You do have at least one bug :P

http://www.zshare.net/image/5512852658f475df/

Anonymous said...

and another bug...
does this count as me winning? :)

http://www.zshare.net/image/55128743b4d8e956/

Lee Saunders said...

The first bug - Found it! Thanks! I never even thought of clicking on the Tic Tac Toe grid.

My code sees that as a move, yet does not place a piece.

As for the second bug, I cannot figure out how to reproduce it. Any hints would be appreciated.

(This weekend I have to go fix that first bug!!)

Unknown said...

Click on the line instead of the box. That should reproduce the second one.

Lee Saunders said...

Fixed! Both the game in the post and the download.

(You may need to reload to get the lastest version!)

Anonymous said...

center down, right up, right down, left down this is winer commbination.

Anonymous said...

i always win the program
is there any programming logic?

Anonymous said...

Another winning combo is

top-right, center-down, right-down, left-down :)

Anyway, very good example for WPF/SL

Jinx said...

There is no AI, it's hard-coded, you make a move and it places itself in the same position everytime...

If you don't pick center, the computer will...
If you pick center, the computer will pick top-left...

Only 2 winning combos (both essentially the same):
1. TR,BC,BR,BL
2. BC,TR,BR,BL

Lee Saunders said...

You may need to brush up on your knowledge of AI.

This type of AI is called a "Knowledge based AI", it makes its moves based on the "Knowledge" that is programmed into it. It's "Knowledge" is based on the logic from the article I cited in my blog entry.

Obviously the author missed a permutation that leads to a win for the human player, so the "Knowledge base" is incomplete.

Even "Learning AI's" such as Neural Networks are "Hard Coded" in their responses once the teaching phase is finished and any specific input will result in the same output, time and time again.

Nishikant Kotgire said...

Hi Lee,

Nice game and AI concepts :-)