Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check array contains the same numbers

Tags:

c#

predicate

dice

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.

like image 442
andrelange91 Avatar asked Dec 10 '25 17:12

andrelange91


1 Answers

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

like image 71
ΦXocę 웃 Пepeúpa ツ Avatar answered Dec 13 '25 06:12

ΦXocę 웃 Пepeúpa ツ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!