Link to home
Start Free TrialLog in
Avatar of On_The_Level
On_The_Level

asked on

Java array Connect 4 search, help!

Ok, i am attempting to make a connect four like game in java, however i have hit a problem.  The problem is searching for the four counters in a row!  It is a little difficult to explain, but basicaly here is the code for searching for 4 next to eachother in a row (horizontaly accross the board) (which i thought should work, and find four in a row):

  int x=0;
  int y=0;
  int c=1;
  int d=0;
  int noinrow = 0;
 
 
 for (int row=0; row<6; row++)
 {
       for(int column=0; column<4; column++)
       {
             for(int check=0;check<3; check++)
             {
                  if (grid.get_pos(x,y)==grid.get_pos(c,d))
                  {
                        noinrow = noinrow + 1;      
                  }
                  c = c+1;
                  
             }
             if ((noinrow==3)&(grid.get_pos(x,y)!=0))
             {
                   winner=board.get_pose(x,y);
             }
             x = x +1;
             c = x;
       }
       x = 0;
       c = 0;
       y = y +1;
       d = d+1;
 }

ok a few thing:

grid.get_pos(x,y)  gets the value stored in the array at possition (x,y).  The array is the connect four grid.  Ether a 1 for player 1s counter or a 2 for player 2's counter is stored in the array, or 0 if empty.

winner - this displays a message box stating who is the winner.

the idea was that it would search along the bottom line, then the next line up etc. til if found, if any, four in a row.

Ok now i though this should work but it dosnt.  I know this (if it worked) only searches along the rows (horizontaly) of the grid, but i figured if i could get this to work, i could, with ALOT of difficulty get the others to work - searching verticaly and diagonaly.

Anywhy, this dont work, can anyone suggest what is wrong, or other ways of doing it?

Any help would be MUCH appreciated.  And i would be VERY greatfull for any help.

Cheers.
Avatar of hoomanv
hoomanv
Flag of Canada image

see my implementation (only horizontaly)
do the same for vertical
i'll search for a good algorithm for diagonal

      final int ROW = 6, COL = 7;
      
      int board[][] = {
            {0,0,0,0,0,0,0},
            {0,0,1,1,0,0,0},
            {0,0,2,2,0,0,0},
            {0,0,2,2,0,0,0},
            {0,0,1,1,1,1,0},
            {0,0,2,1,2,2,0},
      };
      
      int winner = 0, count = 0;
      outer:
      for(int r = 0; r < ROW; r++) {
            count = winner = 0;
            for(int c = 0; c < COL; c++) {
                  if(board[r][c] != 0) {
                        if(winner == board[r][c]) {
                              if(++count == 4)
                                    break outer;
                        }
                        else {
                              winner = board[r][c];
                              count = 1;
                        }
                  }
                  else if(winner != 0) {
                        count = winner = 0;
                  }
            }
      }
      
      System.out.println(winner);
ASKER CERTIFIED SOLUTION
Avatar of hoomanv
hoomanv
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of On_The_Level
On_The_Level

ASKER

Ah, thats great, that works a treet!  Much apperciated, any idea how im am going to go about doing the diagonal way?
Sorry can i just ask a question.

What does and how does this section work:

                        if(++count == 4)
                              break outer;


It may be a bit of a daft question sorry, but im used to if statements with brackets etc. like this

if(example==example2)
{

    do whatever;
}
if(++count == 4)
    break outer;

is equivallent to

count++; // or ++count;
if(count == 4) {
    break outer;
}

The use of brackets is only required if the command is run over several lines
++count == 4
it first increases the count value by one and then compares it with 4

count++ == 4
first compares and then increases, so if count is 3 and you say count++ == 4 it returns false because 3 was compared to 4
oh right ok, i get you, thanks for that.

do you have any idea how i would go about searching diagonaly?
> do you have any idea how i would go about searching diagonaly?

give me time
more points are also appreciate :)
no problem ;-)
see what im trying to achieve.

class Test {
      static final int NUMROW = 6, NUMCOL = 7;
      
