TodorMitev

HangmanGame

Mar 28th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.61 KB | None | 0 0
  1. /************************
  2.  * "Hangman" game
  3.  *
  4.  * devoloped by Todor Mitev
  5.  *
  6.  * for Telerik Academy
  7. ************************/
  8.  
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13.  
  14. namespace Hangman
  15. {
  16.     class HangmanPlayer
  17.     {
  18.         static void Main(string[] args)
  19.         {
  20.             Console.WriteLine("------------------------------------------");
  21.             Console.WriteLine("Hi! This game \"Hangman\" (in Bulgarian: \"Besenica\") is \ndeveloped by Todor Mitev.");
  22.             Console.WriteLine("Enjoy your game and good luck!:)");
  23.             Console.WriteLine("------------------------------------------");
  24.  
  25.             Hangman player = new Hangman();
  26.             player.Start();
  27.         }
  28.     }
  29.  
  30.     public class Player : IComparable<Player>
  31.     {
  32.         public string Name { get; set; }
  33.         public int Score { get; set; }
  34.  
  35.         public Player(int score)
  36.             :this(null,score)
  37.         { }
  38.  
  39.         public Player(string name, int score)
  40.         {
  41.             this.Name = name;
  42.             this.Score = score;
  43.         }
  44.  
  45.         public int CompareTo(Player other)
  46.         {
  47.             int resultOfCompare = this.Score.CompareTo(other.Score);
  48.             if (resultOfCompare == 0)
  49.             {
  50.                 resultOfCompare = this.Name.CompareTo(other.Name);
  51.             }
  52.             return resultOfCompare;
  53.         }
  54.  
  55.         public override string ToString()
  56.         {
  57.             string result = String.Format("{0} --> {1}", this.Name, this.Score);
  58.             return result;
  59.         }
  60.     }
  61.  
  62.     public class Hangman
  63.     {
  64.         private string[] WORDS = new string[] {"computer", "programmer", "software", "debugger", "compiler",
  65.                                         "developer", "algorithm", "array", "method" ,"variable"};
  66.         private const string HELP = "help";
  67.         private const string TOP = "top";
  68.         private const string RESTART = "restart";
  69.         private const string EXIT = "exit";
  70.  
  71.         private const string START_MSG = "Welcome to \"Hangman\" game. Please try to guess my secret word.\n" +
  72.             "Use 'top' to view the top scoreboard, 'restart' to start a new game,\n'help' to cheat " +
  73.             "and 'exit' to quit the game.";
  74.         private const string WORD_IS_MSG = "The secret word is: ";
  75.         private const string ENTER_ANSWER_MSG = "Enter your guess: ";
  76.         private const string CORRECT_ANSWER_MSG = "Good job! You revealed ";
  77.         private const string INCORRECT_ANSWER_MSG = "Sorry! There are no unrevealed letters ";
  78.         private const string ENTER_SCOREBOARD_NAME_MSG = "Please enter your name for the top scoreboard: ";
  79.         private const string SCOREBOARD_MSG = "Scoreboard:";
  80.         private const string INCORRECT_COMMAND_MSG = "Incorrect guess or command!";
  81.  
  82.         private char[] wordToReveal;
  83.         private char[] wordToDisplay;
  84.         private int mistakes;
  85.         private int helpCalled;
  86.         private List<Player> scoreboard = new List<Player>();
  87.  
  88.         public Hangman()
  89.         {
  90.             this.scoreboard = new List<Player>();
  91.         }
  92.  
  93.         public void Start()
  94.         {
  95.             NewGame();
  96.         }
  97.  
  98.         private void NewGame()
  99.         {
  100.             Random indexToChoose = new Random();
  101.             int index = indexToChoose.Next(1, WORDS.Length);
  102.             this.wordToReveal = WORDS[index].ToCharArray();
  103.             this.wordToDisplay = new char[(wordToReveal.Length * 2) - 1];
  104.             this.mistakes = 0;
  105.             this.helpCalled = 0;
  106.            
  107.             Console.WriteLine(START_MSG);
  108.             FillWordToDisplay(wordToDisplay);
  109.  
  110.             Play();
  111.         }
  112.  
  113.         private void Play()
  114.         {
  115.             DisplayWord(wordToDisplay);
  116.  
  117.             bool isFinished = CheckForFinish();
  118.             if (!isFinished)
  119.             {
  120.                 Console.Write(ENTER_ANSWER_MSG);
  121.                 ReadInput();
  122.             }
  123.             else
  124.             {
  125.                 SignToScoreboard();
  126.             }
  127.         }
  128.  
  129.         private void SignToScoreboard()
  130.         {
  131.             if (this.helpCalled != 0)
  132.             {
  133.                 Console.WriteLine("You won with {0} mistake(s) but you have cheated. You are not allowed to enter to the scoreboard", this.mistakes);
  134.                 this.mistakes = 0;
  135.                 this.helpCalled = 0;
  136.             }
  137.             else
  138.             {
  139.                 Console.WriteLine("You won with {0} mistake(s).",this.mistakes);
  140.                 Console.Write(ENTER_SCOREBOARD_NAME_MSG);
  141.                 string name = Console.ReadLine();
  142.                 Player player = new Player(name, this.mistakes);
  143.                 this.scoreboard.Add(player);
  144.                 scoreboard.Sort();
  145.                 PrintScoreboard();
  146.             }
  147.             NewGame();
  148.         }
  149.  
  150.         private void PrintScoreboard()
  151.         {
  152.             Console.WriteLine("Scoreboard:");
  153.             if (scoreboard.Count != 0)
  154.             {
  155.                 for (int i = 0; i < scoreboard.Count; i++)
  156.                 {
  157.                     Console.WriteLine("{0}. {1}", i+1, scoreboard[i].ToString());
  158.                 }
  159.             }
  160.             else
  161.             {
  162.                 Console.WriteLine("THE SCOREBOARD IS EMPTY!");
  163.             }
  164.             Console.WriteLine();
  165.         }
  166.  
  167.         private bool CheckForFinish()
  168.         {
  169.             for (int i = 0; i < wordToReveal.Length; i++)
  170.             {
  171.                 if (wordToReveal[i] != wordToDisplay[i * 2])
  172.                 {
  173.                     return false;
  174.                 }
  175.             }
  176.             return true;
  177.         }
  178.  
  179.         private void ReadInput()
  180.         {
  181.             string input = Console.ReadLine();
  182.  
  183.             if (input.Equals(String.Empty))
  184.             {
  185.                 Console.WriteLine("You've entered a empty line...Please try again!");
  186.                 ReadInput();
  187.             }
  188.             if (input.Length > 1)
  189.             {
  190.                 ProcessCommand(input);
  191.             }
  192.             else
  193.             {
  194.                 ProcessLetter(input[0]);
  195.             }
  196.  
  197.             Play();
  198.         }
  199.  
  200.         private void ProcessLetter(char letter)
  201.         {
  202.             if (wordToReveal.Contains(letter))
  203.             {
  204.                 FillLetter(letter,false);
  205.             }
  206.             else
  207.             {
  208.                 mistakes++;
  209.                 Console.WriteLine("{0} {1}",INCORRECT_ANSWER_MSG,letter);
  210.             }
  211.         }
  212.  
  213.         private void FillLetter(char letter,bool isHelp)
  214.         {
  215.             List<int> indexesLetterAppears = new List<int>();
  216.  
  217.             for (int i = 0; i < wordToReveal.Length; i++)
  218.             {
  219.                 if (wordToReveal[i] == letter)
  220.                 {
  221.                     indexesLetterAppears.Add(i);
  222.                 }
  223.             }
  224.  
  225.             int guessedLetterCount = indexesLetterAppears.Count;
  226.             for (int i = 0; i < guessedLetterCount; i++)
  227.             {
  228.                 int indexToFill = indexesLetterAppears[i];
  229.                 wordToDisplay[indexToFill * 2] = letter;
  230.             }
  231.             if (!isHelp)
  232.             {
  233.                 if (guessedLetterCount > 1)
  234.                 {
  235.                     Console.WriteLine("{0} {1} letters", CORRECT_ANSWER_MSG, guessedLetterCount);
  236.                 }
  237.                 else
  238.                 {
  239.                     Console.WriteLine("{0} {1} letter", CORRECT_ANSWER_MSG, guessedLetterCount);
  240.                 }
  241.             }
  242.             else
  243.             {
  244.                 Console.WriteLine("OK, I reveal for you the next letter {0}.",letter);
  245.             }
  246.         }
  247.  
  248.         private void ProcessCommand(string cmd)
  249.         {
  250.             switch (cmd)
  251.             {
  252.                 case RESTART:
  253.                     {
  254.                         NewGame();
  255.                     }
  256.                     break;
  257.                 case TOP:
  258.                     {
  259.                         PrintScoreboard();
  260.                     }
  261.                     break;
  262.                 case HELP:
  263.                     {
  264.                         Help();
  265.                         this.helpCalled++;
  266.                     }
  267.                     break;
  268.                 case EXIT:
  269.                     {
  270.                         Console.WriteLine("Good Bye!");
  271.                         Environment.Exit(0);
  272.                     }
  273.                     break;
  274.                 default:
  275.                     Console.WriteLine(INCORRECT_COMMAND_MSG);
  276.                     break;
  277.             }
  278.         }
  279.  
  280.         private void Help()
  281.         {
  282.             int indexOfLetterToFill = 0;
  283.             for (int i = 0; i < wordToDisplay.Length; i++)
  284.             {
  285.                 if (wordToDisplay[i] == '_')
  286.                 {
  287.                     indexOfLetterToFill = i;
  288.                     break;
  289.                 }
  290.             }
  291.             indexOfLetterToFill /= 2;
  292.             FillLetter(wordToReveal[indexOfLetterToFill],true);
  293.         }
  294.  
  295.         private void DisplayWord(char[] wordToDisplay)
  296.         {
  297.             Console.Write(WORD_IS_MSG);
  298.             foreach (char ch in wordToDisplay)
  299.             {
  300.                 Console.Write(ch);
  301.             }
  302.             Console.WriteLine();
  303.         }
  304.  
  305.         private void FillWordToDisplay(char[] wordToDisplay)
  306.         {
  307.             for (int i = 0; i < wordToDisplay.Length; i++)
  308.             {
  309.                 if (i % 2 == 0)
  310.                 {
  311.                     wordToDisplay[i] = '_';
  312.                 }
  313.                 else
  314.                 {
  315.                     wordToDisplay[i] = ' ';
  316.                 }
  317.             }
  318.         }
  319.     }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment