Character Matching Game Using Universal Windows App

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:

  1. Clicked first button and second button Character match - Won the game and continue to play more times.
  2. Clicked first button and second button character do not match – Loses the game and continue to play more times.
  3. 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.
  4. To shuffle the button text and start a new game, click on “Start New Game” button.

Prerequisites

  1. Visual Studio 2015. You can download it from here.
  2. Windows Phone 8.1 Emulators download here.

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

Create our ASP.NET Core 1.0.1 Web Application

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).

game

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.

game

To display the message box, we need to import, using Windows.UI.Popups.

Public variable
  1. Each variable has been commented with its uses.#region Fields  
  2.     // for creating buttons at runtime  
  3. Button[] puzzleButtons;  
  4. //to compare the previous click charater with new click  
  5. Button oldButton;  
  6. Button newButton;  
  7. string oldChar = "";  
  8. string newChar = "";  
  9. //Counter varibale to check the result  
  10. int clickCount = 0;  
  11. int ansCount = 0;  
  12. int totalClickCount = 0;#  
  13. endregion  
Start New Game Button Click

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.
  1. private void button_Click(object sender, RoutedEventArgs e) {  
  2.     // to shuffle the character and reorder the characters for new Puzzle Game start  
  3.     string namePuzzle = "AZCHIJSARBQCNSKZDIFBOHCRQFEGLM";  
  4.     Random rnd = new Random();  
  5.     string findmyNameChar = new string(namePuzzle.ToCharArray().OrderBy(s => (rnd.Next(2) % 2) == 0).ToArray());  
  6.     //reset the game and to start new;  
  7.     oldChar = "";  
  8.     newChar = "";  
  9.     clickCount = 0;  
  10.     ansCount = 0;  
  11.     totalClickCount = 0;  
  12.     //to create dynamic buttons  
  13.     int xVal = 4;  
  14.     int yVal = 10;  
  15.     int boxWidth = (Convert.ToInt32(pnlButtons.Width) / 4) - 20;  
  16.     int boxHeight = 60;  
  17.     pnlButtons.Children.Clear();  
  18.     puzzleButtons = new Button[findmyNameChar.Length];  
  19.     int column = 0;  
  20.     int rows = 0;  
  21.     //Create buttons and add to grid at runtime  
  22.     for (int i = 0; i < findmyNameChar.Length; i++) {  
  23.         puzzleButtons[i] = new Button();  
  24.         puzzleButtons[i].Name = findmyNameChar[i].ToString() + i.ToString();  
  25.         puzzleButtons[i].FontSize = 16;  
  26.         puzzleButtons[i].Background = new SolidColorBrush(Windows.UI.Colors.OrangeRed);  
  27.         puzzleButtons[i].Foreground = new SolidColorBrush(Windows.UI.Colors.Black);  
  28.         puzzleButtons[i].Content = ""// findmyNameChar[i].ToString();  
  29.         puzzleButtons[i].HorizontalAlignment = HorizontalAlignment.Left;  
  30.         puzzleButtons[i].VerticalAlignment = VerticalAlignment.Top;  
  31.         puzzleButtons[i].Margin = new Thickness(xVal, yVal, 0, 0);  
  32.         puzzleButtons[i].Click += new RoutedEventHandler(puzzleButton_Click);  
  33.         // puzzleButtons[i].Width = 20;  
  34.         puzzleButtons[i].Height = boxHeight;  
  35.         pnlButtons.Children.Add(puzzleButtons[i]);  
  36.         xVal = xVal + boxWidth + 10;  
  37.         column = column + 1;  
  38.         if (xVal + 100 >= pnlButtons.Width) {  
  39.             rows = rows + 1;  
  40.             column = 0;  
  41.             xVal = 4;  
  42.             yVal = yVal + boxHeight + 24;  
  43.         }  
  44.     }  
  45. }  
In the dynamically created button click, we will check for the clicked character and compare with first clicked, presently clicked character and display the message. In the code part, we can see the comments for each line, which explains in detail about the program logic.

