We are going to create a simple Tic Tac Toe game for Windows Phone. We’ll be using Visual Studio as our development environment and C# for our programming language. All the resources you need are listed below and all are free.
VISUAL STUDIO 2010 EXPRESS FOR WINDOWS PHONE http://www.microsoft.com/visualstudio/en-us/products/2010-editions/windows-phone-developer-tools or http://bit.ly/ymbC9Q
Windows Phone for Absolute Beginners video series https://channel9.msdn.com/Series/Windows-Phone-7-Development-for-Absolute-Beginners or http://bit.ly/x72NnV
For a copy of this post in Word format, the images for the project, and code snippets used to create this lab download the PhoneTTT.zip file from https://skydrive.live.com/?cid=9a87f3a86cb0aa3e&id=9A87F3A86CB0AA3E%215413
Open Visual Studio
Find Visual Studio on the Start menu and open it up. It should look something like this.
Next we will open a new project. From the File menu select New Project. From the New Project form select Visual C# as the language Silverlight for Windows Phone under that and Windows Phone Application as the application type. Make sure you give your project a name. Something like TicTacToe followed by your initials will do nicely (TicTacToeACT)
We’ll be using the latest Windows Phone Version which is 7.1.
Our initial screen will look something like this.
We’ll come back to this in a minute.
Add Image Resources
We need to add some image files to the project. We will be adding images for an empty box , the letter O and the letter X. Right click on the Solution in the Solution viewer and select Add > Existing Item. You can download them from here if you like.
Browse to where the files for the project are (should be on the Desktop) and select the three files we need and click on add.
Design Our Form
Now let’s go back to designing our form on the mock up of the phone. Left click on where it says “page name” and select properties. The properties window will open up and we will change the Text property to Tic Tac Toe.
Next we will use the Tool box and drag an Image onto the form.
Using the properties form set the Height to 69, the width to 93 and the Source to the image file of the empty box. Use copy and paste to make a series of identical image boxes on the form. The results should look something like the image above.
Add a tenth copy below the ninth copy. Next to that draw a Text Block. Change the Text property of the Text Block to read “Next Move.” You may want to play with the font size property as well. The result will look something like this.
We’ll come back to this but now let us enter some code.
Declare some variables
Under MainPage.xaml on the Solution Explorer you will find MainPage.xaml.cs which hold our programming code. Double click on that to open the code editing window.
Above the constructor declare some variables that we will use to manage the game throughout the program.
Inside the constructor we will set the initial values of our variables.
Note that the resetBoxes method has not been created yet so the IDE gives us a warning. (The same sort of red squiggly live we are used to from Microsoft Word) Let’s correct that now. Also note that a common cause of problems is the wrong location in the Uri section. Change the “TicTacToe” in this sample code to match your project name.
Create a Method to Set the Game Board
Our method here will have to initialize some additional variables. We do it here so that we can reset them all in just one place when we start new games.
Respond to Taps
We’ll play the game by tapping on individual boxes. For each tap we have to make sure it is a valid move (nothing in the box already), store and display the right symbol and check for a win or a tie. We’ll need a tab routine for each box but I’m only going to show one of them here. The rest is left as an exercise for the student. J
As you can see this method calls several of the methods we have not yet created. Let’s get on that.
Updating a Box
Checking for a Winner
Checking for a winner means looking for three boxes in a row, column or diagonal that have the same value. Oh and it has to be the same not empty box value.
Getting close now. Notice the call to TieGame? Let’s create that routine after we create the winning end of a game.
Handling the End of a Game
Here we have the two routines that show the end of a game. One for a winning game and one for a tie game.
We just have two more things to do. The first of those is that we have to “tell” the image boxes to call the tap routines when they are tapped. For this we go back to the design window.
Assigning Tap Handlers
Back on the property window you will find the Events tab. Selecting that and moving down we find an option for Tap. Selecting each image one by one we can use the drop down list to set the right handler for each image box.
Starting a New Game
All we need now is a way to start a new game. I suggest we tap on the Text Block to do that. Select the Text Block and in the properties window option for Tap enter New Game and press the enter key. This will open up the code window with a chance for use to call the ResetBoxes method we created.
Ok let’s run it and see how we did.