Thread: help with arrays and loops

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

    help with arrays and loops

    im sorry about this repost. i just registered my screen name so i can be certain that i could go bak and modify my question.

    i need help with the for loops to check the winning combinations.
    thank you

    using an array, how can i check if there is a certain number of 'chips' in a row, horizontal or diagonal. i know that there are a bunch a for loops. its cause this is bearly my first programming class and we just went over this like 4 days ago and he just went over regular arrays like a[5] and like 1/2 a class on a[5][5] so im totally lost. it will be totally appreciated. PLEASE help me. i cant get it to work.

    for example:

    a[5][5];

    ( ) ( ) ( ) ( ) ( )
    ( ) ( ) ( ) ( ) ( )
    ( ) ( ) ( ) ( ) ( )
    ( ) ( ) ( ) ( ) ( )
    (o) (o) (o) (o) ( )
    this is a winner

    ( ) ( ) ( ) ( ) ( )
    (o) ( ) ( ) ( ) ( )
    (o) ( ) ( ) ( ) ( )
    (o) ( ) ( ) ( ) ( )
    (o) ( ) ( ) ( ) ( )
    this is a winner

    ( ) ( ) ( ) ( ) ( )
    ( ) ( ) ( ) (o) ( )
    ( ) ( ) (o) ( ) ( )
    ( ) (o) ( ) ( ) ( )
    (o) ( ) ( ) ( ) ( )
    this is a winner

    ( ) ( ) ( ) ( ) ( )
    ( ) (o) ( ) ( ) ( )
    ( ) ( ) (o) ( ) ( )
    ( ) ( ) ( ) (o) ( )
    ( ) ( ) ( ) ( ) (o)
    this is a winner







    ok thanx. i understand the vertical and horizontal loops..but what about the diagonal loops.. thanx
    Last edited by jdiazj1; 11-22-2001 at 02:48 PM.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Well if your going for 1 as slot full and 0 as slot empty;

    ( ) ( ) ( ) ( ) ( )
    ( ) ( ) ( ) ( ) ( )
    ( ) ( ) ( ) ( ) ( )
    ( ) ( ) ( ) ( ) ( )
    (o) (o) (o) (o) ( )

    a[4][0] - a[4][3] are 1's

    the rest are 0's. Remember that a[5][5] creates a multidimentional array with 25 members, but the first is always 0 and the last would be 4.

    You could just assess that each of them are not zero. So for the above example,

    if (a[4][0] && a[4][1] && a[4][2] && a[4][3])
    {
    //winner!!!!
    }

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    What you could do is have a for loop (for the incrementation of variables) and test to see if it is this much more (addition) or this much times (multiplication).

    hmmm...do you understand what I'm getting at?
    1978 Silver Anniversary Corvette

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    6
    well to tell u the truth, im just beginner in programming and well like i had said before. my professor only talked about arrays for like 2 days, and well im sorry but i dont know where u are coming from. what do u mean by crementation of variables and testing to see if it is this much more (addition) or this much times (multiplication) thanx.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Mmmm - how about
    Code:
    #include <stdio.h>
    
    #define MAX 5
    
    void print_board ( char board[][MAX] ) {
        int r, c;
        for ( r = 0 ; r < MAX ; r++ ) {
            for ( c = 0 ; c < MAX ; c++ ) {
                printf( "%c", board[r][c] );
            }
            printf( "\n" );
        }
    }
    
    void check_for_line ( char board[][MAX], int start_r, int start_c ) {
        // at this point, we can go in 8 possible directions
        // and we already know that board[start_r][start_c] is an 'X'
        int r1, c1;
    
        // r1 = -1 is previous row, 0 is current row, +1 is next row
        for ( r1 = -1 ; r1 <= 1 ; r1++ ) {
            for ( c1 = -1 ; c1 <= 1 ; c1++ ) {
                int i, r, c;
    
                // both zeros mean we don't move anywhere, so skip this case
                if ( r1 == 0 && c1 == 0 ) continue;
    
                for ( i = 1 ; i < 4 ; i++ ) {
                    // the next square, in our chosen direction
                    r = start_r + r1 * i;
                    c = start_c + c1 * i;
    
                    // check that we are still on the board - exit if we are not
                    if ( r < 0 || r >= MAX ) break;
                    if ( c < 0 || c >= MAX ) break;
    
                    // check that we still have a matching piece - exit if not
                    if ( board[r][c] != 'X') break;
                }
    
                // got 4 in a row - report the good news!
                if ( i == 4 ) {
                    printf( "Line found starting at %d,%d ending at %d,%d\n",
                            start_r, start_c, r, c );
                }
            }
        }
    }
    
    // check every position of the board, to see if it's the start of a line
    void check ( char board[][MAX] ) {
        int r, c;
        for ( r = 0 ; r < MAX ; r++ ) {
            for ( c = 0 ; c < MAX ; c++ ) {
                if ( board[r][c] == 'X' ) {
                    check_for_line( board, r, c );
                }
            }
        }
    }
    
    int main ( ) {
        // example board with 1,2,3,4 length lines of X
        char board[MAX][MAX] = {
            { '.', '.', '.', '.', 'X' },
            { 'X', 'X', '.', 'X', '.' },
            { '.', '.', 'X', '.', '.' },
            { '.', 'X', 'X', '.', '.' },
            { '.', '.', 'X', '.', 'X' },
        };
        print_board( board );
        check( board );
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help using arrays and loops for math
    By LLINE in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2008, 04:09 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. while loops with arrays
    By volk in forum C Programming
    Replies: 2
    Last Post: 02-04-2003, 07:22 PM
  4. Arrays and loops
    By Zewu in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2002, 11:05 AM
  5. loops and arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-03-2001, 03:12 PM