Winning conditions (Connect 4)

This is a discussion on Winning conditions (Connect 4) within the Game Programming forums, part of the General Programming Boards category; I have a Connect 4 game almost complete.. but i'm having trouble coding the winning conditions. I know I could ...

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Winning conditions (Connect 4)

    I have a Connect 4 game almost complete.. but i'm having trouble coding the winning conditions. I know I could do it with like 50 if statements but that seems highly unreasonable.

    Can any of you experienced coders help me? This is what I have so far; its extremly ugly..

    Code:
    	USHORT row = 0;
    	for (USHORT i = 0; i<4; i++)
    	{
    		for (USHORT ii = 1; ii<5; ii++)
    		{	
    			for (USHORT iii = 2; iii<6; iii++)
    			{
    				for (USHORT iiii = 3; iiii<7; iiii++)
    				{
    					if ((board[row][i] == board[row][ii]) && (board[row][ii] == board[row][iii]) && (board[row][iii] == board[row][iiii]))
    					{
    						if (player == player1)
    						{
    							//horizontal win;
    							return 1;
    						} else {
    							//horizontal win;
    							return 2;
    						}
    					}
    					if (iiii == 6)
    					{
    						if (row < 6)
    						{
    							row++;
    						}
    						i = 0;
    						ii = i++;
    						iii = ii++;
    						iiii = iii++;
    					}	
    				}
    			}
    		}
    	}
    Does that seem reasonable?

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    since there are a finite amount of winning conditions... [and many of them are very similar...] you can do it with loops... you don't need anything really nested like you have, but you can easily go through all conditions provided you know what they are...
    hasafraggin shizigishin oppashigger...

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I thought we already answered this. There was a great algorithm posted in another connect 4 thread. You may want to check that out.

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Sorry, I missed that thread.. and im having trouble locating it. Anyone know what its name was?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Non-blocking connect()?
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 04-22-2009, 03:40 PM
  2. connect() function, strange error
    By Mr_Miguel in forum C Programming
    Replies: 1
    Last Post: 12-12-2006, 05:51 PM
  3. connect timeout
    By X PaYnE X in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-14-2005, 09:30 PM
  4. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 02:46 PM
  5. Advanced connect four game
    By Ion Blade in forum C++ Programming
    Replies: 10
    Last Post: 07-28-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21