Stef Monson

Triviality - A+ Edition

This knock-off Jeopardy game uses questions related to the CompTIA A+ certification and was originally created in Java as a college midterm project, and was re-created from memory using Unity 2017.4.1f1 in an effort to learn C# and Unity Real-Time Development Platform.

Language: C#
Development Tool: Unity 2017.4.1f1

Code Samples

//Imports
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
using Mono.Data.Sqlite;
using System.Data;
using System;
  
public class Gameplay : MonoBehaviour {
/* Handles the game play mechanics of the category board, question and answer screens
 *  -Derived from MonoBehavior class to handle Unity scripting features
 *  (Called by EventSystem of the Board scene) */
  
  //Variable Declarations
  public static int pointTotal; //Keeps running point total for endgame
  int points; //stores point total for last button pressed
  System.Random rnd = new System.Random (); //random number generation
  string sqlQuery; //holds SQL query for passing into Query function
  int questionID; //stores ID of current question for answer parsing
  int questioncount = 0; //stores number of questions answered for endgame checking
  string guess; //stores current guess for check against query result
  
  void Start () {
  /* Initializes the game
   * Screen handling uses groups of objects to display question, answer and board
   * positioning them into the FOV of the camera. This order to minimizes the need for 
   * communication between scenes and remembering which buttons have been deactivated
   * between repetitive reloads of each screen
   *   (Called by loading the "Game" scene */
    
    //Make sure Question and Answer screens are not displayed
    GameObject.FindGameObjectWithTag ("Question")
      .transform.position = new Vector3 (-1000.0f, -1000f, 0.0f);
    GameObject.FindGameObjectWithTag ("Answer")
      .transform.position = new Vector3 (-1000.0f, -1000f, 0.0f);
  
    //set points and point total to zero
    points = 0;
    pointTotal = 0;
  }//end Start()
  
  string Query (string sqlQuery, bool q) {
  /* Queries the proper table for questions and answers
   *    -string sqlQuery - query built by calling function
   *    -bool q - true if query is for a question, false if query is for an answer
   *    -return result - text to be displayed on the next screen
   *    (Called from other functions inside of Gameplay.cs) */
  
    string result;
    //Path to SQLite3 database
    string conn = "URI=file:" + Application.dataPath + "/DB/questions.db";
  
    //Creates new Sqlite connection
    IDbConnection dbconn = (IDbConnection)new SqliteConnection (conn);
    //Opens a connection to the database
    dbconn.Open ();
  
    //Build the sql command using passed in string sqlQuery
    IDbCommand dbcmd = dbconn.CreateCommand ();
    dbcmd.CommandText = sqlQuery;
  
    //Execute query
    IDataReader reader = dbcmd.ExecuteReader ();
  
    //List objects that hold results of the query
    List qID = new List ();
    List row = new List ();
  
    //Reads each line of response and stores them in list objects
    while (reader.Read ()) {
      qID.Add (reader.GetInt32 (0));
      row.Add (reader.GetString (1));
    } //end while (reader.Read)
  
    //If we are querying for a question, generate a random number from the results and 
    //return the corresponding question
    //This allows for each category button to have multiple possible questions so it is 
    //not always the same game
    if (q) {
      int i = rnd.Next (0, row.Count);
  
      questionID = qID [i];//Remember question ID for later answer query
      result = row [i];
    } //end if (q)
  
    //If we are querying for an answer, check the guessed answer with all possible results
    //associated with question ID
      //This allows for multiple acceptible answers to each question based on spelling or 
      //grammatical additions
    else {
      //Default result is incorrect and displays first answer found in query as correct
      result = "Incorrect!\n\nThe Answer was:\n" + row [0];
  
      //for loop changes result to true if guess matches any of the returned answers
      for (int i = 0; i < row.Count; i++) {
        //match against .ToLower() so as not to discriminate correctness based on 
        //capitalization
        if (row [i].ToLower () == guess.ToLower ()) { 
          pointTotal += points;
          result = "For $"
          + points
          + " you are correct!\n\nTotal:\n $"
          + pointTotal; //show current point total
        } //end if (row[i] == guess)
  
      } //end for (i < row.Count) {
    } //end else
  
    //Close, dispose and clear reader and database connection
    reader.Close ();
    reader = null;
    dbcmd.Dispose ();
    dbcmd = null;
    dbconn.Close ();
    dbconn = null;
  
    return result;
  } //end Query()
  
