Answer to Question #301020 in C# for Aaa

Question #301020

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

1
Expert's answer
2022-02-22T06:45:00-0500
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();

        }
    }

   
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS