Hangman test


SUBMITTED BY: Guest

DATE: Jan. 27, 2013, 7:04 p.m.

FORMAT: C#

SIZE: 1.7 kB

HITS: 5689

  1. using System;
  2. using System.Text;
  3. namespace HangmanGame {
  4. class App {
  5. static void Main() {
  6. Hangman h = new Hangman();
  7. h.Start();
  8. }
  9. }
  10. class Hangman {
  11. public void Start() {
  12. String hangman = @"
  13. ___________
  14. | |
  15. | 0
  16. | /|\
  17. | / \
  18. |
  19. |";
  20. Console.WriteLine(hangman);
  21. Console.WriteLine("Welcome to a basic hangman game. You simply try to guess random word one letter at a time");
  22. string[] words = new string[] {"test", "programming", "microsoft"};
  23. Random random = new Random();
  24. String guess = words[random.Next(0, words.Length)];
  25. StringBuilder word = new StringBuilder(" ");
  26. bool gameOn = true;
  27. for (int i = 0; i < guess.Length; i++) {
  28. if (word[i] == ' ') {
  29. word[i] = '_';
  30. }
  31. }
  32. Console.WriteLine(word);
  33. while (gameOn) {
  34. string letter = Console.ReadLine();
  35. for (int i = 0; i < guess.Length; i++) {
  36. if (guess[i].ToString() == letter) {
  37. word[i] = Convert.ToChar(letter);
  38. }
  39. }
  40. if (word.ToString().Trim() == guess) {
  41. gameOn = false;
  42. Console.WriteLine("You won the game!");
  43. }
  44. Console.WriteLine(word);
  45. }
  46. }
  47. }
  48. }

comments powered by Disqus