Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6.  
  7.  
  8. public class game_main : MonoBehaviour {
  9.    
  10.     [Header("Buttons")]
  11.         public Button key_1_btn;
  12.         public Button key_2_btn;
  13.         public Button key_3_btn;
  14.         public Button key_4_btn;
  15.         public Button key_5_btn;
  16.         public Button key_6_btn;
  17.         public Button key_7_btn;
  18.         public Button key_8_btn;
  19.         public Button key_9_btn;
  20.         public Button key_0_btn;
  21.         public Button key_clear_btn;
  22.         public Button key_enter_btn;
  23.        
  24.        
  25.     [Header("Text")]
  26.         public Text guess_text;
  27.        
  28.         public Text timer_text;
  29.         public Text tries_text;
  30.    
  31.    
  32.    
  33.     private int upper_limit;
  34.    
  35.     private int answer;
  36.     private int number_of_guesses;
  37.    
  38.     private string current_guess;
  39.    
  40.     private float timeLeft = 123.0f;
  41.    
  42.    
  43.    
  44.     //Here you setup the component you are on right now (the "this" object). You should use Awake to set up references between scripts.
  45.     void Awake() {
  46.         current_guess = "";
  47.        
  48.         guess_text.text = current_guess;
  49.        
  50.         number_of_guesses = 0;
  51.        
  52.         upper_limit = 1000000;
  53.         answer = Random.Range(0,upper_limit);
  54.        
  55.         tries_text.text = "Tries: " + number_of_guesses.ToString();
  56.     }
  57.    
  58.     //Here you setup things that depend on other components. Use Start to pass any information back and forth.
  59.     void Start() {
  60.         Button btn_1 = key_1_btn.GetComponent<Button>();
  61.         btn_1.onClick.AddListener(key_1_btn_task);
  62.        
  63.         Button btn_2 = key_2_btn.GetComponent<Button>();
  64.         btn_2.onClick.AddListener(key_2_btn_task);
  65.        
  66.         Button btn_3 = key_3_btn.GetComponent<Button>();
  67.         btn_3.onClick.AddListener(key_3_btn_task);
  68.        
  69.         Button btn_4 = key_4_btn.GetComponent<Button>();
  70.         btn_4.onClick.AddListener(key_4_btn_task);
  71.        
  72.         Button btn_5 = key_5_btn.GetComponent<Button>();
  73.         btn_5.onClick.AddListener(key_5_btn_task);
  74.        
  75.         Button btn_6 = key_6_btn.GetComponent<Button>();
  76.         btn_6.onClick.AddListener(key_6_btn_task);
  77.        
  78.         Button btn_7 = key_7_btn.GetComponent<Button>();
  79.         btn_7.onClick.AddListener(key_7_btn_task);
  80.        
  81.         Button btn_8 = key_8_btn.GetComponent<Button>();
  82.         btn_8.onClick.AddListener(key_8_btn_task);
  83.        
  84.         Button btn_9 = key_9_btn.GetComponent<Button>();
  85.         btn_9.onClick.AddListener(key_9_btn_task);
  86.        
  87.         Button btn_0 = key_0_btn.GetComponent<Button>();
  88.         btn_0.onClick.AddListener(key_0_btn_task);
  89.        
  90.         Button btn_clear = key_clear_btn.GetComponent<Button>();
  91.         btn_clear.onClick.AddListener(key_clear_btn_task);
  92.        
  93.         Button btn_enter = key_enter_btn.GetComponent<Button>();
  94.         btn_enter.onClick.AddListener(key_enter_btn_task);
  95.     }
  96.    
  97.     //Use this when applying physics forces as this function relies on the physics engine.
  98.     void FixedUpdate() {
  99.         //
  100.     }
  101.    
  102.     //Update is called once per frame, it's speed depends on the load on the graphics engine.
  103.     void Update() {
  104.         if (Input.GetKeyDown(KeyCode.Keypad1))      { key_1_btn_task(); }
  105.         if (Input.GetKeyDown(KeyCode.Keypad2))      { key_2_btn_task(); }
  106.         if (Input.GetKeyDown(KeyCode.Keypad3))      { key_3_btn_task(); }
  107.         if (Input.GetKeyDown(KeyCode.Keypad4))      { key_4_btn_task(); }
  108.         if (Input.GetKeyDown(KeyCode.Keypad5))      { key_5_btn_task(); }
  109.         if (Input.GetKeyDown(KeyCode.Keypad6))      { key_6_btn_task(); }
  110.         if (Input.GetKeyDown(KeyCode.Keypad7))      { key_7_btn_task(); }
  111.         if (Input.GetKeyDown(KeyCode.Keypad8))      { key_8_btn_task(); }
  112.         if (Input.GetKeyDown(KeyCode.Keypad9))      { key_9_btn_task(); }
  113.         if (Input.GetKeyDown(KeyCode.Keypad0))      { key_0_btn_task(); }
  114.         if (Input.GetKeyDown(KeyCode.KeypadEnter))  { key_enter_btn_task(); }
  115.         if (Input.GetKeyDown(KeyCode.KeypadPeriod)) { key_clear_btn_task(); }
  116.         if (Input.GetKeyDown(KeyCode.KeypadMinus))  { key_back_btn_task();  }
  117.        
  118.         timeLeft -= Time.deltaTime;
  119.         float min = (timeLeft/60);
  120.         float sec = timeLeft % 60;
  121.         float mil = 0;
  122.         //timer_text.text = string.Format("{0:0}:{1:00}:{2:00}", min, sec, mil);
  123.         timer_text.text = min + ":" + sec + ":" + mil;
  124.     }
  125.    
  126.     void key_1_btn_task() {
  127.         if (current_guess.Length >= 10) {
  128.             //Debug.Log ("END");
  129.         }else{
  130.             //Debug.Log ("1");
  131.             current_guess = current_guess + "1";
  132.             guess_text.text = current_guess;
  133.         }
  134.     }
  135.     void key_2_btn_task() {
  136.         if (current_guess.Length >= 10) {
  137.             //Debug.Log ("END");
  138.         }else{
  139.             //Debug.Log ("2");
  140.             current_guess = current_guess + "2";
  141.             guess_text.text = current_guess;
  142.         }
  143.     }
  144.     void key_3_btn_task() {
  145.         if (current_guess.Length >= 10) {
  146.             //Debug.Log ("END");
  147.         }else{
  148.             //Debug.Log ("3");
  149.             current_guess = current_guess + "3";
  150.             guess_text.text = current_guess;
  151.         }
  152.     }
  153.     void key_4_btn_task() {
  154.         if (current_guess.Length >= 10) {
  155.             //Debug.Log ("END");
  156.         }else{
  157.             //Debug.Log ("4");
  158.             current_guess = current_guess + "4";
  159.             guess_text.text = current_guess;
  160.         }
  161.     }
  162.     void key_5_btn_task() {
  163.         if (current_guess.Length >= 10) {
  164.             //Debug.Log ("END");
  165.         }else{
  166.             //Debug.Log ("5");
  167.             current_guess = current_guess + "5";
  168.             guess_text.text = current_guess;
  169.         }
  170.     }
  171.     void key_6_btn_task() {
  172.         if (current_guess.Length >= 10) {
  173.             //Debug.Log ("END");
  174.         }else{
  175.             //Debug.Log ("6");
  176.             current_guess = current_guess + "6";
  177.             guess_text.text = current_guess;
  178.         }
  179.     }
  180.     void key_7_btn_task() {
  181.         if (current_guess.Length >= 10) {
  182.             //Debug.Log ("END");
  183.         }else{
  184.             //Debug.Log ("7");
  185.             current_guess = current_guess + "7";
  186.             guess_text.text = current_guess;
  187.         }
  188.     }
  189.     void key_8_btn_task() {
  190.         if (current_guess.Length >= 10) {
  191.             //Debug.Log ("END");
  192.         }else{
  193.             //Debug.Log ("8");
  194.             current_guess = current_guess + "8";
  195.             guess_text.text = current_guess;
  196.         }
  197.     }
  198.     void key_9_btn_task() {
  199.         if (current_guess.Length >= 10) {
  200.             //Debug.Log ("END");
  201.         }else{
  202.             //Debug.Log ("9");
  203.             current_guess = current_guess + "9";
  204.             guess_text.text = current_guess;
  205.         }
  206.     }
  207.     void key_0_btn_task() {
  208.         if (current_guess.Length >= 10) {
  209.             //Debug.Log ("END");
  210.         }else{
  211.             //Debug.Log ("0");
  212.             current_guess = current_guess + "0";
  213.             guess_text.text = current_guess;
  214.         }
  215.     }
  216.     void key_back_btn_task() {
  217.         //Debug.Log ("back");
  218.         current_guess = current_guess.Substring(0, current_guess.Length - 1);
  219.         guess_text.text = current_guess;
  220.     }
  221.     void key_clear_btn_task() {
  222.         //Debug.Log ("clear");
  223.         current_guess = "";
  224.         guess_text.text = current_guess;
  225.     }
  226.     void key_enter_btn_task() {
  227.         //Debug.Log ("ENTER!");
  228.         //Debug.Log ("GUESS: " + current_guess);
  229.        
  230.         int temp_guess = System.Int32.Parse(current_guess);
  231.         if (temp_guess == answer) {
  232.             Debug.Log ("CORRECT!");
  233.         }else{
  234.             if (answer > temp_guess) {
  235.                 Debug.Log ("HIGHER!");
  236.             }else{
  237.                 Debug.Log ("LOWER!");
  238.             }
  239.         }
  240.            
  241.         Debug.Log ("GUESS: " + current_guess);
  242.         current_guess = "";
  243.         guess_text.text = current_guess;
  244.        
  245.         number_of_guesses++;
  246.         tries_text.text = "Tries: " + number_of_guesses.ToString();
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement