hi,
I was wondering if someone has the disponibility to tell me what is wrong with this code. I am tryign to make the connect-4 game, but i am having some problems.
-so far, I have the board and the user's tokens working, but for some reason when it ask for the user's choice, the code skips the computer's choice one play, and then it throws the computer's choice twice in the next play. It keeps doing this throughout the whole process.
I've been trying to find the solution to the problem, but i can't find it and fix it.
If someone has any comments or solutions..would be greatly appreciated.
Thanks in advance.
code______________________________________________ _
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 6
#define F 4
void print_board(char board[N][N] )
{
int i, r, c, n[N];
for ( r = 0 ; r < N ; r++ )
{
for ( c = 0 ; c < N ; c++ )
{
printf( "( %c )", board[r][c] );
}
printf( "\n" );
}
for(i = 0; i < N; i++)
n[i]= i;
for( i = 0; i < N; i++)
printf(“ %d “, n[i]);
}
int check_for_winner(char board[N][N],int c, int r)
{
int r1, c1;
for ( r1 = 0 ; r1 <= 2 ; r1++ )
{
for ( c1 = 0 ; c1 <= 2 ; c1++ )
{
int i, y, x;
if ( r1 == 0 && c1 == 0 ) continue;
for ( i = 1 ; i < F ; i++ )
{
y = c + r1 * i;
x = r + c1 * i;
if ( y < 0 || y >= N ) break;
if ( x < 0 || x >= N ) break;
if ( board[y][x] != 'X' || board[y][x] != 'O')
break;
}
if ( i == F )
return 1;
else
return 0;
}
}
}
int check(char board[][N])
{
int r, c;
for ( r = 0 ; r < N ; r++ )
{
for ( c = 0 ; c < N ; c++ )
{
if ( board[r][c] =='X' || board[r][c] =='O' )
{
if( (check_for_winner( board,r,c )) ==1)
return 1;
else
return 0;
}
}
}
}
int player(char board[N][N])
{
int sel,i;
print_board( board );
printf("\nType a number between 0 and %d to make a move: “, N-1); scanf("%d",&sel);
if(sel >= 0 || sel <=6 )
{
for ( i = N ; i >= 0 ; i-- )
{
if (board[i][sel] == ' ')
{
board[i][sel] ='X';
break;
}
}
if(check( board)==1)
{
printf("You Win!\n");
printf("The Final Board: \n");
print_board( board );
return 1;
}
else
return 0;
}
else
{
printf(“ Invalid Entry. Try again!\n”)
player(board);
}
}
int computer(char board[N][N])
{
int sel1,i;
srand(time(NULL));
sel1=rand()% N;
printf("Computer selects column number %d\n",sel1);
for ( i = N ; i >= 0 ; i-- )
{
if(board[i][sel1] == ' ')
{
board[i][sel1] = 'O';
break;
}
}
if( check( board )==1)
{
printf("Computer wins!\n");
printf("The Final Board: \n");
print_board( board);
return 1;
}
else
return 0;
}
int main(void)
{
char board[N][N];
int i,j;
printf("\n Welcome to the game \” Connect %d \” \n\n",F);
for(i=0;i<N;i++)
for(j=0;j<N;j++)
board[i][j]=' ';
player(board);
computer(board);
do{
player(board);
if(player(board) == 1) break;
computer(board);
}while(computer(board)!=1);
return 0;
}
__________________________________________________ __