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.
- public MainPage()
-         {
-             InitializeComponent();
-             _playWithComputer = false;
-             //add the application bar
-             ApplicationBar = new ApplicationBar();
-             ApplicationBarIconButton button1 = new ApplicationBarIconButton(new Uri("/images/NewGame.ico", UriKind.Relative));
-             button1.Text = "New Game";
-             ApplicationBar.Buttons.Add(button1);
-             button1.Click += new EventHandler(button1_Click);
-             ApplicationBarMenuItem menuItem1 = new ApplicationBarMenuItem("Single Player");
-             ApplicationBarMenuItem menuItem2 = new ApplicationBarMenuItem("Two Players");
-             ApplicationBar.MenuItems.Add(menuItem1);
-             ApplicationBar.MenuItems.Add(menuItem2);
-             menuItem1.Click += new EventHandler(menuItem1_Click);
-             menuItem2.Click += new EventHandler(menuItem2_Click);
-             //start the game
-             StartGame();
-         }
-         void menuItem2_Click(object sender, EventArgs e)
-         {
-             _playWithComputer = false;
-             StartGame();
-         }
-         void menuItem1_Click(object sender, EventArgs e)
-         {
-             _playWithComputer = true;
-             StartGame();
-         }
I will explain the _playWithComputer in a minute.
This MenuBar will look like this when you run your application.
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