erehh

Hangman c#

Sep 13th, 2022
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Hangman
  4. {
  5.     internal class Program
  6.     {
  7.         static string word;
  8.  
  9.         static char[] guess;
  10.  
  11.         static int lives;
  12.  
  13.         static char input;
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             update();
  18.  
  19.         }
  20.  
  21.         static void start()
  22.         {
  23.             Console.Clear();
  24.             word = pickWord();
  25.             fillBlank();
  26.  
  27.             lives = 5;
  28.         }
  29.         static void update()
  30.         {
  31.             start();
  32.  
  33.             while (lives > 0)
  34.             {
  35.  
  36.  
  37.                 printWord();
  38.  
  39.                 promptPlayer();
  40.  
  41.                 input = getInput();
  42.  
  43.                 checkInput(input);
  44.  
  45.                 checkGameOver();
  46.             }
  47.         }
  48.  
  49.         static string pickWord()
  50.         {
  51.             string[] words = new string[]
  52.             {
  53.                 "hydrophobia", "bacteria", "car","dog","electricity","humanity","compound"
  54.             };
  55.  
  56.             Random rnd = new Random();
  57.             int index = rnd.Next(0, words.Length);
  58.  
  59.             return words[index];
  60.         }
  61.  
  62.         static void fillBlank()
  63.         {
  64.             guess = new char[word.Length];
  65.             for (int i = 0; i < word.Length; i++)
  66.             {
  67.                 guess[i] = '_';
  68.             }
  69.         }
  70.  
  71.         static void printWord()
  72.         {
  73.             Console.WriteLine("Guess the word: ");
  74.  
  75.             for (int i = 0; i < guess.Length; i++)
  76.             {
  77.                 Console.Write(guess[i] + " ");
  78.             }
  79.             Console.WriteLine();
  80.         }
  81.  
  82.         static void promptPlayer()
  83.         {
  84.             Console.Write("Enter a letter: ");
  85.  
  86.             if (lives < 5) { Console.WriteLine($"\nYou have {lives} lives left"); }
  87.         }
  88.  
  89.         static char getInput()
  90.         {
  91.             char input = char.Parse(Console.ReadLine());
  92.  
  93.             return input;
  94.         }
  95.  
  96.         static void checkInput(char input)
  97.         {
  98.             bool correct = false;
  99.  
  100.             for (int i = 0; i < word.Length; i++)
  101.             {
  102.                 if (word[i] == input)
  103.                 {
  104.                     guess[i] = input;
  105.                     correct = true;
  106.                     Console.Clear();
  107.                 }
  108.             }
  109.  
  110.             if (!correct)
  111.             {
  112.                 lives--;
  113.                 Console.Clear();
  114.             }
  115.         }
  116.  
  117.         static void checkGameOver()
  118.         {
  119.             if (lives == 0)
  120.             {
  121.                 Console.WriteLine("Game over!\n");
  122.             }
  123.  
  124.             else if (!guess.Contains('_'))
  125.             {
  126.                 Console.WriteLine("You win!\n");
  127.             }
  128.         }
  129.     }
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment