Tom4045
Tom4045

Reputation: 29

How to determine input when the can have a variable?

I'm working on this commandline based dice. And I want it to be able to output statistics and reset those internal statistics. Currently I have the following classes: Dice and DiceInterface.

I want the user to be able to use the following format for input: "'Throw' to throw the dice, alternate use 'Throw(x)', 'Stat(side)' to see how many times (side) has been thrown, or 'Reset' to reset thrown statistics"

I need a way to determine if the user has typed Throw, Throw(x), Stat(side) or Reset. Throw and Reset are obvious to me but I find it quite difficult to imagine a way to do Throw(x) and Stat(side). Does anyone have any tips or a solution?

Thanks in advance!

using System;
using System.Collections.Generic;
using System.Text;

namespace Oefening_2
{
    class DiceInterface
    {
        static void Main()
        {
            StartProgram();
        }
        static void StartProgram()
        {
            Console.WriteLine("Hello, how many sides would you like the dice to have?");
            var input = Console.ReadLine();
            if (Int32.TryParse(input, out int number))
            {
                int desiredSides = number;
                Dice NewDice = new Dice(desiredSides);
                Console.WriteLine("What would you like to do? ");
                Console.WriteLine("Type: 'Throw' to throw the dice, alternate use 'Throw(x)', 'Stat(side)' to see how many times (side) has been thrown, or 'Reset' to reset thrown statistics");
                MainIO(NewDice);
            }
            else
            {
                Console.WriteLine("That is not an integer! The program will restart now...");
                StartProgram();
            }
        }

        static void MainIO(Dice CurrentDice)
        {
            Console.Write("Input: ");
            string input = Console.ReadLine();

            //Throw
            if (input == "Throw")
            {
                Console.WriteLine(CurrentDice.Throw());
                MainIO(CurrentDice);
            }

            //Throw(x)
            else if(input == "")

            //Thrown(side)

            //Reset
            else if (input == "Reset")
            {
                CurrentDice.ResetStatistics();
                MainIO(CurrentDice);
            }
        }
    }
}
using System;

namespace Oefening_2
{
    class Dice
    {
        public int Sides { get; set; }
        private readonly Random _rnd = new Random();
        public int[] Thrown;

        public Dice(int sides)
        {
            Sides = sides;
            Thrown = new int[sides--];
        }

        public Dice():this(6)
        {

        }

        public int Throw()
        {
            int thrownNumber = _rnd.Next(1, Sides);
            Thrown[thrownNumber]++;
            return thrownNumber;
        }

        public int NrOfTimesThrown(int side)
        {
            int result;

            result = Thrown[side];

            return result;
            
        }

        public void ResetStatistics()
        {
            for(int i = 0 ; i < Sides; i++)
            {
                Thrown[i] = 0;
            }
  
        }
    }
}

Upvotes: 0

Views: 43

Answers (2)

PippoZucca
PippoZucca

Reputation: 161

If I correctly understood your problem, it could be solved with an approach like this:

        Console.WriteLine("Enter a value");
        var s = Console.ReadLine();

        if(s.Contains('('))
        {
            int pFrom = s.IndexOf("(");
            int pTo = s.LastIndexOf(")");
            var myChoice = s.Substring(0, pFrom);
            var myValue = s.Substring(pFrom + 1, pTo - pFrom);

            Console.WriteLine($"You choosed : {myChoice}");
            Console.WriteLine($"With a value of: {myValue}");
        }

This is just a brief example of how to "understand" if there is a value and then recover it.

I hope this could help you somehow.

Upvotes: 1

JonasH
JonasH

Reputation: 36596

This can be fairly simply done with StartsWith to check if a string starts with some prefix, and use .Remove to get anything after this prefix:

if(input.StartsWith("Stat"){
    var parameterString = input.Remove(0, "Stat".Length);
    if(int.TryParse(parameterString , out var side){
        ...
    }
}

Upvotes: 1

Related Questions