0

if i have

int win[][] ={{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};

Can I put condition in this way?

if(((win[0][0]) && (win[0][1]) && (win[0][2]))||

            ((win[1][0]) && (win[1][1]) && (win[1][2]))||   

            ((win[2][0]) && (win[2][1]) && (win[2][2]))||

            ((win[3][0]) && (win[3][1]) && (win[3][2]))||

            ((win[4][0]) && (win[4][1]) && (win[4][2]))||

            ((win[5][0]) && (win[5][1]) && (win[5][2]))||

            ((win[6][0]) && (win[6][1]) && (win[6][2]))||

            ((win[7][0]) && (win[7][1]) && (win[7][2]))||

            ((win[8][0]) && (win[8][1]) && win[8][2]))) 
2
  • 3
    I don't see why not... though why you would do it that way? Have you considered looping over every group of 3? Commented Nov 11, 2011 at 13:52
  • 1
    why don't you try yourself first? Commented Nov 11, 2011 at 13:53

3 Answers 3

2

Your array is 8x3, means last element will be win[7][2]. So calling win[8][0] will throw an ArrayIndexOutOfBounds exception. If you correct this error, your code will work. Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

For a clearer version of this, why not try:

for(int i = 0; i < 8; i++) {
    if(win[i][0] && win[i][1] && win[i][2]) {
        doStuff();
        break;
    }
}

2 Comments

it's ok your solution, but i don't understand exactly how it works...for exemple if i=4, what will be the condition that will be verified?
You just answered your own question. When i=4, the is in the if statement will be replaced with 4s. Since your original condition was if ANY set of three are all true (because of your use of the || OR operator). Mine adds the break because, after one of the conditions are true (e.g. if win[2][0] && win[2][1] && win[2][2] evaluates to true), there is no reason to continue evaluating the remainder of the sets.
-1

If your aim is completely confuse the reader - you can

2 Comments

don't have enough reputation yet to make comments
Could you please explain why do you down vote this answer. Do you think it is ok to write such conditions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.