Page 1 of 1

a logic problem?

Posted: Mon Sep 15, 2003 9:24 pm
by k_pnxi
I have a problem not know how to handle, say I have a tic tac toe game, which is like


Code: Select all

XXO
  OOX
  OOX
How can I use a nested for loop to check whether each player win the game, here is part what I do so far:

Code: Select all

for($i=0; $i<3; $i++)
 &#123;
       for($j=0; $j<3; $j++)
       &#123;
            if($grid&#1111;$i]&#1111;$j] == "X")
           &#123;
	     print("Player1 win");
           &#125;
           else if($grid&#1111;$i]&#1111;$j] == "O")
          &#123;
	    print("Player2 win");
           &#125; 
       &#125;
&#125;
the above logic should be wrong, I have no idea how to build the logic to use nested loops solve the problem, can anybody tell me how to figur it out?

Posted: Tue Sep 16, 2003 4:21 am
by pootergeist
Personally I'd use

1 -- 2 -- 4
8 - 16 - 32
64 128 256

so you can then add the number of the block to either X or O - if either have scores equalling 7, 56, 448, 73, 146, 292, 84 or 273 then they have won.

[note] used mental arithmetic to there, so check totals [/note][/list]

Posted: Tue Sep 16, 2003 2:08 pm
by m3rajk
pootergeist wrote:Personally I'd use

1 -- 2 -- 4
8 - 16 - 32
64 128 256

so you can then add the number of the block to either X or O - if either have scores equalling 7, 56, 448, 73, 146, 292, 94 or 273 then they have won.

[note] used mental arithmetic to there, so check totals [/note]
nearly correct. but i fixed it for you.