Note

To use the message box, here we have to use the async in our button click event.
  1. private async void puzzleButton_Click(object sender, RoutedEventArgs e) {  
  2.         // we store the currently clicked button  
  3.         Button puzzleButton = (Button) sender;  
  4.         //If the currently clicked button already clicked we give the message to click other button to play game.  
  5.         if (puzzleButton.Content != null) {  
  6.             MessageDialog msgbox2 = new MessageDialog("You have already clicked this !Kindly click another puzzle button :)""Warning");  
  7.             await msgbox2.ShowAsync();  
  8.             return;  
  9.         }  
  10.         //first we increment the value to check the first and secound clicked character  
  11.         clickCount = clickCount + 1;  
  12.         //we get the clicked button character and display in button   
  13.         string clickedChar = puzzleButton.Name[0].ToString();  
  14.         puzzleButton.Content = clickedChar;  
  15.         //If user clikced for first time we store the button clicked character informaiton   
  16.         if (clickCount == 1) {  
  17.             oldChar = clickedChar;  
  18.             oldButton = (Button) sender;  
  19.             newChar = "";  
  20.         } else if (clickCount == 2) //If the user click for the second time we store the latest button clicked information  
  21.         {  
  22.             clickCount = 0;  
  23.             newChar = clickedChar;  
  24.             newButton = (Button) sender;  
  25.             //we compare both previous and newly clicked button character and if both match we vie the message as correct.  
  26.             if (oldChar == newChar) {  
  27.                 totalClickCount = totalClickCount + 1;  
  28.                 ansCount = ansCount + 1;  
  29.                 // if user correctly clickt the answer continuously 3 time we give him congrats msg with as he's genius in this game.  
  30.                 if (ansCount == 3) {  
  31.                     ansCount = 0;  
  32.                     MessageDialog msgbox2 = new MessageDialog("Wow you did 3 time and you are the genius now :)""Congrats");  
  33.                     await msgbox2.ShowAsync();  
  34.                 } else {  
  35.                     MessageDialog msgbox2 = new MessageDialog("Its perfect :)""Congrats");  
  36.                     await msgbox2.ShowAsync();  
  37.                 }  
  38.             } else {  
  39.                 //If we not both the previous and presnt button has same character we give sorry try again message to the user and clear the data to play again.  
  40.                 newButton.Content = "";  
  41.                 oldButton.Content = "";  
  42.                 if (ansCount > 0) {  
  43.                     ansCount = ansCount - 1;  
  44.                     // ansCount = 0;  
  45.                 }  
  46.                 MessageDialog msgbox2 = new MessageDialog("Sorry !Try Again :(""Try Again");  
  47.                 await msgbox2.ShowAsync();  
  48.             }  
  49.         }  
  50.     }  
  51.     // This is just a sample script. Paste your real code (javascript or HTML) here.  
  52. if ('this_is' == /an_example/) {  
  53.     of_beautifier();  
  54. else {  
  55.     var a = b ? (c % d) : e[f];  
  56. }  
Run in Windows Emulator

Select Emulator and click run. Here, I have used Emulator 10.0.10 UVGA 4 inch.

game

When we run the app, we can see our program will be running in Emulator.

game

Start New Game

Click Start New Game button to start our game. Afterwards, click this button. We can see buttons will be created on our grid dynamically. The button text will be empty, as we need to find the matching characters of any 2 buttons.

game

Correct Answer

If the member clicks the first button and second button has the same character, we can see the Congrats message, as shown below.

game

Genius Player

If user continuously wins 3 times, then he/she is the genius of this game.

game

Wrong Answer

If the user clicks first and second button, and it doesn’t have the same character, then he/she loses the game and can play again.

game

Warning Message

If the user clicks the same button already selected as an answer, then he/she can’t click on the same button. The app will give the message to click other buttons to play the game.

game

Hope, you liked Windows Mobile Character Matching game. Download it and have try by yourself. If you have any questions, kindly leave a comment.


Similar Articles