Create an application that will allow the user to play cow and bull(only 4 character word)
Same character same position-cow
Same character different position-bull
Love
Cow-0,bull-0
Hate
Cow-0,bull-3
When
Cow-2,bull-0
What
Cow-4,bull-0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<string> words = new List<string>();
words.Add("what");
words.Add("love");
words.Add("hate");
Random r = new Random();
string selectedWord = words[r.Next(0, words.Count - 1)];
int cows = 0;
int bulls = 0;
Console.WriteLine("Enter your guess: ");
do
{
cows = 0;
bulls = 0;
string guess = Console.ReadLine().ToLower(); ;
for (int i = 0; i < 4; i++)
{
if (guess[i] == selectedWord[i])
{
cows++;
}
else
{
for (int j = 0; j < 4; j++)
{
if (i != j && guess[i] == selectedWord[j])
{
bulls++;
}
}
}
}
Console.WriteLine("Cow - {0}, bull - {1}", cows, bulls);
}
while (cows != 4);
Console.ReadKey();
}
}
}
Comments
Leave a comment