Search code examples
c#randomintdice

C# Dice game, turn this into a user typed input


Ive made a dice "game" which randomly chooses a number and then "rolls" to see how many rolls it takes to get the same number. The problem im having is how can I make the pyöräytys to be typed in by the user.

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

namespace Noppapeli
{
    class Program
    {
        static void Main(string[] args)
        {
            int pyöräytys;
            int satunnainen;
            int luku = 0;

            Random noppa = new Random((int)DateTime.Now.Ticks);

            int.TryParse(Console.ReadLine(),out pyöräytys);
            Console.WriteLine("Arvon numeron ja sitten koitan saada sen uudelleen");
            Console.WriteLine("Haettava numero on: " + pyöräytys);
            Console.ReadLine();
            do
            {
                luku++;
                satunnainen = noppa.Next(1, 7);
                Console.WriteLine("numero on: " + satunnainen);
                if (satunnainen == pyöräytys)
                {
                    satunnainen = pyöräytys;
                }
            } while (pyöräytys != satunnainen);

            Console.WriteLine("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
            Console.WriteLine("Haettu numero: " + pyöräytys);
            Console.WriteLine("Pyöräytetty numero: " + satunnainen);
            Console.Write("Kesti " + luku + " Nopan pyöräytystä saada tulos!");
            Console.ReadLine();
        }
    }
}

I tried changing the pyöräytys = noppa.Next(1,7); to something that would read the key that is pressed but couldnt find anything.

EDIT: Thanks for the solution WhileTrueSleep, i updated it in the code above. Just another thing, now that I can input a number over 6 it goes on a infinite loop. how would I make it so that the number must be between 1-6.


Solution

  • int.TryParse(Console.ReadLine(), out pyöräytys);
    

    Edit: Replace this line against pyöräytys = noppa.Next(1, 7); The user now can enter the value in the console. If the input is not valid then pyöräytys will stay by its default value (0).