import java.util.Scanner; /** * Grid class * * Layout of Grid [0] [1] [2] ... [0] [1] [2] * */ public class Grid { private static final int ROWS = 6; private static final int COLS = 7; private char board[][]; /** * METHOD 1 construct the game grid initialize each cell to the "" */ public Grid() { //2D array to use as grid board = new char[ROWS][COLS]; // For Loops that assign an * to each array cell for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { board[i][j] = '*'; } } } /** * METHOD 2 check for fullBoard * * @return true if board is full, false otherwise * */ private boolean fullBoard() { //variable to hold value for a full board boolean full = false; for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { if (board[i][j] != '*') { full = true; } } } return full; } /** * METHOD 3 * * @param col * @return true if col is on the grid */ private boolean isColValid(int col) { boolean valid = false; return valid; } /** * METHOD 4 (10%) * * @param column * @return "first" unoccupied cell in column return -1 if there is no * unoccupied cell in this row */ private int firstEmpty(int column) { return -1; } /** * METHOD 5 (15%) display the board */ private void showBoard() { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { System.out.print(board[i][j] + " "); } System.out.println(""); } } /** * METHOD 6 check board for a win * * @return true if there is a row, col, or diagonal of 4 matching colors * false otherwise */ private boolean checkForWin() { boolean win = false; //Check by row for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length - 4; j++) { if (board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j + 2] && board[i][j + 2] == board[i][j + 3]) { while (board[i][j] != '*') { win = true; } } } } //Check by column for (int i = 0; i < COLS; i++) { for (int j = 0; j <= ROWS - 3; j++) { if (board[i][j] == board[i + 1][j] && board[i + 1][j] == board[i + 2][j] && board[i + 2][j] == board[i + 3][j]) { while (board[i][j] != '*') { win = true; } } } } // Check Diagonally bottom left to top right return win; } /** * GIVEN * * @return true if there is a win or if the board is full */ private boolean gameOver() { //game is over if there is a win or the board is full return this.checkForWin() || fullBoard(); } /** * GIVEN * * @param column * @param color place color in first unoccupied row in this column (update * cell from '*' to color) * @return true if the play is successful, false if color cannot be placed * in this column */ private boolean play(int column, char color) { //if the column is not valid, return false if (!this.isColValid(column)) { return false; } //find the first empty row in this column int row = firstEmpty(column); //see if column is full if (row < 0) { return false; } //else, set the cell to this color board[row][column] = color; return true; } /** * GIVEN driver method that controls game play * * @return when game is over, i.e. if win or if board is full (no "" in * cells) * */ public void playGame() { do { //show the board this.showBoard(); //ask user for a column and color System.out.print("Enter column (0 - 6) and " + "color (red or black): "); Scanner kybd = new Scanner(System.in); int col = kybd.nextInt(); String color = kybd.next(); //play it this.play(col, color.charAt(0)); } while (!this.gameOver()); } }