    public static void main(String[] args) throws Exception {      
            int board[][] = {
                  {0,1,1,1,0,0,0},
                  {1,0,1,1,0,0,0},
                  {1,0,2,2,0,0,0},
                  {1,0,2,2,0,0,0},
                  {0,0,1,1,1,0,1},
                  {2,1,2,2,2,2,1},
            };
            
            for(int c = NUMCOL; c > 0; c--) {
                  for(int r = 0, cc = c; r < NUMCOL - c; r++, cc++) {
                        System.out.print(board[r][cc] + " ");
                  }
                  System.out.println();
            }
            for(int r = 0; r < NUMROW; r++) {
                  for(int c = 0, rr = r; c < NUMROW - r; c++, rr++) {
                        System.out.print(board[rr][c] + " ");
                  }
                  System.out.println();
            }
    }
}

if you cant continue with it let me know
ok mmmm :-s    I shall give it a bash, see how i get on
wasnt that clear ?
my prog produces the diagonal values left-up to right-down. you can change it to have left-down to right-up too
0
0 0
0 0 0
1 0 0 0
1 1 0 0 1
1 1 2 0 0 1
0 0 2 2 1 2
1 0 2 1 2
1 0 1 2
1 0 2
0 1
2
Ah yeah i see, i could then search through these to find a matching four in a row.
this is more efficient

for(int c = NUMCOL - 4; c > 0; c--) {
      for(int r = 0, cc = c; r < NUMCOL - c; r++, cc++) {
            System.out.print(board[r][cc] + " ");
      }
      System.out.println();
}
for(int r = 0; r < NUMROW - 3; r++) {
      for(int c = 0, rr = r; c < NUMROW - r; c++, rr++) {
            System.out.print(board[rr][c] + " ");
      }
      System.out.println();
}

1 0 0 0
1 1 0 0 1
1 1 2 0 0 1
0 0 2 2 1 2
1 0 2 1 2
1 0 1 2

as you only need to search in terms longer than 4 digits
Hi right, yet again, i though this would work but obviously dosnt:

       int [][] array = new int [6][7];
       final int NUMROW = 6, NUMCOL =7;
       
       for(int c = NUMCOL - 4; c > 0; c--)
       {
             for(int r = 0, cc = c; r < NUMCOL - c; r++, cc++)
             {
                   array[r][cc] = board.get_state(r,cc);
             }
       }
       for(int r = 0; r < NUMROW - 3; r++)
       {
             for(int c = 0, rr = r; c < NUMROW - r; c++, rr++)
             {
                   array[rr][c] = board.get_state(rr,c);
             }
       }
 
 
 
      int count3 = 0;
     outer3:
 for(int r3 = 0; r3 < 6; r3++)
  {
        for(int c3 = 0; c3 < 7; c3++)
        {
          if(array[c3][r3] != 0)
          {
              if(who_won == array[c3][r3])
              {
                    if(++count3 == 4)
                    break outer3;
              }
              else
              {
                    who_won = array[c3][r3];
                    count3 = 1;
              }
        }
        else if(who_won != 0)
        {
              count3 = who_won = 0;
        }
     }
     count3 = who_won = 0;
   }
 

any suggestions?

Cheers
> array[rr][c] = board.get_state(rr,c);
what is the purpose of this line ?
this puts the value at (rr,c) ether 1, 2 or 0 in the new array at possition (rr,c). by doing this this should allow me to search through the new array (called array) to find diagonal matches.
anyway, run the below code and see the result
all you have to do is to search through the 'arr' row by row

class Test {
    static final int NUMROW = 6, NUMCOL = 7;
     
