a logic problem?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
k_pnxi
Forum Newbie
Posts: 1
Joined: Mon Sep 15, 2003 9:24 pm

a logic problem?

Post 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?
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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]
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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.
Post Reply