Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Microsoft Visual C# 2012: An Introduction to Object-Oriented Programming Chapter

ID: 3766809 • Letter: M

Question

Microsoft Visual C# 2012: An Introduction to Object-Oriented Programming

Chapter 14 Question 6

Create a program (IN C# LANGUAGE) named HighScore containing a Form that hosts a game in which the computer randomly selects one of three letters (A, B, or C) and the user tries to guess which letter was selected. At the start of the game, read in the previous high score from a data file. (Create this file to hold "0" the first time the game is played.) Display the previous high score on the Form to show the player the score to try to beat. Play continues for 10 rounds. As the player makes each guess, show the player's guess and the computer's choice, and award a point if the player correctly guesses the computer's choice. Keep a running count of the number of correct guesses. After 10 rounds, disable the game controls and create a file that holds the guesses. After 10 rounds, disable the game controls and create a file that holds the new high score, which might be the same as before the game or a new higher number. When the player begins a new game, the high score will be displayed on the Form as the new score to beat.

In C# Please.

Explanation / Answer

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public class Result
    {
        public int Index { get; set; }
        public bool Flag { get; set; }
    }

    static void Main()
    {
        Program game = new Program();
        game.Run();
    }

    void Run()
    {
        // initialize the number of attempts
        int numberOfAttempts = 10;

        Console.WriteLine(" Welcome to Random Number Guessing Game.");
        Console.WriteLine(" Guess the random number between 1 to 3.");
        Console.WriteLine(string.Format(" You have {0} attempts to win the game.", numberOfAttempts));

        // Call the method to Generate the Random Number
        string randomNumber = GenerateRandomNumber();

        for (int i = 1; i <= numberOfAttempts; i++)
        {
            // Call the method to get the user input
            string userInput = GetUserInput(i);

            // Get the result - Collection containing the result of all 4 digits
            List<Result> result = GetResult(randomNumber, userInput);

            // Guess the count of number of digits that are correct
            int flagCount = result.Where(f => f.Flag == true).Count();

            // Get the place(s)/index of digits that are correct
            string digitsCorrect = string.Join(",", result.Where(f => f.Flag == true)
                .Select(c => (++c.Index).ToString()));

            // check the flag count and display appropriate message
            if (flagCount == 1)
            {
                Console.WriteLine("Random Number:{0} , Your Input:{1}", randomNumber, userInput);
                Console.WriteLine("You guess is correct! Game Won..hurray...:)");
                break;
            }
            else if (i == numberOfAttempts)
            {
                Console.WriteLine("sorry, You missed it! Game Lost..:(");
                Console.WriteLine("Random Number is {0}", randomNumber);
            }
            else
            {
                digitsCorrect = flagCount == 0 ? "none" : digitsCorrect;
                Console.WriteLine(string.Format("Digit(s) in place {0} correct", digitsCorrect));
            }
        }

        Console.ReadLine();
    }

    public List<Result> GetResult(string randomNumber, string userInput)
    {
        char[] splitRandomNumber = randomNumber.ToCharArray();
        char[] splitUserInput = userInput.ToCharArray();

        List<Result> results = new List<Result>();

        for (int index = 0; index < randomNumber.Length; index++)
        {
            Result result = new Result();
            result.Index = index;
            result.Flag = splitRandomNumber[index] == splitUserInput[index];
            results.Add(result);
        }

        return results;
    }

    public string GetUserInput(int attempt)
    {
        int inputNumber;

        Console.WriteLine(string.Format(" Guess the number. Attempt:{0}", attempt));
        Console.WriteLine("Input a number");

        if (int.TryParse(Console.ReadLine(), out inputNumber)
            && inputNumber.ToString().Length == 1)
        {
            return inputNumber.ToString();
        }
        else
        {
            Console.WriteLine("You have entered a invalid input.");
            return "0000";
        }
    }

    public string GenerateRandomNumber()
    {
        Random random = new Random();
        string number = string.Empty;
        int length = 1;

        for (int i = 0; i < length; i++)
        {
            number += random.Next(1, 3);
        }

        return number;
    }
}