0

I'm making a game of connect four and I'm trying to use for loops to check for four matching pieces in a row vertically, horizontally or diagonally. (Edited to show more code)

public class ConnectFour {

/** Number of columns on the board. */
public static final int COLUMNS = 7;

/** Number of rows on the board. */
public static final int ROWS = 6;

/** Color for computer player's pieces */
public static final Color COMPUTER = Color.BLACK;

/** Color for human player's pieces */
public static final Color HUMAN = Color.RED;

/** Color for blank spaces. */
public static final Color NONE = Color.WHITE;


/**
 * Drops a piece of given Color in column.  Modifies the board
 * array. Assumes the move is legal.
 *
 * @param board The game board.
 * @param color Color of piece to drop.
 * @param column Column in which to drop the piece.
 */
public static void drop(Color[][] board, Color color, int column) {
    int row = 0;
    while (column < COLUMNS && row < ROWS){
        for(int i = row; i < ROWS; i++){
            if (board[row][column] != NONE){
                row = i;
            }
        }

        if (color == HUMAN){
            board[row][column] = HUMAN;
            break;
        } else
            board[row][column] = COMPUTER;
            break;
    }   
}


public static Color winner(Color[][] board) {
    Color win = NONE;
    int diagCount = 1;
    int horCount = 1;
    int verCount = 1;
    // horizontal win check
    for (int row = 0; row < ROWS; row++){
        for (int col = 0; col < COLUMNS - 1; col++){
            if (board[row][col] == board[row][col + 1]){
                horCount++;
                if (horCount == 4){
                    win = board[row][col];
                    return win;
                }
            } else {
                horCount = 1;
            }
        }
    }

    // vertical win check
    for (int col = 0; col < COLUMNS; col++){
        for (int row = 0; row < ROWS - 1; row++){
            if (board[row][col] == board[row + 1][col]){
                verCount++;
                if (verCount == 4){
                    win = board[row][col];
                    return win;
                }
            } else {
                verCount = 1;
            }
        }
    }

I've made the horizontal win check and vertical win check loops but it only goes through whichever loop I put first (in this case the horizontal win check). When I comment the horizontal win check out to test the vertical win check it only recognizes a win in the first column. I've been looking at my code forever and can't figure out what I've done wrong... any help is appreciated!

(also, board[0][0] would be the bottom left corner of the board and board[5][6] is the top right)

jbl94
  • 1
  • 1
  • `board` is an array of what type? – bpgeck Oct 26 '15 at 20:08
  • It's an array of colors, the colors are none(white), human(red) and computer(black) – jbl94 Oct 26 '15 at 20:14
  • 2
    Okay you should be comparing with `.equals` then rather than `==`. I believe you are currently checking if the two Colors are instances of each other rather than if they are equal. See here: http://stackoverflow.com/a/5761156/5087118 – bpgeck Oct 26 '15 at 20:20
  • If you posted your code for none, human, and computer, it would be a little easier for me to understand – bpgeck Oct 26 '15 at 20:27
  • Thanks! I changed the "==" to ".equals" and it's still doing the same thing so i'm not sure what's going wrong. I added a bit more code to the post as well. – jbl94 Oct 26 '15 at 20:49
  • Sorry for the late reply, I've been messing with your code. How do you go about checking whether someone won or not? Because your win checks actually work fine for all my tests – bpgeck Oct 26 '15 at 21:20
  • You can see my testing code here: https://ideone.com/n5qW87. A bit lengthy but basically it fills the board using your drop method which I did not edit at all. Then it checks to see if there is a winner with your `winner` that I edited by returning `null` if there is no winner. Then it clears the board, and fills it again with another test case, rechecks, etc. The only possible problem I could come up with for your code at this point is that you may handle the return value of winner properly – bpgeck Oct 26 '15 at 21:35

0 Answers0