Xamarin.Android - Flags Quiz Game App - Part One

Introduction

In this article, we shall learn how to build a flags quiz game in Xamarin Android.

Features

  • Questions about flags of all the countries in the world
  • Time Limit: 5 seconds per question
  • Database: SQLite
  • Mode: Easy, Medium, Hard, Hardest
  • Score: Highest and lowest score based medals

Part 1

Download the data and tools

Write simple tools for creating databases query.

Step 1

Extract the flag images from the package and save them on your computer.

 

Step 2

Open Visual Studio and go to New Project-> Templates-> Visual C#-> Window Classic Desktop-> select Console Application. Give it a name as ToolCreateQuery.

 

Step 3

Next, go to Solution Explorer-> Project Name -> Program file and write the following code changing appropriate namespaces.

Note

Must change your flag images folder location before building the project.
 
 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Text.RegularExpressions;  
  7. using System.Threading.Tasks;  
  8. namespace ToolCreatQuery {  
  9.     class Program {  
  10.         //For Android...  
  11.         public static string ConvertToUnsign(string str) {  
  12.             //Convert unicode to Asxii Character  
  13.             //because Android Drawable Resource can't contains unicode.  
  14.             Regex regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");  
  15.             string temp = str.Normalize(System.Text.NormalizationForm.FormD);  
  16.             return regex.Replace(temp, String.Empty).Replace('\u0111''d').Replace('\u0110''D');  
  17.         }  
  18.         //Function to get all images names in file  
  19.         private static List < string > getListCountry() {  
  20.             DirectoryInfo d = new DirectoryInfo(@ "E:\Countries Flag");  
  21.             FileInfo[] files = d.GetFiles("*.png");  
  22.             List < string > lstNames = new List < string > ();  
  23.             foreach(var file in files) {  
  24.                 //Add this line to rename all uper file name to lower file name.  
  25.                 //because Android Drawable Resource can't contains "'" and "-" character  
  26.                 //So i remove it.  
  27.                 //Just run and get query.  
  28.                 File.Move(file.FullName, ConvertToUnsign(file.FullName.ToLower().Replace("'", String.Empty).Replace("-", String.Empty)));  
  29.                 lstNames.Add(file.Name.Replace(".png", String.Empty)); //Pakistan.png -> Pakistan  
  30.             }  
  31.             return lstNames;  
  32.         }  
  33.         //Function get random name of country from list without duplicate  
  34.         private static List < string > getNameRandom(string name, List < string > lstNames) {  
  35.             HashSet < string > myHashSet = new HashSet < string > ();  
  36.             myHashSet.Add(name); //first we need add name  
  37.             //Second, we will random some other name with no duplicate with name  
  38.             while (myHashSet.Count < 4) //we need 4 names for 4 answer  
  39.             {  
  40.                 //Add random name to HashSet  
  41.                 myHashSet.Add(lstNames.OrderBy(s => Guid.NewGuid()).First());  
  42.             }  
  43.             return myHashSet.OrderBy(s => Guid.NewGuid()).ToList(); // Random all answer  
  44.         }  
  45.         //Function to generate query INSERT INTO for create Database Question  
  46.         private static async Task genrateQuery() {  
  47.             List < string > lstQuery = new List < string > ();  
  48.             List < string > lstCountryName = getListCountry();  
  49.             string query = string.Empty;  
  50.             foreach(var name in lstCountryName) {  
  51.                 List < string > answerList = getNameRandom(name, lstCountryName);  
  52.                 //With one value in listCountryName , we create one  
  53.                 //Question with 4 answer and 1 correct answer  
  54.                 //Example  
  55.                 //INSERT INTO QuestionINSERT INTO Question (Image,AnswerA,AnswerB,AnswerC,AnswerD,CorrectAnswer)  
  56.                 //VALUES ('Pakistan','India','Russia','Germany','Pakistan');  
  57.                 query = "INSERT INTO Question (Image,AnswerA,AnswerB,AnswerC,AnswerD,CorrectAnswer)" + $ "VALUES(\"{name}\",\"{answerList[0]}\",\"{ answerList[1]}\",\"{ answerList[2]}\",\"{ answerList[3]}\",\"{name}\");";  
  58.                 lstQuery.Add(query); // Add query we just created to list  
  59.                 //Write all to File  
  60.                 System.IO.File.WriteAllLines(@ ".//QueryGenerate.txt", lstQuery);  
  61.             }  
  62.         }  
  63.         static void Main(string[] args) {  
  64.             Console.WriteLine("TOOL GENERATE QUERY 1.0");  
  65.             Console.WriteLine("Please wait...");  
  66.             genrateQuery();  
  67.             Console.WriteLine("Success...");  
  68.             Console.ReadKey();  
  69.         }  
  70.     }  
  71. }  

Just rebuild the project and go to ToolCreateQuery-> bin-> Debug and run ToolCreateQuery.exe file. The query generates a txt file in the same folder.

 

Step 4

Go to File-> New Database. A pop window will show up. Give it a name like MyDB and location where you want to save.

 

Step 5

Now, add a new table with name Question and add 7 fields - ID, Image, AnswerA, AnswerB, AnswerC, AnswerD, CorrectAnswer.

Step 6c

Next, add another table with the name Ranking and give two fields - Id and Score type. Score type must be a real number.

 

Step 7

Next, add a final table of the database with name UserPlayCount and add two fields - Level, PlayCount.

 

Step 8

Open Query-generated txt file, copy all the queries, and open DB Browser for SQLite.

 

Click on "Execute SQL" and paste all the queries in it. Press the "Start" button. It writes the changes. We have created an SQLite database for Android application.