I am trying to make a simple dice game. it's entirely in the console. a user can set an infinite number of dices. and then the game has to tell how many rolls it took for all the dices to be 6 at the same time.
I have tried something like this,
int i = 0;
int[] throws = new int[4000];
bool success = false;
do
{
throws[1] = dice.Next(1, 7);
throws[2] = dice.Next(1, 7);
throws[3] = dice.Next(1, 7);
throws[4] = dice.Next(1, 7);
throws[5] = dice.Next(1, 7);
throws[6] = dice.Next(1, 7);
if (Array.TrueForAll(throws, 6))
{
success = true;
}
i++;
} while (success != true);
but trueforall says fails with something called predicate, which i have been unable to understand fully.
is there another way ?
a bit stuck here.. hope someone can help with this.
A predicate is a method that takes one object/variable as argument, checks a condition on that and returns either true
or false
.. now to the issue:
instead of doing:
if (Array.TrueForAll(throws, 6))
do:
if (Array.TrueForAll(throws, x => x == 6))
but what is this?
x => x == 6
is exactly that predicate we are talking about
is a lambda that can be read as:
take every element in the array, in a variable X. now evaluate if X == 6