  public void ButtonPressed (UnityEngine.UI.Button btnPressed) {
  /* Disables the button that was pressed and queries the database for a question matching 
   * its category and difficulty
   *   -Button btnPressed - the button that was pressed holds the category and difficulty 
   *    information in a "tag"
   *   (Called by button press on category board) */
  
    //Disable button pressed and clear text field
    btnPressed.interactable = false;
    btnPressed.GetComponentInChildren ().text = "";
  
    //Increase question count (for end-game initiation)
    questioncount += 1;
  
    //Build query string
    sqlQuery = "Select Q_ID, question from questions where CAT_ID = "
    + btnPressed.name.Substring (0, 1)
    + " AND Difficulty = "
    + btnPressed.name.Substring (2, 1) + ";";
  
    points = int.Parse (btnPressed.name.Substring (2, 1)) * 100;
  
    //Run query
    string question = Query (sqlQuery, true);
  
    //Set Question Text to query result
    GameObject.FindGameObjectWithTag ("QText")
      .GetComponentInChildren ().text = question;
  } //end ButtonPressed()
  
  public void CheckAnswer (UnityEngine.UI.InputField guessbox) {
  /* Queries the database for the answer to the current question and parses it against the 
   * guess entered
   *   -InputField guessbox - the field containing the players guess
   *   (Called by button press on Question screen) */
  
    //Sets guess for parse
    guess = guessbox.text;
    //resets guessbox text so it will say "Enter answer here..." again instead of the last
    //answer entered
    guessbox.text = "";
  
    //Build query string
    sqlQuery = "Select Q_ID, answer from answers where Q_ID = " + questionID + ";";
  
    //Run query
    string answer = Query (sqlQuery, false);
  
    //Set Answer Text to Query result
    GameObject.FindGameObjectWithTag ("AText")
      .GetComponentInChildren ().text = answer;
  } //end CheckAnswer()
  
  public void CheckEndGame () {
  /* Function "CheckEndGame" checks the questioncount to determine whether or not all of
   * the questions have been answered
   *   (Called by button press on Answer Screen) */
  
    //If player has answered all 20 questions, load the EndGame scene
    if (questioncount == 20) {
      SceneManager.LoadScene ("EndGame");
    } //end if (questiocount == 20)
  } //end CheckEndGame ()
} //end GamePlay class
Q_IDQuestionCAT_IDDifficulty
56 What device allows multiple PCs to share a monitor? 5 1
57 What connection type is the best to allow a group of users to be able to access the same printer? 5 1
58 What is the maximum capacity of a CD-R? 5 1
59 If you want to utilize your motherboard's dual-channel RAM feature, what type of RAM do you need to use? 5 2
60 What connector would you use to connect a video card to a power supply 5 2
61 What display connector has 15-pins? 5 2
62 What type of printer uses an imaging drum? 5 3
63 What type of printer would have a maintenance kit that includes a fuser, transfer roller and pickup rollers? 5 3
64 What type of drive is the most resistant to physical shock? 5 3
65 What level of RAID increases drive size and access speed without providing any type of data redundancy? 5 4
66 What type of connector would you use on a CAT5 cable? 5 4
67 When setting up an LCD monitor, which resolution mode, by definition, provides the best visual quality? 5 4
Q_IDAnswer
56 KVM Switch
56 KVM
56 keyboard video mouse switch
57 Ethernet
58 700 MB
58 700mb
59 DDR
60 PCIe 6-pin
60 pcie 6 pin
60 6-pin pcie
60 6 pin pcie
61 VGA
62 Inkjet
62 an inkjet printer
62 inkjet printer
63 Laser
63 laser printer
63 a laser printer
64 Solid State Drive
64 solid state disk
64 ssd
65 RAID 0
65 0
66 RJ-45
66 RJ45
66 RJ 45
67 Native