Windows Phone 7, Programming a Tic Tac Toe Adding Application Bar Menu and Seperate Logic – Part 3

If you have been reading Programming Tic Tac Toe Part 1 and Programming Tic Tac Toe Part 2 . You should be ready to do this. If not please read those first if you are not familiar with Windows 7 programming.

On this segment I would like the add an Application Menu bar and want to provide the option for Single Player(play with computer) or 2 Player option.

Also I would like to separate the GameLogic in a separate class library and review the class library as I promised on part 1.  So here you go.

 

Adding the Application menu bar programatically is pretty straight forward.

Create coupe of ApplicationMenuBar items and then add them to the menu bar. After that you need to create the EventHandlers for each one of them.

Code Snippet
  1. public MainPage()
  2.         {
  3.             InitializeComponent();
  4.             _playWithComputer = false;
  5.             //add the application bar
  6.             ApplicationBar = new ApplicationBar();
  7.  
  8.             ApplicationBarIconButton button1 = new ApplicationBarIconButton(new Uri("/images/NewGame.ico", UriKind.Relative));
  9.             button1.Text = "New Game";
  10.  
  11.             ApplicationBar.Buttons.Add(button1);
  12.             button1.Click += new EventHandler(button1_Click);
  13.  
  14.             ApplicationBarMenuItem menuItem1 = new ApplicationBarMenuItem("Single Player");
  15.             ApplicationBarMenuItem menuItem2 = new ApplicationBarMenuItem("Two Players");
  16.             ApplicationBar.MenuItems.Add(menuItem1);
  17.             ApplicationBar.MenuItems.Add(menuItem2);
  18.             menuItem1.Click += new EventHandler(menuItem1_Click);
  19.             menuItem2.Click += new EventHandler(menuItem2_Click);
  20.             //start the game
  21.             StartGame();
  22.  
  23.         }
  24.  
  25.         void menuItem2_Click(object sender, EventArgs e)
  26.         {
  27.             _playWithComputer = false;
  28.             StartGame();
  29.         }
  30.  
  31.         void menuItem1_Click(object sender, EventArgs e)
  32.         {
  33.             _playWithComputer = true;
  34.             StartGame();
  35.         }

 

I will explain the _playWithComputer in a minute.

 

This MenuBar will look like this when you run your application.

image

 

When you have Single player, we have created a variable called _playWithComputer and we set it to false on the constructor. When you select Single Player from the menu bar, we set it to true and start the game. If a Game is on the way, it will get reset.

 

Now we took the GameLogic out of the application and  added a new Windows 7 Class Library project called GameLogicHelper and added the GameLogic static class with the logic.

Also I created a seperate Class Library for the enums called GeneralEnums.

 

There are some logic refinement on the GameLogic.

All that mumbo jumbo about adding equals to 15, 6, 24, 12 and 18 are gone.  We really don’t need those anymore.

We have 8 Lists that covers 8 ways a game can be won, if any of those Lists items count goes to 3, we have a winner.

 

Give it a shot. Source Code is included.

Click to Download Source Code for Part 3 Download Source

Leave a Reply

Your email address will not be published. Required fields are marked *