1

I am making a game four in a row, where the number of rows and colomns is determined by the player(so can be anything, as long as more than four). So users input is stored in the variables "rows" and "column". The field looks smth like this, so the first row is always numbers.

I have trouble with the search algorithm for the winner. While my algorithm for the horizontal search works fine, the vertical one with the same logic gives me the out of bound error.If you could please help me to spot the mistake, I would be very grateful. Thank you! 1 2 3 4 5 |-|-|-|-| |-|-|-|-| |-|-|-|-|

public static String checkWinner(String [][]field){
//horizontally, which works
   for(int i=1; i<=rows; i++){
       for (int j=0; j<=column-1;j++){
           if (((j>=3 || j==column-1) && field[i][j]!="|_" && field[i][j]==field[i][j-1] && field[i][j]==field[i][j-2] && field[i][j]==field[i][j-3])
           || (field[i][j]!="|_" && field[i][j]==field[i][j+1] && field[i][j]==field[i][j+2] && field[i][j]==field[i][j+3]))
           {
               return field[i][j];
           }
       }
    }


//vertically which doesn't work

   for(int i=0; i<column; i++){
      for (int j=1; j<=rows-1;j++){
         if (((j>=4 || j==rows-1) && field[j][i]!="|_" && field[j][i]==field[j-1][i] && field[j][i]==field[j-2][i] && field[j][i]==field[j-3][i])
         || (field[j][i]!="|_" && field[j][i]==field[j+1][i] && field[j][i]==field[j+2][i] && field[j][i]==field[j+3][i]))
         {
            return field[i][j];
         }
      }
   }
   return null;
}
MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
javaniewbie
  • 23
  • 1
  • 1
  • 5

1 Answers1

1

In your Horizontally loop logic you are running your loop for column from j=0 to column-1
but then you are accessing field[i][j] with values like j+1, j+2, j+3 which will go beyond column

for example : user enters column value as 5 but your logic of j+2 at j=4 & i=0 will make it to access 6th column (field[0][6]) which is obviously OutOfBound.

check for same error in Vertically Loop logic for rows value

and as NOTE: compare strings using equals() not using equality operators like ==

Community
  • 1
  • 1
exexzian
  • 7,782
  • 6
  • 41
  • 52
  • Thank you for your answer. But that horizontal check is working, actually. Rows and Column are inputs from the user as for how big he wants his field to be. But later I make a matrix using the indexes starting 0 till column-1. – javaniewbie Dec 15 '13 at 21:26
  • @javaniewbie your intention is right but you will have to prevent your logic from checking a location that doesn't exists - by the way are you coding any game – exexzian Dec 15 '13 at 21:29
  • Thats what I stilldon't understand cause I can't see where I am checking the location that doesn't exist and how come the horizontal works and the vertical not. ..Yeah, I am working on the connect four game. – javaniewbie Dec 15 '13 at 21:31
  • @javaniewbie that's what I explain in my example like here `field[i][j]==field[i][j+2]` and then relate it with my exmaple – exexzian Dec 15 '13 at 21:33
  • Thank you sansix, I will keep trying. – javaniewbie Dec 15 '13 at 21:37
  • @javaniewbie welcome :) and best of luck with the `game` – exexzian Dec 15 '13 at 21:38