# help with arrays and loops

• 11-21-2001
jdiazj1
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
• 11-21-2001
Fordy
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!!!!
}
• 11-22-2001
Garfield
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?
• 11-24-2001
jdiazj1
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.
• 11-24-2001
Salem
```#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; }```