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; }



LinkBack URL
About LinkBacks



...