In this article, let's see how to create a simple character matching game, using Windows Universal app. We will create our own game and have fun with Windows phone, using Universal app development.

Windows Universal Application
If we want an application, which needs to be run in any Windows device, for example Windows Phone, Windows 8 or Windows 10 operating system, we can develop a single application, which can be run in any Windows device, using Windows Universal Application.
What is a Character Matching Game?
Character Matching game is a game where a user can click 2 buttons one by one. If both the first clicked and second clicked button have the same character, then he won one time and if he/she wins continuously for 3 times, then he/she is very good in the puzzle game. If he/she clicked different characters from the first and second button, then he/she loses the game. There is no limit to playing the game, we can play any number of times. This game has 4 major rules:
- Clicked first button and second button Character match - Won the game and continue to play more times.
- Clicked first button and second button character do not match – Loses the game and continue to play more times.
- Clicked first button and second button Character match and if he/she won continuously 3 times, then he’s a genius in this game and continues to play more times.
- To shuffle the button text and start a new game, click on “Start New Game” button.
Prerequisites
Note
This example was been developed, using Windows 8.1 operating system. The same Application can be used for Windows 10 operating system.
Code part
Step 1
After installing both Visual Studio 2015 Windows Phone Emulators, click Start. Choose Programs and select Visual Studio 2015 - Click Visual Studio 2015.
Click New, followed by Project and select Visual C# >Select Windows 8 > Select Blank app (Universal Windows 8.1).

Select your project location path and give the name for your UWP app and click OK.
Adding controls depends on your requirements, and write your first code to display your output. In this example, we have used a Textblock (Textblock is similar to a Label control), button and a Grid control.
The Grid control is the main thing, since we will add buttons dynamically to the Grid whenever the user clicks on the “Start New Game” button.

To display the message box, we need to import, using Windows.UI.Popups.
Public variable
- Each variable has been commented with its uses.#region Fields
- // for creating buttons at runtime
- Button[] puzzleButtons;
- //to compare the previous click charater with new click
- Button oldButton;
- Button newButton;
- string oldChar = "";
- string newChar = "";
- //Counter varibale to check the result
- int clickCount = 0;
- int ansCount = 0;
- int totalClickCount = 0;#
- endregion
In the new game, click button and we will check for the total characters. Add the buttons dynamically to the grid. For the dynamically added buttons, we will create a click event to check each result of the button, which is clicked with previous and latest button click.
- private void button_Click(object sender, RoutedEventArgs e) {
- // to shuffle the character and reorder the characters for new Puzzle Game start
- string namePuzzle = "AZCHIJSARBQCNSKZDIFBOHCRQFEGLM";
- Random rnd = new Random();
- string findmyNameChar = new string(namePuzzle.ToCharArray().OrderBy(s => (rnd.Next(2) % 2) == 0).ToArray());
- //reset the game and to start new;
- oldChar = "";
- newChar = "";
- clickCount = 0;
- ansCount = 0;
- totalClickCount = 0;
- //to create dynamic buttons
- int xVal = 4;
- int yVal = 10;
- int boxWidth = (Convert.ToInt32(pnlButtons.Width) / 4) - 20;
- int boxHeight = 60;
- pnlButtons.Children.Clear();
- puzzleButtons = new Button[findmyNameChar.Length];
- int column = 0;
- int rows = 0;
- //Create buttons and add to grid at runtime
- for (int i = 0; i < findmyNameChar.Length; i++) {
- puzzleButtons[i] = new Button();
- puzzleButtons[i].Name = findmyNameChar[i].ToString() + i.ToString();
- puzzleButtons[i].FontSize = 16;
- puzzleButtons[i].Background = new SolidColorBrush(Windows.UI.Colors.OrangeRed);
- puzzleButtons[i].Foreground = new SolidColorBrush(Windows.UI.Colors.Black);
- puzzleButtons[i].Content = ""; // findmyNameChar[i].ToString();
- puzzleButtons[i].HorizontalAlignment = HorizontalAlignment.Left;
- puzzleButtons[i].VerticalAlignment = VerticalAlignment.Top;
- puzzleButtons[i].Margin = new Thickness(xVal, yVal, 0, 0);
- puzzleButtons[i].Click += new RoutedEventHandler(puzzleButton_Click);
- // puzzleButtons[i].Width = 20;
- puzzleButtons[i].Height = boxHeight;
- pnlButtons.Children.Add(puzzleButtons[i]);
- xVal = xVal + boxWidth + 10;
- column = column + 1;
- if (xVal + 100 >= pnlButtons.Width) {
- rows = rows + 1;
- column = 0;
- xVal = 4;
- yVal = yVal + boxHeight + 24;
- }
- }
- }








Anu VPosted Nov 21, 2016, 12:27 AM
Nice article.. Thanks for sharing.