Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************
- * "Hangman" game
- *
- * devoloped by Todor Mitev
- *
- * for Telerik Academy
- ************************/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Hangman
- {
- class HangmanPlayer
- {
- static void Main(string[] args)
- {
- Console.WriteLine("------------------------------------------");
- Console.WriteLine("Hi! This game \"Hangman\" (in Bulgarian: \"Besenica\") is \ndeveloped by Todor Mitev.");
- Console.WriteLine("Enjoy your game and good luck!:)");
- Console.WriteLine("------------------------------------------");
- Hangman player = new Hangman();
- player.Start();
- }
- }
- public class Player : IComparable<Player>
- {
- public string Name { get; set; }
- public int Score { get; set; }
- public Player(int score)
- :this(null,score)
- { }
- public Player(string name, int score)
- {
- this.Name = name;
- this.Score = score;
- }
- public int CompareTo(Player other)
- {
- int resultOfCompare = this.Score.CompareTo(other.Score);
- if (resultOfCompare == 0)
- {
- resultOfCompare = this.Name.CompareTo(other.Name);
- }
- return resultOfCompare;
- }
- public override string ToString()
- {
- string result = String.Format("{0} --> {1}", this.Name, this.Score);
- return result;
- }
- }
- public class Hangman
- {
- private string[] WORDS = new string[] {"computer", "programmer", "software", "debugger", "compiler",
- "developer", "algorithm", "array", "method" ,"variable"};
- private const string HELP = "help";
- private const string TOP = "top";
- private const string RESTART = "restart";
- private const string EXIT = "exit";
- private const string START_MSG = "Welcome to \"Hangman\" game. Please try to guess my secret word.\n" +
- "Use 'top' to view the top scoreboard, 'restart' to start a new game,\n'help' to cheat " +
- "and 'exit' to quit the game.";
- private const string WORD_IS_MSG = "The secret word is: ";
- private const string ENTER_ANSWER_MSG = "Enter your guess: ";
- private const string CORRECT_ANSWER_MSG = "Good job! You revealed ";
- private const string INCORRECT_ANSWER_MSG = "Sorry! There are no unrevealed letters ";
- private const string ENTER_SCOREBOARD_NAME_MSG = "Please enter your name for the top scoreboard: ";
- private const string SCOREBOARD_MSG = "Scoreboard:";
- private const string INCORRECT_COMMAND_MSG = "Incorrect guess or command!";
- private char[] wordToReveal;
- private char[] wordToDisplay;
- private int mistakes;
- private int helpCalled;
- private List<Player> scoreboard = new List<Player>();
- public Hangman()
- {
- this.scoreboard = new List<Player>();
- }
- public void Start()
- {
- NewGame();
- }
- private void NewGame()
- {
- Random indexToChoose = new Random();
- int index = indexToChoose.Next(1, WORDS.Length);
- this.wordToReveal = WORDS[index].ToCharArray();
- this.wordToDisplay = new char[(wordToReveal.Length * 2) - 1];
- this.mistakes = 0;
- this.helpCalled = 0;
- Console.WriteLine(START_MSG);
- FillWordToDisplay(wordToDisplay);
- Play();
- }
- private void Play()
- {
- DisplayWord(wordToDisplay);
- bool isFinished = CheckForFinish();
- if (!isFinished)
- {
- Console.Write(ENTER_ANSWER_MSG);
- ReadInput();
- }
- else
- {
- SignToScoreboard();
- }
- }
- private void SignToScoreboard()
- {
- if (this.helpCalled != 0)
- {
- Console.WriteLine("You won with {0} mistake(s) but you have cheated. You are not allowed to enter to the scoreboard", this.mistakes);
- this.mistakes = 0;
- this.helpCalled = 0;
- }
- else
- {
- Console.WriteLine("You won with {0} mistake(s).",this.mistakes);
- Console.Write(ENTER_SCOREBOARD_NAME_MSG);
- string name = Console.ReadLine();
- Player player = new Player(name, this.mistakes);
- this.scoreboard.Add(player);
- scoreboard.Sort();
- PrintScoreboard();
- }
- NewGame();
- }
- private void PrintScoreboard()
- {
- Console.WriteLine("Scoreboard:");
- if (scoreboard.Count != 0)
- {
- for (int i = 0; i < scoreboard.Count; i++)
- {
- Console.WriteLine("{0}. {1}", i+1, scoreboard[i].ToString());
- }
- }
- else
- {
- Console.WriteLine("THE SCOREBOARD IS EMPTY!");
- }
- Console.WriteLine();
- }
- private bool CheckForFinish()
- {
- for (int i = 0; i < wordToReveal.Length; i++)
- {
- if (wordToReveal[i] != wordToDisplay[i * 2])
- {
- return false;
- }
- }
- return true;
- }
- private void ReadInput()
- {
- string input = Console.ReadLine();
- if (input.Equals(String.Empty))
- {
- Console.WriteLine("You've entered a empty line...Please try again!");
- ReadInput();
- }
- if (input.Length > 1)
- {
- ProcessCommand(input);
- }
- else
- {
- ProcessLetter(input[0]);
- }
- Play();
- }
- private void ProcessLetter(char letter)
- {
- if (wordToReveal.Contains(letter))
- {
- FillLetter(letter,false);
- }
- else
- {
- mistakes++;
- Console.WriteLine("{0} {1}",INCORRECT_ANSWER_MSG,letter);
- }
- }
- private void FillLetter(char letter,bool isHelp)
- {
- List<int> indexesLetterAppears = new List<int>();
- for (int i = 0; i < wordToReveal.Length; i++)
- {
- if (wordToReveal[i] == letter)
- {
- indexesLetterAppears.Add(i);
- }
- }
- int guessedLetterCount = indexesLetterAppears.Count;
- for (int i = 0; i < guessedLetterCount; i++)
- {
- int indexToFill = indexesLetterAppears[i];
- wordToDisplay[indexToFill * 2] = letter;
- }
- if (!isHelp)
- {
- if (guessedLetterCount > 1)
- {
- Console.WriteLine("{0} {1} letters", CORRECT_ANSWER_MSG, guessedLetterCount);
- }
- else
- {
- Console.WriteLine("{0} {1} letter", CORRECT_ANSWER_MSG, guessedLetterCount);
- }
- }
- else
- {
- Console.WriteLine("OK, I reveal for you the next letter {0}.",letter);
- }
- }
- private void ProcessCommand(string cmd)
- {
- switch (cmd)
- {
- case RESTART:
- {
- NewGame();
- }
- break;
- case TOP:
- {
- PrintScoreboard();
- }
- break;
- case HELP:
- {
- Help();
- this.helpCalled++;
- }
- break;
- case EXIT:
- {
- Console.WriteLine("Good Bye!");
- Environment.Exit(0);
- }
- break;
- default:
- Console.WriteLine(INCORRECT_COMMAND_MSG);
- break;
- }
- }
- private void Help()
- {
- int indexOfLetterToFill = 0;
- for (int i = 0; i < wordToDisplay.Length; i++)
- {
- if (wordToDisplay[i] == '_')
- {
- indexOfLetterToFill = i;
- break;
- }
- }
- indexOfLetterToFill /= 2;
- FillLetter(wordToReveal[indexOfLetterToFill],true);
- }
- private void DisplayWord(char[] wordToDisplay)
- {
- Console.Write(WORD_IS_MSG);
- foreach (char ch in wordToDisplay)
- {
- Console.Write(ch);
- }
- Console.WriteLine();
- }
- private void FillWordToDisplay(char[] wordToDisplay)
- {
- for (int i = 0; i < wordToDisplay.Length; i++)
- {
- if (i % 2 == 0)
- {
- wordToDisplay[i] = '_';
- }
- else
- {
- wordToDisplay[i] = ' ';
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment