Advertisement

(C#) - Check all values in a array are TRUE?

Started by December 12, 2018 10:28 PM
7 comments, last by A4L 6 years ago

Hi there... I am having a issue in C# with checking if all the values in a array are True... I'm getting strange behaviour... so I wanted to ask to make sure this code is correct.. or if there is a deeper problem with my logic after this test happens...


            // Check for Draw
            bool draw = isSlotTaken.All(x => x);
            if (draw) { rungame = false; winner = "Draw"; }

Will that IF STATEMENT resolve is every single value in the bool isSlotTaken is set to true?

 

Thanks!

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop
13 minutes ago, A4L said:

Hi there... I am having a issue with checking if all the values in a array are True... I'm getting strange behaviour... so I wanted to ask to make sure this code is correct.. or if there is a deeper problem with my logic after this test happens...



            // Check for Draw
            bool draw = isSlotTaken.All(x => x);
            if (draw) { rungame = false; winner = "Draw"; }

Will that IF STATEMENT resolve is every single value in the bool isSlotTaken is set to true?

 

Thanks!

 

If you want to cycle through an array it's pretty easy, you can use for loops, or foreach.

For example, if you need to check all values in the array to be true then you would do something like this:


bool allValuesTrue = true;

for (int a = 0; a < array.length; a++)

{

if (array[a].status == false) { 

allValuesTrue = false; 

// Code to exit loop

}
}

The second something isn't true there is zero point in checking the rest if you need all to be true, and you can safely exit out.

Also when posting code questions, please indicate the language you're using.

Programmer and 3D Artist

Advertisement

You should probably mention the progamming language used somewhere in the post. It looks like it's C#, so I'll assume that.

With the given example, I think draw will be true if every element of isSlotTaken is a bool with the value true.

This sounds like a problem you should look into using a debugger -- are you familiar with debugging techniques? Breakpoints, etc.

Hello to all my stalkers.

1 hour ago, Lactose said:


bool draw = isSlotTaken.All(x => x);

With the given example, I think draw will be true if every element of isSlotTaken is a bool with the value true.

This is what I thought as well, I have done some tests and I also think this is how it works.. but I copied the code from stack exchange, so I do not really understand why it works. I think that there is just a problem in my logic not setting true after completing, so in some it is resolving false when I think it should be true.

1 hour ago, Lactose said:

This sounds like a problem you should look into using a debugger -- are you familiar with debugging techniques? Breakpoints, etc.

No not really. I have only been coding for about a week, and learning from a book called "The C# Players Guide". I am using the free visual studio and if I press f5 and there is an error I can see some issues in the log thing. I do not know about breakpoints and stuff though or how to "watch" variables or w/e. I assume the book just hasn't gotten to that stuff yet.

 

1 hour ago, Rutin said:

 



bool allValuesTrue = true;

for (int a = 0; a < array.length; a++)

{

if (array[a].status == false) { 

allValuesTrue = false; 

// Code to exit loop

}
}

 

This looks like it also works.. I did a test but I do prefer the simple format of the other way, now I know it actually functions as I thought it would.

1 hour ago, Lactose said:

You should probably mention the progamming language used somewhere in the post. It looks like it's C#, so I'll assume that.

I've edited the OP Title to include that I am, indeed, talking about C#

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop
53 minutes ago, A4L said:

I think that there is just a problem in my logic not setting true after completing, so in some it is resolving false when I think it should be true.

If so, I think the problem is with the contents of isSlotTaken.

Hello to all my stalkers.

Is there a way to modify the .ALL in a way so it only checks certain cells in the table?

I know I can do a for loop with a true false setting getting switched on matching cells.. but I was wondering if there was a quicker way.. kinda like all.

So... say I have....


 bool [] array = new boll[10]; 

.... is there a way to check that cell [1][5][9] is true and ignore all the other cells? Without using a standard for loop, in a "readable one like" type way... like with that .All thing?

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop
Advertisement
23 minutes ago, A4L said:

Is there a way to modify the .ALL in a way so it only checks certain cells in the table?

I know I can do a for loop with a true false setting getting switched on matching cells.. but I was wondering if there was a quicker way.. kinda like all.

So... say I have....



 bool [] array = new boll[10]; 

.... is there a way to check that cell [1][5][9] is true and ignore all the other cells? Without using a standard for loop, in a "readable one like" type way... like with that .All thing?


bool theseThreeCellsTrue = cell[1] && cell[5] && cell[9];

 

Hello to all my stalkers.

Nice! Thanks man! I need to get the hang of these condensed = things.. that make everything so much easier to read.

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop

This topic is closed to new replies.

Advertisement