    public static void main(String[] args) throws Exception {
            int arr[][] = new int[26][];
            int k = 0;
            
            int board[][] = {
                  {0,1,1,1,0,0,0},
                  {1,0,1,1,0,0,0},
                  {1,0,2,2,0,0,0},
                  {1,0,2,2,0,0,0},
                  {0,0,1,1,2,0,1},
                  {2,1,2,2,2,2,1},
            };
       
            for(int r = 0; r < NUMROW; r++, k++) {
                  arr[k] = new int[NUMCOL];
                  for(int c = 0; c < NUMCOL; c++) {
                        arr[k][c] = board[r][c];
                  }
            }
            
            for(int c = 0; c < NUMCOL; c++, k++) {
                  arr[k] = new int[NUMROW];
                  for(int r = 0; r < NUMROW; r++) {
                        arr[k][r] = board[r][c];
                  }
            }
            //////////
            for(int c = NUMCOL - 4; c > 0; c--, k++) {
                  arr[k] = new int[NUMCOL - c];
                  for(int r = 0, cc = c; r < NUMCOL - c; r++, cc++) {
                        arr[k][r] = board[r][cc];
                  }
            }
            
            for(int r = 0; r < NUMROW - 3; r++, k++) {
                  arr[k] = new int[NUMROW - r];
                  for(int c = 0, rr = r; c < NUMROW - r; c++, rr++) {
                        arr[k][c] = board[rr][c];
                  }
            }
            ////////////
            for(int c = 3; c < NUMCOL - 1; c++, k++) {
                  arr[k] = new int[c + 1];
                  for(int r = 0, cc = c; cc >= 0; r++, cc--) {
                        arr[k][r] = board[r][cc];
                  }
                  
            }
            
            for(int r = 0; r < NUMROW - 3; r++, k++) {
                  arr[k] = new int[NUMROW - r];
                  for(int i = 0, c = NUMCOL - 1, rr = r; rr < NUMROW; c--, rr++, i++) {
                        arr[k][i] = board[rr][c];
                  }
            }

            // print all
            for(int i = 0; i < k; i++) {
                  for(int j = 0; j < arr[i].length; j++)
                        System.out.print(arr[i][j] + " ");
                  System.out.println();
            }
    }
}
man you have confused me to ****.

Sorry i am sort of a beginner at java, as u prob guessed
what is confusing ?
the code provides all horizental + vertical + diagonal combinations in a 2d array in such a way that each row of the arr is a combination.
the arr has 26 rows : 7(vertical) + 6(horizental) + 12(digonal > 4)
ok i dont know but i cant get any of that to work.

Surelly this should work, checking for 4 matching diagonaly for columns 1 - 3 only:

int count3 = 0;


outer3:
for(int c=1, s=6;c<4; s--, c++)
{
      for(int x=c,y=0; x<s; x++, y++)
      {
            if(board.get_state(x,y) != 0)
            {
                  if(who_won == board.get_state(x,y))
                  {
                        if(++count3 == 4)
                        break outer3;
                  }
                  else
                  {
                        who_won = board.get_state(x,y);
                        count3 = 1;
                  }
            }
            else if(who_won != 0)
        {
              count3 = who_won = 0;
        }
     }
     count3 = who_won = 0;
}
> ok i dont know but i cant get any of that to work.
did you have problem with my last code ? wasn't that what you want ?
when i went to search through the array, if found no matching in a row.
the content of "int arr[][] = new int[26][]" after the processing will be

0 1 1 4 0 0 0
1 0 1 1 0 0 0
1 0 2 2 0 0 0
1 0 2 2 0 0 0
0 0 1 1 2 0 1
2 1 2 2 2 2 1
0 1 1 1 0 2
1 0 0 0 0 1
1 1 2 2 1 2
4 1 2 2 1 2
0 0 0 0 2 2
0 0 0 0 0 2
0 0 0 0 1 1
4 0 0 0
1 1 0 0 1
1 1 2 0 0 1
0 0 2 2 2 2
1 0 2 1 2
1 0 1 2
4 1 0 1
0 1 2 0 0
0 0 2 2 0 2
0 0 0 2 1 1
0 0 0 1 2
0 0 2 2

try to find the matching 4 in this array only not in the main game board
post your code and I'll check it
sorry the above has a mistake

for this board
            int board[][] = {
                  {0,1,1,1,0,0,0},
                  {1,0,1,1,0,0,0},
                  {1,0,2,2,0,0,0},
                  {1,0,2,2,0,0,0},
                  {0,0,1,1,2,0,1},
                  {2,1,2,2,2,2,1},
            };

the array content is

