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