HangMan needs optimization

 using System;

using System.IO;

using System.Linq;

using System.Collections.Generic;

namespace HangMan

{

  public class Program

  {

 public static void Main()

 {

   //Select a random word and store it in string

   string[] words = { "crow", "dog", "hen", "cow", "lion", "zebra", "snake", "monkey", "tiger" };

   Random randomGenerator = new Random();

   int randomNumber = randomGenerator.Next(0, words.Length);

   string hiddenword = words[randomNumber];

   // Console.WriteLine(hiddenword);

   Console.WriteLine("HangMan Animals\n-------");//

   string userst = "";

   for (int i = 0; i < hiddenword.Length; i++)

   {

  userst += "*";

   }

   Console.WriteLine(userst);

   int index = 0;

   string input = "";

   string test = "";

   test = hiddenword;

   string test2 = "";

   while (userst != test)

   {

  input = Console.ReadLine();

  {

    if (input.Length > 1)

    {

   break;

    }

    if (hiddenword.Contains(input))

    {

   index = hiddenword.IndexOf(input);

   userst = userst.Remove(index, 1);

   userst = userst.Insert(index, input);

   Console.WriteLine(userst);

    }

    else

   Console.WriteLine("Try again");

    if (userst == hiddenword)

   Console.WriteLine("Congrats you won");

  }

   }

 }

  }

}


Comments

Popular Posts