Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

random value buttongetkey

Discussion in 'Scripting' started by Rbert, Sep 22, 2014.

  1. Rbert

    Rbert

    Joined:
    Jul 13, 2014
    Posts:
    28
    i want to have a getkey for a random value..heres my code..can you help me

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using System.Data;
    5. using Mono.Data.SqliteClient;
    6.  
    7.  
    8. public class play : MonoBehaviour {
    9.     public GUISkin style;
    10.     public Texture[] icon;
    11.     public string answer;
    12.     public string x = "";
    13.     public ArrayList Let = new ArrayList ();
    14.     public ArrayList letters = new ArrayList ();
    15.     public string l = "QWERTYUIOPASDFGHJKLZXCVBNM";
    16.     public string c = "";
    17.     public string[] files;
    18.     public ArrayList exampleFiles;
    19.     public string whatToGuess;
    20.     public ArrayList isAvailable;
    21.     public ArrayList toBeRemoved;
    22.     //file
    23.     public string word;
    24.     public string tagalog;
    25.     public string english;
    26.     public string complete;
    27.     public bool hasRun = false;
    28.     public bool[] completed = new bool[6];
    29.     public bool fin;
    30.     public bool nahulaan=false;
    31.     public int cc;
    32.     public int lvl;
    33.     public bool ok;
    34.     public int maxlvl;
    35.     void OnGUI () {
    36.         if (ok == true)
    37.         {
    38.            
    39.             GUI.skin = style;
    40.             try
    41.             {
    42.                 //buttons image
    43.                 GUI.Box (new Rect (370, 330, 100, 30), "");
    44.                 if(GUI.Button(new Rect(540,288,145,35), "NEXT WORD")) {
    45.                     Application.LoadLevel(0);
    46.                 }
    47.                 if (GUI.Button (new Rect (200,40,140,140),new GUIContent (icon[0])))
    48.                 {
    49.                    
    50.                 }
    51.                 if (GUI.Button (new Rect (370,40,140,140),new GUIContent (icon[1])))
    52.                 {
    53.                    
    54.                 }
    55.                 if (GUI.Button (new Rect (520,40,140,140),new GUIContent (icon[2])))
    56.                 {
    57.                    
    58.                 }
    59.                 if (GUI.Button (new Rect (670,40,140,140),new GUIContent (icon[3])))
    60.                 {
    61.                    
    62.                 }
    63.                
    64.                 //1st six buttons
    65.                 if(fin==false)
    66.                 {
    67.                     for (int i=0; i<6; i++) {
    68.                         if (isAvailable [i] == "1") {
    69.                             if ((GUI.Button (new Rect (300 + ((i % 6) * 40), 250, 35, 35), "" + letters [i])) && (Let.Count < answer.Length)) {
    70.                                 Let.Add (letters [i]);
    71.                                 isAvailable [i] = 0;
    72.                                 toBeRemoved.Add (i);
    73.                             }
    74.                         }
    75.                         else
    76.                         {
    77.                             GUI.Button (new Rect (300 + ((i % 6) * 40), 250, 35, 35), "");
    78.                         }
    79.                     }
    80.                     //2nd six buttons
    81.                     for (int i=6; i<12; i++) {
    82.                         if (isAvailable [i] == "1") {
    83.                             if ((GUI.Button (new Rect (300 + ((i % 6) * 40), 288, 35, 35), "" + letters [i])) && (Let.Count < answer.Length)) {
    84.                                 Let.Add (letters [i]);
    85.                                 isAvailable [i] = 0;
    86.                                 toBeRemoved.Add (i);
    87.                             }
    88.                         }
    89.                         else
    90.                         {
    91.                             GUI.Button (new Rect (300 + ((i % 6) * 40), 288, 35, 35), "");
    92.                         }
    93.                        
    94.                     }
    95.                     //BOX
    96.                     for (int i=0; i<answer.Length; i++) {
    97.                         if (i < 6) {
    98.                             GUI.Button (new Rect (300 + ((i % 6) * 40), 200, 35, 35), "");
    99.                         } else if ((i >= 6) && (i < 12)) {
    100.                             GUI.Button (new Rect (540 + ((i % 6) * 40), 200, 35, 35), "");
    101.                         }
    102.                     }
    103.                     //revert
    104.                     if ((GUI.Button (new Rect (540, 250, 35, 35), "<"))&&(nahulaan==false)) {
    105.                         if (Let.Count > 0) {
    106.                             Let.RemoveAt (Let.Count - 1);
    107.                             isAvailable [(int)toBeRemoved [toBeRemoved.Count - 1]] = "1";
    108.                             toBeRemoved.RemoveAt (toBeRemoved.Count - 1);
    109.                         }                  
    110.                     }
    111.                     //ANSWERS
    112.                     x = "";
    113.                     for (int i=0; i<Let.Count; i++) {
    114.                         if (i < 6) {
    115.                             GUI.Button (new Rect (300 + ((i % 6) * 40), 200, 35, 35), "" + Let [i]);
    116.                         } else if ((i >= 6) && (i < 12)) {
    117.                             GUI.Button (new Rect (540 + ((i % 6) * 40), 200, 35, 35), "" + Let [i]);
    118.                         }
    119.                        
    120.                         x = x + Let [i];
    121.                     }
    122.                     if ((x == answer)&&(nahulaan==false)) {
    123.                        
    124.                         string _strDBName = "URI=file:"+Application.dataPath+"/dbfile";
    125.                        
    126.                         IDbConnection _connection = new SqliteConnection(_strDBName);
    127.                         IDbCommand _command = _connection .CreateCommand();
    128.                         string sql;
    129.                        
    130.                         _connection.Open();
    131.                        
    132.                         //                    sql = "CREATE TABLE IF NOT EXISTS words (word VARCHAR(20), tagalog VARCHAR(50), english VARCHAR(50), level INT(), primary key(word))";
    133.                         //                    _command.CommandText = sql;
    134.                         //                    _command.ExecuteNonQuery();
    135.                        
    136.                         _command.CommandText = "INSERT INTO answered VALUES ('"+answer+"')";
    137.                         _connection.Open ();
    138.                         _command.ExecuteNonQuery();
    139.                         _connection.Close();
    140.                         nahulaan = true;
    141.                         x = "";  
    142.                     }
    143.                     if(nahulaan==true)
    144.                     {
    145.                         //meanings!!
    146.                         GUI.Box (new Rect (370, 330, 100, 30), "CORRECT!");
    147.                        
    148.                         GUI.Label (new Rect (200, 370, 300, 50), tagalog);
    149.                         GUI.Label (new Rect (400, 370, 500, 100), english);
    150.                        
    151.                        
    152.                     }
    153.                 }
    154.                
    155.                
    156.             }
    157.             catch(IOException errr)
    158.             {
    159.                 Debug.Log(errr.Message);
    160.             }
    161.  
    162.         }
    163.  
    164.        
    165.        
    166.     }
    167.    
    168.     // Use this for initialization
    169.     void Start ()
    170.     {
    171.  
    172.         cc = 0;
    173.  
    174.         isAvailable = new ArrayList ();
    175.         toBeRemoved = new ArrayList ();
    176.         exampleFiles = new ArrayList ();
    177.  
    178.         string _strDBName = "URI=file:" + Application.dataPath + "/dbfile";
    179.  
    180.         IDbConnection _connection = new SqliteConnection (_strDBName);
    181.         IDbCommand _command = _connection .CreateCommand ();
    182.         string sql;
    183.  
    184.         _connection.Open ();
    185.  
    186.         sql = "CREATE TABLE IF NOT EXISTS words (word VARCHAR(20), tagalog VARCHAR(50), english VARCHAR(50))";
    187.         _command.CommandText = sql;
    188.         _command.ExecuteNonQuery ();
    189.  
    190.         _command.CommandText = "select level from words order by level DESC limit 1;";
    191.         _connection.Open ();
    192.         IDataReader _reader = _command.ExecuteReader ();
    193.         while (_reader.Read())
    194.         {
    195.             maxlvl = _reader.GetInt32(0);
    196.         }
    197.  
    198.         _command.CommandText = "select * from lvlmax order by level DESC limit 1;";
    199.         _connection.Open ();
    200.         _reader = _command.ExecuteReader ();
    201.         while (_reader.Read())
    202.         {
    203.             lvl = _reader.GetInt32(0);
    204.             Debug.Log (lvl);
    205.         }
    206.  
    207.  
    208.         _command.CommandText = "SELECT count(*) FROM words WHERE word not in (select word from answered) and level ='"+lvl+"' limit 1;";
    209.         _connection.Open ();
    210.         _reader = _command.ExecuteReader ();
    211.         while (_reader.Read())
    212.         {
    213.             if(_reader.GetInt32(0)>0)
    214.             {
    215.  
    216.                 _command.CommandText = "SELECT * FROM words WHERE word not in (select word from answered) and level ='"+lvl+"' limit 1;";
    217.                 _reader = _command.ExecuteReader ();
    218.                
    219.                 while (_reader.Read())
    220.                 {
    221.                     word = _reader.GetString(0);
    222.                     tagalog = _reader.GetString(1);
    223.                     english = _reader.GetString(2);
    224.                 }          
    225.                 _connection.Close ();
    226.                
    227.                 for (int i=0; i<12; i++)
    228.                     isAvailable.Add ("1");
    229.                
    230.                 ArrayList lstExampleFiles = new ArrayList();
    231.                 lstExampleFiles.AddRange( exampleFiles );
    232.                 int d2 = Random.Range (0, lstExampleFiles.Count);
    233.                 Debug.Log (word);
    234.                 answer = word;
    235.                 c = answer;
    236.                 for (int i=0; i<c.Length; i++) {
    237.                     letters.Add(c.Substring(i,1));
    238.                 }
    239.                 int rr = 0;
    240.                 for (int i=letters.Count; i<12; i++) {
    241.                     rr = Random.Range(0,l.Length);
    242.                     letters.Add(l.Substring(rr,1));
    243.                 }
    244.                 letters = ShuffleList (letters);      
    245.                 icon = new Texture[4];
    246.                 for(int i=0; i<4;i++)
    247.                 {
    248.                     icon[i]= (Texture)Resources.Load(answer+"/"+(i+1));
    249.                 }
    250.                 ok = true;
    251.             }
    252.             else
    253.             {
    254.                 if(maxlvl>lvl)
    255.                 {
    256.                     //per lvl
    257.  
    258.  
    259.  
    260.                     Debug.Log("CONGRATULATION. NATAPOS MO ANG LVL : " +lvl);
    261.  
    262.  
    263.                     sql = "insert into lvlmax values("+(lvl+1)+");";
    264.                     _command.CommandText = sql;
    265.                     _command.ExecuteNonQuery ();
    266.                     ok = false;
    267.  
    268.                 }
    269.                 else
    270.                 {
    271.                     //all
    272.  
    273.  
    274.                     Debug.Log("CONGRATS YOW. NATAPOS MO");
    275.  
    276.                     Application.LoadLevel(0);
    277.                 }
    278.             }
    279.         }  
    280.  
    281.     }
    282.    
    283.     // Update is called once per frame
    284.     void Update () {
    285.        
    286.     }
    287.    
    288.     private ArrayList ShuffleList(ArrayList inputList)
    289.     {
    290.         ArrayList randomList = new ArrayList();
    291.        
    292.        
    293.         int randomIndex = 0;
    294.         while (inputList.Count > 0)
    295.         {
    296.             randomIndex = Random.Range (0, inputList.Count-1); //Choose a random object in the list
    297.             randomList.Add(inputList[randomIndex]); //add it to the new, random list
    298.             inputList.RemoveAt(randomIndex); //remove to avoid duplicates
    299.         }
    300.        
    301.         return randomList; //return the new random list
    302.     }
    303. }
    304.