I am newbie in C# and I doing dice roll game with. I have problem to place random int value to property. Tried different variations but none of them working, just got more or less red wavy lines under text.
How can I place the results of that random operation into property Amount? Google does not helped me after 1,5 hour finding. Where is the problem?
using System;
using System.Collections.Generic;
using System.Text;
using static System.Random;
namespace DiceRollGame
{
class Dice
{
private static Random randomnumber = new Random();
public int Amount
{
get
{
return Amount;
}
set
{
Amount = value;
}
}
public int ThrowAmount { get; set; }
public Dice(int throwamount)
{
ThrowAmount = 0;
}
public static void Throw()
{
int numbervalue = randomnumber.Next(1, 6);
Amount = int numbervalue;
}
}
}
Try this out:
class Dice
{
private readonly Random rnd;
public int Amount { get; set; }
public int ThrowAmount { get; set; }
public Dice() => rnd = new Random();
public void Throw() => Amount = rnd.Next(1, 7); //Random() max value is exclusive
}
Usage:
Dice d = new Dice();
d.Throw();
Console.WriteLine(d.Amount);
Sorry my language skills, but that ThrowAmount is how many times player has throw the dice, I consider that You think it is the number of dice. Which get three wins first is winner of this game. I made a constructor to reset amount of throws. That's why there is 0. That Amount is the number of the dice.
@masteroscar then you don't need to pass it as parameter to the constructor, it will have the default value 0 when you instantiate it.