/** TicTacToe @author John Doe Period 3 */ import java.util.Scanner; public class TicTacToe { public static void main(String[] args) { String[][] board = new String[3][3]; // tic tac toe board boolean gameOver = false; // flag variable determining whether current game is over or not boolean validMove = false; // flag variable determining whether move is valid or not Scanner keyboard = new Scanner(System.in); // user input thru keyboard final String COMPUTER_SYMBOL = "o"; // computer's game piece final String PLAYER_SYMBOL = "x"; // player's game piece // initializing board with blank spaces for (int row = 0; row < board.length; row++) { for (int col = 0; col < board[row].length; col++) { board[row][col] = " "; } } while (!gameOver) { // ************************ player's move ************************** validMove = false; while (!validMove) { System.out.print("Enter row of next move (0 - 2): "); int nextMoveRow = keyboard.nextInt(); System.out.print("Enter column of next move (0 - 2): "); int nextMoveColumn = keyboard.nextInt(); if (checkValidMove(board, nextMoveRow, nextMoveColumn)) { board[nextMoveRow][nextMoveColumn] = PLAYER_SYMBOL; validMove = true; } else { System.out.println("Invalid move!"); System.out.println(displayBoard(board)); } } // ************************ computer's move ************************ // TODO: make random but valid computer move or add AI System.out.println(displayBoard(board)); } }// end of main public static boolean checkValidMove(String[][] board, int row, int col) { if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col].equals(" ")) { return true; } return false; }// end of checkValidMove public static String displayBoard(String[][] board) { String str = ""; // board representation for (int row = 0; row < 3; row++) { str += "|"; for (int col = 0; col < 3; col++) { str += board[row][col] + " "; } str += "|\n"; } return str; }// end of displayBoard }