0

I am making a connect 4 game, and I am trying to check for a win, so far I have been able to check right diagonally anywhere on the game board, How do I now check diagonally left, horizontally and vertically, I tried reversing the direction to check on diagonally left but that did not work? What can I change to check towards the left? horizontal and vertical

//Column Size
private static final int COLS = 7; 
//Row Size
private static final int ROWS = 6; 
//Dynamic Array
private State [][]count = new State[COLS][ROWS];
//Length of Pattern to check FOUR counters in a row
public static final int LEN=4;


//trajectory
public State checkWinner() {
    for(int col=0; col<count.length;++col) {
        for(int row=0; row<count[col].length; ++row) {
            State result = checkWinner(col,row);
            if (result!=null) {
                return result;
            }
        }
    }
    return null;
}

public State checkWinner(int col, int row) {
    State cell = count[col][row];
    if (cell==null ||cell==State.BLANK) { return null; }

    // check Diagonally Right 
    if((col+LEN<=COLS) && (row+LEN<=ROWS)){
        boolean same = true;
        for(int i=1;i<LEN;++i) {
            if (count[col+i][row+i]!=cell) {
                same=false;
                break;                  
            }
        }
        if (same) {
            return cell;
        }   
    }
    return null;
}
user3299420
  • 21
  • 2
  • 6
  • 1
    You can take a look at: http://www.javaproblems.com/2013/01/creating-connect-four-game-in-java.html – Etienne Feb 12 '14 at 15:21
  • 1
    I would have your winning method accept an offset for rows and offset for right. Then you check for runs using those offsets. Like if you call it with (-1, 1) then at each step it will check 1 column up and 1 row to the right. – Cruncher Feb 12 '14 at 15:35
  • what would the algorithm look like and what position is this for? – user3299420 Feb 12 '14 at 15:38
  • You may be interested in [this](http://stackoverflow.com/a/20202083/953482) post, where I describe the simplest possible win-checking algorithm ;-) – Kevin Feb 12 '14 at 15:39
  • 1
    that is the worst possible way to check for a win – user3299420 Feb 12 '14 at 15:42
  • 1
    Possible duplicate of [Connect 4 check for a win algorithm](http://stackoverflow.com/questions/32770321/connect-4-check-for-a-win-algorithm) – Krease Mar 28 '16 at 19:56

2 Answers2

0

I can recommend a much simpler approach. There are only a small number of possible winning patterns: 24 horizontally, 21 vertically, and 12 diagonally both ways for a total of 69 possibilities.

If you represent red and black as two separate bit fields, each has a total of 42 bits (present or not) which can be stored in an int. Create 69 bitmasks, one for each winning pattern, then AND each mask against the board position for red (or black) which is an int.

Tyler Durden
  • 11,156
  • 9
  • 64
  • 126
0

Your loop in checkWinner(int col, int row) is only checking three positions; it should start at i = 0 rather than i = 1. To check vertically use count[col][row+i], to check horizontally use count[col+i][row]. Be sure to modify the if statement prior to the loop when checking in the reverse direction.

vap
  • 151
  • 1
  • 2
  • 9
  • what do I modify it to? and what do you mean its only checking 3 positions? which one is the reverse direction? do you mean diagonally to the left? – user3299420 Feb 12 '14 at 17:09
  • It is connect four, so you need four matches correct? If the loop starts at 1 and goes to i – vap Feb 12 '14 at 19:49
  • For your if statement prior to the loop on the reverse diagonal check, you could use if((col-LEN >=0) && (row-LEN>=0)). Then your loop would be for (i = LEN-1; i>=0; i--) – vap Feb 12 '14 at 19:55
  • Instead try for(i=0; i – vap Feb 13 '14 at 00:25
  • I'm assuming col 0 row 0 is the bottom left corner. After consideration you should only need to check the diagonals right up and right down. You have the right up case. For the loop use for(i=0; i=0))... count[col+i][row-i]. – vap Feb 13 '14 at 00:46
  • still does not reverse to the left – user3299420 Feb 13 '14 at 12:29
  • But you shouldn't need to check the reverse, just work left to right. And for verticals, bottom to top. And for the diagonals use what I described, that should cover all possible wins. – vap Feb 13 '14 at 23:35
  • yeah that is working but its doing 5 in a row not 4, how do i change that? – user3299420 Feb 14 '14 at 00:20
  • Are you using for(i=o; i – vap Feb 15 '14 at 03:08