Untitled

 avatar
unknown
plain_text
2 years ago
3.4 kB
3
Indexable
using System;

public interface IIO
{
    void Write(string message);
    void WriteLine(string message);
    string ReadLine();
}

public class ConsoleIO : IIO
{
    public void Write(string message)
    {
        Console.Write(message);
    }

    public void WriteLine(string message)
    {
        Console.WriteLine(message);
    }

    public string ReadLine()
    {
        return Console.ReadLine();
    }
}

public class NumberGuessingGame
{
    private string playerName;
    private IIO io;

    public NumberGuessingGame(IIO io)
    {
        this.io = io;
    }

    public void Start()
    {
        io.WriteLine("Welcome to the Number Guessing Game!");
        io.WriteLine("=====================================");
        io.WriteLine();

        io.Write("Enter your username: ");
        playerName = io.ReadLine();
        io.WriteLine();

        bool playOn = true;
        while (playOn)
        {
            io.WriteLine("New game:");
            io.WriteLine();

            string goal = GenerateGoal();
            io.WriteLine("For practice, the number is: " + goal);
            io.WriteLine();

            int nGuess = 1;
            string bbcc = CheckGuess(goal);
            io.WriteLine(bbcc);
            io.WriteLine();

            while (bbcc != "BBBB,")
            {
                string guess = io.ReadLine();
                io.WriteLine(guess);
                io.WriteLine();

                bbcc = CheckGuess(goal, guess);
                io.WriteLine(bbcc);
                io.WriteLine();

                nGuess++;
            }

            SavePlayerData(nGuess);
            ShowTopList();
            io.WriteLine();

            io.WriteLine("Correct, it took " + nGuess + " guesses.");
            io.Write("Continue? (n for no, y for yes): ");
            string answer = io.ReadLine();
            io.WriteLine();

            if (answer != null && answer != "" && answer.Substring(0, 1) == "n")
            {
                playOn = false;
            }
        }

        io.WriteLine("Thank you for playing the Number Guessing Game!");
    }

    private string GenerateGoal()
    {
        // Generate a random number as the goal
        Random random = new Random();
        int goal = random.Next(1, 101);
        return goal.ToString();
    }

    private string CheckGuess(string goal, string guess = "")
    {
        // Perform the logic to check the guess against the goal
        // and generate the BBCC response string
        // (implementation omitted for brevity)

        return "BBCC"; // Placeholder response
    }

    private void SavePlayerData(int nGuess)
    {
        // Save the player's data (implementation omitted for brevity)
    }

    private void ShowTopList()
    {
        // Show the top list of players (implementation omitted for brevity)
    }
}

//I Main-metoden
public class Program
{
    public static void Main(string[] args)
    {
        IIO io = new ConsoleIO();
        NumberGuessingGame game = new NumberGuessingGame(io);
        game.Start();
    }
}

/*
Nu kommer all in- och utmatning att hanteras via IIO-gränssnittet och du kan enkelt byta ut ConsoleIO mot en annan implementering av IIO om du behöver anpassa in- och utmatningen till en annan plattform eller användargränssnitt.
*/