0

Iam writing on a connect four game. I have implemented a check for horizontal and vertical.

Now I am stuck at the diagonal check, I am not sure how to do it. Here are my check functions:

 public boolean checkWinRows() {
    int sameDiscs;
    Player lastPlayerField = gameField[0][0].getPlayer();

    for(int i = 0; i < gameField.length; i++) {

        sameDiscs = 0;
        for(int j = 0; j < gameField[i].length; j++) {

            sameDiscs = countSameDiscs(sameDiscs, gameField[i][j].getPlayer() == lastPlayerField, gameField[i][j]);
            lastPlayerField = gameField[i][j].getPlayer();

            if(sameDiscs == 4) {
                // set winner
                lastPlayerField.setWon(true);
                return true;
            }

        }
    }

    return false;
}

public boolean checkWinCols() {
    int sameDiscs;
    Player lastPlayerField = gameField[0][0].getPlayer();

    for(int j = 0; j < gameField[0].length; j++) {
        sameDiscs = 0;
        for(int i = 0; i < gameField.length; i++) {

            sameDiscs = countSameDiscs(sameDiscs, gameField[i][j].getPlayer() == lastPlayerField, gameField[i][j]);
            lastPlayerField = gameField[i][j].getPlayer();

            if(sameDiscs == 4) {
                // set winner
                lastPlayerField.setWon(true);
                return true;
            }
        }
    }

    return false;
}
    private int countSameDiscs(int sameDiscs, boolean samePlayer, Field field) {
    if(field.getPlayer() != null) {
        if(samePlayer)
            sameDiscs ++;
        else
            sameDiscs = 1;
    } else
        sameDiscs = 0;
    return sameDiscs;
}

Now I am not sure how to do the diagonal check. I know I can go through all cols and rows, but how can I count the tokens which are diagonally placed?

Greetings

EDIT:

Here are my two diagonal check functions:

public boolean checkWinDiagonal1(int row, int column) {
    int i=0, j=0;
    int iterateCount= 4;
     int sameDiscs;
     Player lastPlayerField = gameField[0][0].getPlayer();
      for( i=row, j=column; iterateCount>0; i++, j--, iterateCount--) {
          sameDiscs = 0;
          sameDiscs = countSameDiscs(sameDiscs, gameField[i][j].getPlayer() == lastPlayerField, gameField[i][j]);
          lastPlayerField = gameField[i][j].getPlayer();

          if(sameDiscs == 4) {
              // set winner
              lastPlayerField.setWon(true);
              return true;
          }

      }
    return false;
}

public boolean checkWinDiagonal2(int row, int column) {
    int i=0, j=0;
    int iterateCount= 4;
    int sameDiscs;
    Player lastPlayerField = gameField[0][0].getPlayer();
      for( i=row, j=column; iterateCount>0; i++, j++, iterateCount--) {
          sameDiscs = 0;
          sameDiscs = countSameDiscs(sameDiscs, gameField[i][j].getPlayer() == lastPlayerField, gameField[i][j]);
          lastPlayerField = gameField[i][j].getPlayer();

          if(sameDiscs == 4) {
              // set winner
              lastPlayerField.setWon(true);
              return true;
          }
      }
   return false;

}

I get this error message: Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException

potu1304
  • 65
  • 1
  • 15

1 Answers1

0

Connect 4 game is a 7*6 matrix and hence we can solve it as follows

Matrix Entries

00 01 02 03 04 05 06

10 11 12 13 14 15 16

20 21 22 23 24 25 26

30 31 32 33 34 35 36

40 41 42 43 44 45 46

50 51 52 53 54 55 56

Matches for connect 4 in the diagonals can happen in forward and backward diagonals.

Forward Diagonals:

Diagonals which are following forward slash "/" are named here as forward diagonals.

Sample matches 03, 12, 21, 30 or 23, 32, 41, 50 or 26, 35, 44, 53 and so on

Let us come up with an array with the starting row and column entries of forward diagonals

It should be 03, 04, 05, 06, 16, 26.

In this array I have added the count, so that it will be easy to iterate and find the match

Forward Diagonals :

[ (0,3,4), (0,4,5), (0,5,6), (0,6,6), (1,6,5), (2,6,4)] read as row, column and count

Iterate the forward diagonals array and invoke forwardDiagonals method

public boolean forwardDiagonals(int row, int column, int iterateCount){
  int i=0, j=0;
  for( i=row, j=column; iterateCount>0; i++, j--, iterateCount--) {

    // Matching logic goes here

  }
}

Backward Diagonals

Diagonals which are following backward slash "\" are named here as backward diagonals.

Sample matches 03, 14, 25, 36 or 20, 31, 42, 53 or 11, 22, 33, 44 and so on

Let us come up with an array with the starting row and column entries of backward diagonals

It should be 20, 10, 00, 01, 02, 03.

Same approach is followed for this array and we have the iterative count, so that it will be easy to iterate and find the match

Backward Diagonals :

[ (2,0,4), (1,0,5), (0,0,6), (0,1,6), (0,2,5), (0,3,4)] 
read as row, column and count

Iterate the backward diagonals array and invoke backwardDiagonals method

public boolean backwardDiagonals(int row, int column, int iterateCount){
  int i=0, j=0;
  for( i=row, j=column; iterateCount>0; i++, j++, iterateCount--) {

    // Matching logic goes here

  }
}
Clement Amarnath
  • 4,779
  • 1
  • 16
  • 29
  • I tried to do so, but it doesnt work, it gives an ArrayIndexOutOfBoundsException. How can I adapt it for my code? And what iterateCount should I choose? – potu1304 May 27 '16 at 14:09
  • I have given you the arrays with forward and backward diagonal with the starting row, starting column and the iteration count. I have highlighted the same in the answer, please have a look into that. – Clement Amarnath May 30 '16 at 05:00