?
Solved

Java array Connect 4 search, help!

Posted on 2006-04-20
28
Medium Priority
?
673 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:On_The_Level
  • 15
  • 13
28 Comments
 
LVL 14

Expert Comment

by:hoomanv
ID: 16501312
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);
0
 
LVL 14

Accepted Solution

by:
hoomanv earned 500 total points
ID: 16501402
oh my code is bugy
move this line "count = winner = 0;" to the end of outer for loop to correct it
so it looks like

      for(int r = 0; r < ROW; r++) {
            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;
                  }
            }
            count = winner = 0;
      }
0
 

Author Comment

by:On_The_Level
ID: 16509203
Ah, thats great, that works a treet!  Much apperciated, any idea how im am going to go about doing the diagonal way?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:On_The_Level
ID: 16509786
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;
}
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16509834
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
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16509882
++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
0
 

Author Comment

by:On_The_Level
ID: 16510150
oh right ok, i get you, thanks for that.

do you have any idea how i would go about searching diagonaly?
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16510276
> do you have any idea how i would go about searching diagonaly?

give me time
more points are also appreciate :)
0
 

Author Comment

by:On_The_Level
ID: 16510304
no problem ;-)
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16510494
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
0
 

Author Comment

by:On_The_Level
ID: 16510653
ok mmmm :-s    I shall give it a bash, see how i get on
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16510786
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
0
 

Author Comment

by:On_The_Level
ID: 16510811
Ah yeah i see, i could then search through these to find a matching four in a row.
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16510995
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
0
 

Author Comment

by:On_The_Level
ID: 16519794
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
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16519801
> array[rr][c] = board.get_state(rr,c);
what is the purpose of this line ?
0
 

Author Comment

by:On_The_Level
ID: 16519938
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.
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16519961
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();
            }
    }
}
0
 

Author Comment

by:On_The_Level
ID: 16520055
man you have confused me to ****.

Sorry i am sort of a beginner at java, as u prob guessed
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16520066
what is confusing ?
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16520100
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)
0
 

Author Comment

by:On_The_Level
ID: 16523742
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;
}
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16523899
> 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 ?
0
 

Author Comment

by:On_The_Level
ID: 16523921
when i went to search through the array, if found no matching in a row.
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16524081
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
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16524103
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
0
 

Author Comment

by:On_The_Level
ID: 16524314
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;
   }
0
 

Author Comment

by:On_The_Level
ID: 16556692
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;

}


}
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question