public class Board { private char[][] board; private final int ROWS = 6; private final int COLS = 7; public Board() { board = new char[ROWS][COLS]; for(int i = 0; i < board.length; i++) { for(int j = 0; j < board[i].length; j++) { board[i][j] = ' '; } } } /** * * @param col - a number 0-6 * @param color * @return */ public boolean placeChecker(int col, char color) { if(board[0][col] != ' ') { //debug - remove System.out.println("Error: cannot place checker"); return false; } for(int i = ROWS-1; i >= 0; i--) { if(board[i][col] == ' ') { board[i][col] = color; return true; } } return false; } public boolean checkWin() { if(checkRow() == true) { return true; } return false; } public boolean checkRow() { for(int i = ROWS-1; i >= 0; i--) { char winner = board[i][COLS/2]; if(winner == ' ') { continue; } int countright = 0, countleft = 0; int j = 4; //loop to count to the right while(j < COLS && board[i][j] == winner) { j++; countright++; } j = 2; //loop to count to the left while(j >= 0 && board[i][j] == winner) { j--; countleft++; } if((countleft+countright) >= 3) { return true; } } return false; } public void display() { for(int i = 0; i < ROWS; i++) { for(int j = 0; j < COLS; j++) { System.out.print("|" + board[i][j] + "|"); } System.out.println(); } } public boolean isFull() { for(int i = 0; i < COLS; i++) { if(board[0][i] == ' ') { return false; } } return true; } public static void main(String[] args) { Board b = new Board(); b.placeChecker(3, 'R'); b.placeChecker(3, 'B'); b.placeChecker(4, 'R'); b.placeChecker(4, 'B'); b.placeChecker(5, 'R'); b.placeChecker(5, 'B'); //placeChecker(6, 'R'); //b.placeChecker(3, 'B'); b.display(); if(b.checkWin()) { System.out.println("There is a win"); } else { System.out.println("No win"); } } }