Advertisement
Guest User

Code for hangman

a guest
Apr 16th, 2013
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. public class Hangman : MonoBehaviour
  6. {
  7.  
  8. public string[] words;
  9. private int lifes = 10;
  10. private string userInput = "";
  11. private string guessedword = "";
  12. public string wordChosen = "";
  13.  
  14. private void Start()
  15. {
  16. wordChosen = words[Random.Range(0, words.Length)];
  17. guessedword = new string("-"[0] , wordChosen.Length);
  18. }
  19.  
  20. private void OnGui()
  21. {
  22. if (lifes > 0 && guessedword != wordChosen)
  23. {
  24.  
  25. GUILayout.Box(guessedword);
  26. userInput = GUILayout.TextField(userInput);
  27. if(GUILayout.Button("Try"))
  28. {
  29. if (userInput.Length > 1)
  30. print("Please Enter One Character At A Time :)");
  31. else
  32. checkChar(userInput[0]);
  33. }
  34. }
  35. else
  36. {
  37. if (lifes > 0)
  38. GUILayout.Box("Congratulations! You Win!");
  39. else
  40. GUILayout.Box(" Awww You Lose :(");
  41. }
  42. }
  43.  
  44. private void CheckChar(char c)
  45. {
  46. bool charExists = false;
  47. for(int x = 0; x = wordChosen.length; x++)
  48. {
  49.  
  50. if (wordChosen[x] == c)
  51. {
  52. charExists = true;
  53. string temp = guessedWord.Substring(0 , x);
  54. guessedWord = temp + c.ToString() + guessedWord.Substring(x + 1, guessedWord.Length - x - 1);
  55. }
  56. }
  57.  
  58. if(!charExists)
  59. lifes --;
  60.  
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement