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)