Thread: more help with arrays!

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    more help with arrays!

    hello guys, i was wondering if anybody can tell me what am i doing wrong with this code. the purpose of this code is to simulate the connect four game and i am supposed to use arrays. ok my problem is this. the game asks the user to select a column. and when it is selected, it drops an 'X' all the way to the bottom. ok i have that done. so my problem is that whenever i select the same column, the 'X' does not stack on top of the previous 'X' instead it just overwrites it and thats it. so thats is what i would like to do. for the 'X's to stack up. here is my code, i would really appriciate it. thanx.
    ------------------------------------------------------------------------------------
    #include <stdio.h>
    #define N 7
    #define F 4

    int main (void)
    {
    char board[N][N];
    int i,j,sel;

    printf("Welcome to Connect %d\n",F);


    for(i=0;i<N;i++)
    for(j=0;j<N;j++)
    board[i][j]='.';
    do
    {
    for(i=0;i<N;i++)
    {
    for(j=0;j<N;j++)
    printf("( %c )",board[i][j]);
    printf("\n\n");
    }

    printf("select a column: ");scanf("%d",&sel);
    for ( i = N-1 ; i >= 0 ; i++ )
    {
    if (board[i][sel-1] != '.') break;

    else if(board[i][sel-1]='X') \\im guessin that my problem is
    board[i--][sel-1]='X'; \\on these three lines
    else
    board[i][sel-1]='X'; \\...
    }
    }while(sel>=1 && sel<=N);

    return 0;
    }

  2. #2
    Unregistered
    Guest
    Maybe use == instead of = on the else if line?

  3. #3
    Unregistered
    Guest
    same thing....there was no difference.
    thankx for replying.....please i need more help..

  4. #4
    Unregistered
    Guest
    do
    {
    for(i=0;i<N;i++)
    {
    for(j=0;j<N;j++)
    printf("( %c )",board[i][j]);
    printf("\n\n");
    }

    printf("select a column: ");scanf("%d",&sel);
    for ( i = N-1 ; i >= 0 ; i-- ) /*count down instead of up*/
    {
    if (board[i][sel-1] == '.') /* Not sure about this algorithm*/
    {
    board[i][sel-1] = 'X';
    break;
    }

    }
    }while(sel>=1 && sel<=N);

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    thank you guys!

    thanx alot. it works. thank u very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM