Almost done with connect-4, NEED A LITTLE HELP

This is a discussion on Almost done with connect-4, NEED A LITTLE HELP within the C Programming forums, part of the General Programming Boards category; hi, I was wondering if someone has the disponibility to tell me what is wrong with this code. I am ...

  1. #1
    Unregistered
    Guest

    Unhappy Almost done with connect-4, NEED A LITTLE HELP

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

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Code:
    do{ 
     player(board); 
     if(player(board) == 1) break; 
     computer(board); 
    }while(computer(board)!=1);
    Follow this code through...
    1. player(board) - player makes a more
    2. if(player(board) == 1) break; - player makes another move. If he wins, break.
    3. computer(board) - computer makes a move
    4. while (computer (board) != 1); - computer makes another move. If he wins, break.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Unregistered
    Guest

    Thumbs up

    ok..I understand what you mean, and i got that idea in the main function. I still have the problem that when i run this code, it ask the user for a choice( 0 to 6), but when the computer puts the random number, it skips one play. In other words, user play correctly, but the computer throws the random number every two plays.
    I think there is a mistake in the check and check_for_winner function, for some reason is not returning the correct job. I can't figure out what it wrong with it.


    Thanks for replying QuestionC.

    I need more advices, from you guys (experts in programming).

  4. #4
    Unregistered
    Guest
    I think in your "do" loop in main you are calling the computer(board) function twice:

    do{
    player(board);
    if(player(board) == 1) break;
    computer(board); /* FIRST CALL */
    }while(computer(board)!=1); /* SECOND CALL */

    Either assign the first call to a variable and then use while(variable != 1) as your condition, or delete the first call.

    I hope that works.

  5. #5
    Unregistered
    Guest

    one solved...

    thanks ... I did your first advice ( using a variable) and is not throwing the computer' number twice anymore. But the computer's number still skiping on play.

    I am changing thing to see if it works in other way,but still can't figure out.


    SEE anything wrong in the code that i am not seeing?

  6. #6
    Unregistered
    Guest
    Same mistake, I think. You are also making two calls to player(board) before you get to the first call for computer(board).

    do{
    player(board); /* FIRST CALL */
    if(player(board) == 1) break; /*SECOND CALL*/
    computer(board);
    }while(computer(board)!=1);

  7. #7
    Unregistered
    Guest
    what have now is :

    do{
    player(board);
    if(player(board) == 1) break;
    variable = computer(board);
    }while(variable !=1);

    it does it once, but the computer's number skips one play

  8. #8
    Unregistered
    Guest
    Try this...?

    do{
    if(player(board) == 1) break;
    variable = computer(board);
    }while(variable !=1);

  9. #9
    Unregistered
    Guest
    hey ...thanks...that works.......




    one player
    one computer

    thanks

  10. #10
    Unregistered
    Guest
    NOw...
    the function check_for_winner that should return 1 when it finds 4 'X' or 4 'O', does not return anything...that makes that no one wins...
    Do you see any error?
    PLease help.

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, 04:40 PM
  2. connect timeout
    By X PaYnE X in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-14-2005, 10:30 PM
  3. Client timed-out once on connect(), can never connect() again
    By registering in forum Networking/Device Communication
    Replies: 6
    Last Post: 10-28-2003, 03:46 PM
  4. MySQL Connect from Outside Computer
    By juschillin in forum Windows Programming
    Replies: 0
    Last Post: 09-27-2002, 09:02 AM
  5. Advanced connect four game
    By Ion Blade in forum C++ Programming
    Replies: 10
    Last Post: 07-28-2002, 08:52 AM

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