Thread: Is there a bug in this part of my algorithm for connect 4?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Is there a bug in this part of my algorithm for connect 4?

    Hi,

    The following is a part of my algorithm for making a winning move in a game of connect four. However, it doen't really work. It can check make win moves if the line is a diagonal one, but not vertical or horizontal ones. I'd already spent some hours on it, but still can't figure out where it went wrong. I've already written a driver program to test the function for those who are willing to help me.

    Thnx in advance

    Code:
    #include <stdio.h>
    #include <conio.c>
    
    char board[ 7 ][ 6 ] = { {' ', ' ', ' ', 'X', 'X', 'X'   },
                             {' ', ' ', ' ', ' ', ' ', ' '   },
                             {' ', ' ', ' ', ' ', ' ', ' '   },
                             {' ', ' ', ' ', ' ', ' ', ' '   },
                             {' ', ' ', ' ', ' ', ' ', ' '   },
                             {' ', ' ', ' ', ' ', ' ', ' '   },
                             {' ', ' ', ' ', ' ', ' ', ' '   } };
    
    int makeWinMove( char currentPlayer, int mode )
    {
       int row;
       int col;
    
       /* check vertically */
       for ( col = 0; col < 7; col++ )
          for ( row = 5; row < 2; row-- ) {
    
             if ( board[ col ][ row ] == currentPlayer &&
                  board[ col ][ row - 1 ] == currentPlayer &&
                  board[ col ][ row - 2 ] == currentPlayer &&
                  board[ col ][ row - 3 ] != 'X' &&
                  board[ col ][ row - 3 ] != 'O' ) {
    
                  return 1; /* indicating success */
             }
    
          }
    
       /* check horizontally */
       for ( row = 0; row < 6; row++ )
          for ( col = 0; col < 4; col++ ) {
             if ( board[ col ][ row ] == currentPlayer &&
                  board[ col + 1 ][ row ] == currentPlayer &&
                  board[ col + 2 ][ row ] == currentPlayer &&
                  board[ col + 3 ][ row ] != 'X' &&
                  board[ col + 3 ][ row ] != 'O' &&
                  ( board[ col + 3 ][ row + 1 ] != ' ' || row == 6 )) {
    
                  return 1;
             }
             if ( board[ col ][ row ] == currentPlayer &&
                  board[ col + 1 ][ row ] == currentPlayer &&
                  board[ col + 3 ][ row ] == currentPlayer &&
                  board[ col + 2 ][ row ] != 'X' &&
                  board[ col + 2 ][ row ] != 'O' &&
                  ( board[ col + 2 ][ row + 1 ] != ' ' || row == 6 )) {
    
                  return 1;
             }
             if ( board[ col ][ row ] == currentPlayer &&
                  board[ col + 3 ][ row ] == currentPlayer &&
                  board[ col + 2 ][ row ] == currentPlayer &&
                  board[ col + 1 ][ row ] != 'X' &&
                  board[ col + 1 ][ row ] != 'O' &&
                  ( board[ col + 1 ][ row + 1 ] != ' ' || row == 6 )) {
    
                  return 1;
             }
             if ( board[ col + 3 ][ row ] == currentPlayer &&
                  board[ col + 1 ][ row ] == currentPlayer &&
                  board[ col + 2 ][ row ] == currentPlayer &&
                  board[ col ][ row ] != 'X' &&
                  board[ col ][ row ] != 'O' &&
                  ( board[ col ][ row + 1 ] != ' ' || row == 6 )) {
    
                  return 1;
             }
          }
    
       /* check diagonally (NE) */
       for ( row = 5; row > 2; row-- )
          for ( col = 0; col < 4; col++ ) {
             if ( board[ col ][ row ] == currentPlayer &&
                  board[ col + 1 ][ row - 1 ] == currentPlayer &&
                  board[ col + 2 ][ row - 2 ] == currentPlayer &&
                  board[ col + 3 ][ row - 3 ] != 'X' &&
                  board[ col + 3 ][ row - 3 ] != 'O' &&
                  ( board[ col + 3 ][ row - 3 + 1 ] != ' ' || row == 5 )) {
    
                  return 1;
             }
             if ( board[ col ][ row ] == currentPlayer &&
                  board[ col + 1 ][ row - 1 ] == currentPlayer &&
                  board[ col + 3 ][ row - 3 ] == currentPlayer &&
                  board[ col + 2 ][ row - 2 ] != 'X' &&
                  board[ col + 2 ][ row - 2 ] != 'O' &&
                  ( board[ col + 2 ][ row - 2 + 1 ] != ' ' || row == 5 )) {
    
                  return 1;
             }
             if ( board[ col ][ row ] == currentPlayer &&
                  board[ col + 3 ][ row - 3 ] == currentPlayer &&
                  board[ col + 2 ][ row - 2 ] == currentPlayer &&
                  board[ col + 1 ][ row - 1 ] != 'X' &&
                  board[ col + 1 ][ row - 1 ] != 'O' &&
                  ( board[ col + 1 ][ row - 1 + 1 ] != ' ' || row == 5 )) {
    
                  return 1;
             }
             if ( board[ col + 3 ][ row - 3 ] == currentPlayer &&
                  board[ col + 1 ][ row - 1 ] == currentPlayer &&
                  board[ col + 2 ][ row - 2 ] == currentPlayer &&
                  board[ col ][ row ] != 'X' &&
                  board[ col ][ row ] != 'O' &&
                  ( board[ col ][ row + 1 ] != ' ' || row == 5 )) {
    
                  return 1;
             }
    
          }
    
       /* check diagonally (NW) */
       for ( row = 5; row > 2; row-- )
          for ( col = 6; col < 2; col-- ) {
             if ( board[ col ][ row ] == currentPlayer &&
                  board[ col - 1 ][ row - 1 ] == currentPlayer &&
                  board[ col - 2 ][ row - 2 ] == currentPlayer &&
                  board[ col - 3 ][ row - 3 ] != 'X' &&
                  board[ col - 3 ][ row - 3 ] != 'O' &&
                  ( board[ col - 3 ][ row - 3 + 1 ] != ' ' || row == 5 )) {
    
                  return 1;
             }
             if ( board[ col ][ row ] == currentPlayer &&
                  board[ col - 1 ][ row - 1 ] == currentPlayer &&
                  board[ col - 3 ][ row - 3 ] == currentPlayer &&
                  board[ col - 2 ][ row - 2 ] != 'X' &&
                  board[ col - 2 ][ row - 2 ] != 'O' &&
                  ( board[ col - 2 ][ row - 2 + 1 ] != ' ' || row == 5 )) {
    
                  return 1;
             }
             if ( board[ col ][ row ] == currentPlayer &&
                  board[ col - 3 ][ row - 3 ] == currentPlayer &&
                  board[ col - 2 ][ row - 2 ] == currentPlayer &&
                  board[ col - 1 ][ row - 1 ] != 'X' &&
                  board[ col - 1 ][ row - 1 ] != 'O' &&
                  ( board[ col - 1 ][ row - 1 + 1 ] != ' ' || row == 5 )) {
    
                  return 1;
             }
             if ( board[ col - 3 ][ row - 3 ] == currentPlayer &&
                  board[ col - 1 ][ row - 1 ] == currentPlayer &&
                  board[ col - 2 ][ row - 2 ] == currentPlayer &&
                  board[ col ][ row ] != 'X' &&
                  board[ col ][ row ] != 'O' &&
                  ( board[ col ][ row + 1 ] != ' ' || row == 5 )) {
    
                  return 1;
             }
          }
    
       return 0;
    }
    
    int main()
    {
       int result;
    
       if ( ( result = makeWinMove( 'X', 0 ) ) == 1 )
          printf( "Made a win move...\n" );
    
       system("PAUSE");
       return 0;
    }
    Last edited by Nutshell; 04-26-2002 at 07:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read part of file
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 01:51 PM
  2. algorithm for duplicate file checking help
    By geekoftheweek in forum C Programming
    Replies: 1
    Last Post: 04-04-2009, 01:46 PM
  3. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  4. a simple algorithm and questions
    By ustuzou in forum C++ Programming
    Replies: 0
    Last Post: 02-18-2002, 11:12 AM
  5. string searching algorithm......help
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-07-2001, 09:59 AM