I'm having a hard time figuring out how to check for a win in my tic-tac-toe board. I have to check to see if the winner is X or O or check to see if there is no winner yet (return N) or the board is full and it is a tie (return F). The method in question is named checkForWin. It is the last included method included. I have included the whole program, but the only part that requires some addition is that last method, checkForWin. All else is okay (sufficiently).
package lab07; import java.util.*; public class Lab07 { /** * @param args the command line arguments Do not change main() */ public static void main(String[] args) { char[][] board; char win; char player; int size = getBoardSize(); while (size >= 3) { board = createEmptyBoard(size); player = getStartingPlayer(); do { getMove(board, player); player = player == 'O' ? 'X' : 'O'; // switch players win = checkForWin(board); } while (win == 'N'); // must be F, O or X to leave loop if (win == 'F') { System.out.printf("\nThe board is Full: no Winner\n"); } else { System.out.printf("\nThe winner is %c\n", win); } displayBoard(board); size = getBoardSize(); } // end play games loop } // end main /** * returns 3, 4 or 5 for the size of the play board or -1 to quit This is * complete, no changes necessary */ public static int getBoardSize() { int retval; Scanner keyboard = new Scanner(System.in); do { System.out.printf("Enter board size 3, 4, 5 or -1 to quit: "); retval = keyboard.nextInt(); } while (retval != 3 && retval != 4 && retval != 5 && retval != -1); keyboard.nextLine(); // you should know why by now return retval; // this MUST BE LAST LINE in this method() } // getBoardSize /** * randomly selects X or O as the starting player * */ public static char getStartingPlayer() { char retval = 'X'; int switchRan; Random ranGen = new Random(); switchRan = ranGen.nextInt(2); switch (switchRan) { case 0: retval = 'O'; break; case 1: retval = 'X'; break; } System.out.printf("\n\tNew Game. Starting player is %c\n", retval); return retval; // this MUST BE LAST LINE in this method() } // end getStartingPlayer /** * gets new memory for a board and fills all cells with a minus sign '-' * * @param size the board if 3 is 3 x 3, if 4 is 4 x 4 etc. */ public static char[][] createEmptyBoard(int size) { char[][] board = new char[size][size]; for (int row = 0; row < board.length; row++) { for (int column = 0; column < board[row].length; column++) { board[row][column] = '-'; } } return board; // this MUST BE LAST LINE in this method() } // end createEmptyBoard /** * 1. displays board and 2. gets row column indexes for next move 3. makes * sure the cell has a '-' is a legal move 4. loops till legal 5. puts the * player's X or O at that cell * * @param board the current play board * @param the X or O of the current player */ public static void getMove(char[][] board, char player) { Scanner keyboard = new Scanner(System.in); int row, col; System.out.printf("Player %c move\n", player); displayBoard(board); do { System.out.print("Enter row: >=0 and <=" + (board.length - 1) + " "); row = keyboard.nextInt(); System.out.print("Enter col: >=0 and <=" + (board.length - 1) + " "); col = keyboard.nextInt(); } while (board[row][col] != '-' && (board[row][col] != player));//CHECK THIS ONCE checkForWin IS COMPLETE board[row][col] = player; } // end getMove /** * displays the current play board to the console * * @param board the current play board to be displayed */ public static void displayBoard(char[][] board) { for (int row = 0; row < board.length; row++) { System.out.println(); for (int column = 0; column < board[row].length; column++) { System.out.printf("%c\t", board[row][column]); } } System.out.println(); } // end displayBoard /** * returns X, O, F or N returns X for X wins O for O wins F for board Full * no winner or N for No winner yet * * @param board, the current play board to be checked */ public static char checkForWin(char[][] board) { char retval = 'N'; int dashCount = board.length * board.length;//set total count of dashes to check for full board vs. open spaces int oCount, xCount; /** * Check for horizontal wins */ for (int row = 0; row < board.length; row++) { oCount = xCount = 0;//Reset X and O counters for (int col = 0; col < board[row].length; col++) { if (board[row][col] == 'X') { xCount++; } else if (board[row][col] == 'O') { oCount++; } else { dashCount--; } if (xCount == board.length) { retval = 'X'; } if (oCount == board.length) { retval = 'O'; } if (dashCount == 0) { retval = 'F'; } } } return retval; // this MUST BE LAST LINE in this method() } // end checkForWin }