Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.20 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Text;// For Stringbuilder
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7.  
  8.  
  9. public class GameManager : MonoBehaviour {
  10.     //list that will hold all of the words that are avaliable to be guessed
  11.     public List<Word> wordList;
  12.     //this is the word that is chosen to be guessed for
  13.     public string wordToGuess;
  14.     //this is whats actually displayed on the screen
  15.     public Text wordDisplay;
  16.     //what the player has guessed
  17.     public string guess;
  18.     //where the guess comes from
  19.     public InputField inputText;
  20.     //a list of all the letters that have already been used
  21.     public List<string> usedLetterList;
  22.     //where the used letter list will display
  23.     public Text usedLetterDisplay;
  24.     //what category the guessed word is in
  25.     public Text categoryTxt;
  26.     //the word that is partially guessed
  27.     private string currentWord;
  28.     //how many words there are
  29.     public int wordCount;
  30.     //number of lives tha player has left
  31.     public int lives = 3;
  32.     //the button in the game that allows the game to be replayed
  33.     public GameObject playAgainButton;
  34.     //boolean that checks if the game is over
  35.     public bool gameOver = false;
  36.     //button that can be pressed to make the hint text show
  37.     public GameObject hintButton;
  38.     //the actual hint text
  39.     public Text hintText;
  40.     //a button that will quit the game when called
  41.     public GameObject quitGameButton;
  42.     //number of points that the player has
  43.     public int points;
  44.     //how the points will be displayed
  45.     public Text currentScore;
  46.     //how the lives remaining will be displayed
  47.     public Text currentLives;
  48.     //the final score the player has, this is used so the final score may be displayed more prominantly
  49.     public Text finalScore;
  50.     //checks to see if when the game ends if the player won
  51.     public bool winGame;
  52.     //the time remaining
  53.     //only used in timed mode
  54.     public Timer time;
  55.     //a string of allowed charachter, this makes it so that non alphabet charachters will not be counted against the player
  56.     private string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  57.  
  58.     // Use this for initialization, it will reset all of the variables and give the player the opportunity to enter the game or exit the game
  59.     //will also pick a word to be tested for
  60.     void Start ()
  61.     {
  62.         winGame = false;
  63.         HideHint();
  64.         HideScore();
  65.         playAgainButton.SetActive(false);
  66.         PickAWord();
  67.         inputText.ActivateInputField();
  68.         quitGameButton.SetActive(false);
  69.         currentLives.text = "Lives: " + lives;
  70.         currentScore.text = "Score: " + points;
  71.  
  72.  
  73.      
  74.     }
  75.     //makes it so that the cursor is always in the input field so the player may always type
  76.     private void Update()
  77.     {
  78.         inputText.ActivateInputField();
  79.     }
  80.  
  81.     //makes it so that the word is hidden to the player
  82.     void MaskWord(string input, char target)
  83.     {
  84.         StringBuilder sb = new StringBuilder(wordToGuess.Length);
  85.         for(int i = 0; i < wordToGuess.Length; i++)
  86.         {
  87.             sb.Append(target);
  88.         }
  89.         currentWord = sb.ToString();
  90.         //Update word to text on screen
  91.         wordDisplay.text = currentWord;
  92.     }
  93.     //turns the *'s to letters as the letters are guessed
  94.     void UnMaskWord(string input, char target)
  95.     {
  96.         //input.ToLower();
  97.         StringBuilder sb = new StringBuilder(wordToGuess.Length);
  98.         for (int i = 0; i < wordToGuess.Length; i++)
  99.         {
  100.             if(input.ToLower() == wordToGuess[i].ToString().ToLower())
  101.             {
  102.                 sb.Append(target);
  103.                 wordCount++;
  104.             }
  105.             else
  106.             {
  107.                 sb.Append(currentWord[i]);
  108.             }
  109.         }
  110.         currentWord = sb.ToString();
  111.         //Update word to text on screen
  112.         currentWord = currentWord.Substring(0, 1).ToUpper() + currentWord.Substring(1).ToLower();
  113.         wordDisplay.text = currentWord;
  114.         //What condition can be checked to see if we've won or not?
  115.         if(wordCount == wordToGuess.Length)
  116.         {
  117.             winGame = true;
  118.             EndGame();
  119.         }
  120.     }
  121.     //this picks the word to be guessed by the player
  122.     void PickAWord()
  123.     {
  124.         int selectedWord = Random.Range(0, wordList.Count-1);
  125.         Debug.Log("selectedWord int : " + selectedWord);
  126.         wordToGuess = wordList[selectedWord].word;
  127.         categoryTxt.text = wordList[selectedWord].category;
  128.         hintText.text = wordList[selectedWord].hint;
  129.         //wordList.Remove(wordList[selectedWord]);
  130.         currentWord = wordToGuess;
  131.         MaskWord(currentWord, '*');
  132.     }
  133.     //this compares the letter the player guesses with the word they are guessing for
  134.     public void LetterGuess()
  135.     {
  136.         if(gameOver == false)
  137.         {
  138.             if (inputText.text != string.Empty)
  139.             {
  140.                 guess = inputText.text.ToLower();
  141.                 AddLetterGuess();
  142.             }
  143.         }
  144.  
  145.     }
  146.     //this checks if the letter guessed is in the word and if it is it will unmask the guessed letter
  147.     //otherwise it will subtract a life
  148.     void CheckWordForLetter()
  149.     {
  150.         if (wordToGuess.Contains(guess))
  151.         {
  152.             //Display letter
  153.             UnMaskWord(guess, guess[0]);
  154.             //Update word to text on screen
  155.             //wordDisplay.text = wordToGuess;
  156.         }
  157.         else
  158.         {
  159.             lives--;
  160.             currentLives.text = "Lives: " + lives;
  161.             if(lives == 0)
  162.             {
  163.                 EndGame();
  164.             }
  165.         }
  166.     }
  167.     //this tests the letters that are being used to see if theyre letters and if theyre actually in the word
  168.     void AddLetterGuess()
  169.     {
  170.         if (!usedLetterList.Contains(guess) && (alphabet.Contains(guess)))
  171.         {
  172.             CheckWordForLetter();
  173.             usedLetterList.Add(guess);
  174.             string tempText = "";
  175.             foreach (string str in usedLetterList)
  176.             {
  177.                 tempText += str;
  178.             }
  179.             usedLetterDisplay.text = tempText;
  180.         }
  181.         ResetGuess();
  182.     }
  183.     //this will make it so that the player can enter a new guess
  184.     void ResetGuess()
  185.     {
  186.         guess = string.Empty;
  187.         inputText.text = string.Empty;
  188.         inputText.ActivateInputField();
  189.     }
  190.     //this ends the game and makes it so that if they win they get a new word and if they lose the game ends
  191.     public void EndGame()
  192.     {
  193.         if (winGame == true)
  194.         {
  195.             points++;
  196.             //this will only be entered if they are playing the timed game mode
  197.             if ((SceneManager.GetActiveScene()).name == "Timed Game Mode")
  198.             {
  199.                 Invoke("Time.ResetTime()", 3);
  200.             }
  201.             currentScore.text = "Score: " + points;
  202.             Debug.Log("User won");
  203.             gameOver = true;
  204.             wordDisplay.fontSize = 48;
  205.             Invoke("ResetGame", 3);
  206.             //this line is in here twice and it sholdnt be i think
  207.             //Invoke("Time.ResetTime()", 3);
  208.             inputText.gameObject.SetActive(false);
  209.         }
  210.         //this is if the player lost
  211.         else
  212.         {
  213.             DisplayScore();
  214.             categoryTxt.gameObject.SetActive(false);
  215.             playAgainButton.SetActive(true);
  216.             quitGameButton.SetActive(true);
  217.             Debug.Log("User lost");
  218.             gameOver = true;
  219.             inputText.gameObject.SetActive(false);
  220.             ResetScore();
  221.             //should this line be here?
  222.         }
  223.     }
  224.    
  225.     //this will reset all of the used variables and give them the options to play again or quit
  226.     public void ResetGame()
  227.     {
  228.         categoryTxt.gameObject.SetActive(true);
  229.         playAgainButton.SetActive(false);
  230.         quitGameButton.SetActive(false);
  231.         winGame = false;
  232.         gameOver = false;
  233.         inputText.gameObject.SetActive(true);
  234.         wordDisplay.fontSize = 36;
  235.         lives = 3;
  236.         if ((SceneManager.GetActiveScene()).name == "Timed Game Mode")
  237.         {
  238.             Debug.Log("it entered");
  239.             time.ResetTime();
  240.         }
  241.         currentLives.text = "Lives: " + lives;
  242.         usedLetterList.Clear();
  243.         usedLetterDisplay.text = string.Empty;
  244.         ResetGuess();
  245.         wordCount = 0;
  246.         HideHint();
  247.         PickAWord();
  248.  
  249.     }
  250.     //lets the player see a hint
  251.     public void DisplayHint()
  252.     {
  253.         hintText.gameObject.SetActive(true);
  254.         hintButton.SetActive(false);
  255.     }
  256.     //hides the hint
  257.     public void HideHint()
  258.     {
  259.         hintText.gameObject.SetActive(false);
  260.         hintButton.SetActive(true);
  261.     }
  262.     //shows the score the player has
  263.     public void DisplayScore()
  264.     {
  265.         finalScore.gameObject.SetActive(true);
  266.         finalScore.text = ("Final Score: " + points);
  267.     }
  268.     //hides the players score
  269.     public void HideScore()
  270.     {
  271.         finalScore.gameObject.SetActive(false);
  272.     }
  273.     //when the player loses their score will reset
  274.     public void ResetScore()
  275.     {
  276.         points = 0;
  277.        
  278.     }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement