import java.util.Scanner; /** * TicTacToe game class * * @author Devin Gray * @date 4/1/2016 **/ public class TicTacToe { private char[][] board; private int size; private char whosTurn; private static final char X = 'X'; private static final char O = 'O'; private static final char EMPTY = ' '; private String result = ""; public TicTacToe(int size) { this.size = size; board = new char[size][size]; initialize(); whosTurn = X; //for fun, lets put an initial X in the middle //board[size/2][size/2] = X; } private void initialize() { for(int row=0; row < board.length; row++) { for(int col=0; col < board[row].length; col++) { board[row][col] = EMPTY; } } } public String toString() { String str = ""; for(int row=0; row < board.length; row++) { //print out the values in each column for(int col=0; col < board[row].length; col++) { str += board[row][col]; if(col != board[row].length - 1) str += "|"; } str += "\n"; //draw separator if(row != board.length - 1) { for(int col=0; col < board[row].length; col++) { str += "—"; if(col != board[row].length - 1) str += "+"; } str += "\n"; } } return str; } public boolean gameOver() { if(horizontalLineExist()) { return true; } if(verticalLineExists()) { return true; } if(diagonalLineExists()) { return true; } if(isFull()) { result = "Its a Cat Game!"; return true; } return false; } public boolean isFull() { boolean isCat = true; for(int row=0; row < board.length; row++) { for(int col=0; col < board[row].length; col++) { if(board[row][col] == EMPTY) { isCat = false; } } } return isCat; } public boolean horizontalLineExist() { int numInRow = 0; char testing = EMPTY; for(int row=0; row < board.length; row++) { numInRow = 0; for(int col=0; col < board[row].length; col++) { //inspect it if(board[row][col] != EMPTY && (col == 0 || testing == board[row][col])) { if(col == 0) testing = board[row][col]; numInRow++; } else { break; } } if(numInRow == size) { result = "WINNER! " + testing + " has " + size + " in a row"; return true; } } return false; } public boolean verticalLineExists() { int numInCol = 0; char testing = EMPTY; for(int col=0; col < size; col++) { numInCol = 0; for(int row=0; row < size; row++) { //inspect it if(board[row][col] != EMPTY && (row == 0 || testing == board[row][col])) { if(row == 0) testing = board[row][col]; numInCol++; } else { break; } } if(numInCol == size) { result = "WINNER! " + testing + " has " + size + " in a column"; return true; } } return false; } public boolean diagonalLineExists() { int numInDiagonal = 0; char testing = EMPTY; //only 2 cases // - top left to bottom right // - bottom left to top right for(int i=0; i < size; i++) { int row = i; int col = i; if(board[row][col] != EMPTY && (i == 0 || testing == board[row][col])) { if(i == 0) testing = board[row][col]; numInDiagonal++; } else { break; } } if(numInDiagonal == size) { result = "WINNER! " + testing + " has " + size + " in a diagonal"; return true; } numInDiagonal = 0; for(int i=0; i < size; i++) { int row = (size-1) - i; int col = i; if(board[row][col] != EMPTY && (i == 0 || testing == board[row][col])) { if(i == 0) testing = board[row][col]; numInDiagonal++; } else { break; } } if(numInDiagonal == size) { result = "WINNER! " + testing + " has " + size + " in a diagonal"; return true; } return false; } public void makeMove() { //whos turn? int row = -1; int col = -1; //ask for move boolean validMove = false; while(!validMove) { Scanner in = new Scanner(System.in); System.out.println("Ok " + whosTurn + " tell me the row: "); row = in.nextInt(); System.out.println("Ok " + whosTurn + " tell me the col: "); col = in.nextInt(); //validation boolean validRow = false; if(row <= board.length && row > 0) { validRow = true; } boolean validCol = false; if(validRow) { if(col <= board[row-1].length && col > 0) { validCol = true; } } if(validRow && validCol) { if(board[row-1][col-1] == EMPTY) { validMove = true; } else { System.out.println("That square is already occupied by " + board[row-1][col-1]); } } else { if(!validRow) { System.out.println("Hey bro.... invalid row"); } else if(!validCol) { System.out.println("Hey man.... invalid col"); } } } //update board board[row-1][col-1] = whosTurn; //flip whos turn it is whosTurn = (whosTurn == X) ? O : X; } public String result() { return result; } }