0 1 1 1 0 0 0
1 0 1 1 0 0 0
1 0 2 2 0 0 0
1 0 2 2 0 0 0
0 0 1 1 2 0 1
2 1 2 2 2 2 1
0 1 1 1 0 2
1 0 0 0 0 1
1 1 2 2 1 2
1 1 2 2 1 2
0 0 0 0 2 2
0 0 0 0 0 2
0 0 0 0 1 1
1 0 0 0
1 1 0 0 1
1 1 2 0 0 1
0 0 2 2 2 2
1 0 2 1 2
1 0 1 2
1 1 0 1
0 1 2 0 0
0 0 2 2 0 2
0 0 0 2 1 1
0 0 0 1 2
0 0 2 2
I Keep getting out of bounce errors

       final int NUMROW = 6, NUMCOL = 7;
        
          int arr[][] = new int[26][];
          int k = 0;

 

     for(int r = 0; r < NUMROW; r++, k++) {
               arr[k] = new int[NUMCOL];
               for(int c = 0; c < NUMCOL; c++) {
                    arr[k][c] = board.get_state(r,c);
               }
          }
         
          for(int c = 0; c < NUMCOL; c++, k++) {
               arr[k] = new int[NUMROW];
               for(int r = 0; r < NUMROW; r++) {
                    arr[k][r] = board.get_state(r,c);
               }
          }
          //////////
          for(int c = NUMCOL - 4; c > 0; c--, k++) {
               arr[k] = new int[NUMCOL - c];
               for(int r = 0, cc = c; r < NUMCOL - c; r++, cc++) {
                    arr[k][r] = board.get_state(r,cc);
               }
          }
         
          for(int r = 0; r < NUMROW - 3; r++, k++) {
               arr[k] = new int[NUMROW - r];
               for(int c = 0, rr = r; c < NUMROW - r; c++, rr++) {
                    arr[k][c] = board.get_state(rr,c);
               }
          }
          ////////////
          for(int c = 3; c < NUMCOL - 1; c++, k++) {
               arr[k] = new int[c + 1];
               for(int r = 0, cc = c; cc >= 0; r++, cc--) {
                    arr[k][r] = board.get_state(r,cc);
               }
               
          }
         
          for(int r = 0; r < NUMROW - 3; r++, k++) {
               arr[k] = new int[NUMROW - r];
               for(int i = 0, c = NUMCOL - 1, rr = r; rr < NUMROW; c--, rr++, i++) {
                    arr[k][i] = board.get_state(rr,c);
               }
          }

          // print all
          for(int i = 0; i < k; i++) {
               for(int j = 0; j < arr[i].length; j++)
                    System.out.print(arr[i][j] + " ");
               System.out.println();
          }
         
               int count2 = 0;
     outer2:
  for(int r2 = 0; r2 < 26; r2++)
  {
        for(int c2 = 0; c2 < 7; c2++)
        {
          if(board.get_state(c2,r2) != 0)
          {
              if(who_won == board.get_state(c2,r2))
              {
                    if(++count2 == 4)
                    break outer2;
              }
              else
              {
                    who_won = board.get_state(c2,r2);
                    count2 = 1;
              }
        }
        else if(who_won != 0)
        {
              count2 = who_won = 0;
        }
     }
     count2 = who_won = 0;
   }
cracked it, i got it to check diagonaly and it works!

heres the code:

if(who_won==0)
  {
 
int count3 = 0;
int place = 0;
int n = 4;
int row = 2;
int col = 0;



outer3:
for(int a=1;a<3; a++)
{
for(int b=1, num=0;b<4; b++, num++)
{
      for(int x=col, y=row; x<n; x++, y++)
      {      
            if(board.get_state(x,y) != 0)
            {
                  if(who_won == board.get_state(x,y))
                  {
                        if(++count3 == 4)
                        break outer3;
                  }
                  else
                  {
                        who_won = board.get_state(x,y);
                        count3 = 1;
                  }
            }
            else if(who_won != 0)
        {
              count3 = who_won = 0;
        }

     }
     
     count3 = who_won = 0;
     
     if(place==0)
     {
     row=row-1;
     n++;
     }
     else if(place==1)
     {
           col=col+1;
   
     }
     
}
place=1;
row=0;
col=1;

}


}
        

if(who_won==0)
  {
 
int count3 = 0;
int place = 0;
int n = 4;
int row = 3;
int col = 0;



outer4:
for(int a=1;a<3; a++)
{
for(int b=1, num=0;b<4; b++, num++)
{
      for(int x=col, y=row; x<n; x++, y--)
      {


            System.out.print(x);
            System.out.print(y);
            System.out.println();

            
            if(board.get_state(x,y) != 0)
            {
                  if(who_won == board.get_state(x,y))
                  {
                        if(++count3 == 4)
                        break outer4;
                  }
                  else
                  {
                        who_won = board.get_state(x,y);
                        count3 = 1;
                  }
            }
            else if(who_won != 0)
        {
              count3 = who_won = 0;
        }

     }
     
     count3 = who_won = 0;
     
     if(place==0)
     {
     row=row+1;
     n++;
     }
     else if(place==1)
     {
           col=col+1;
   
     }
     
}
place=1;
row=5;
col=1;

}


}