diff --git a/src/main/java/App.java b/src/main/java/App.java index 4f6ffd2..f54be61 100644 --- a/src/main/java/App.java +++ b/src/main/java/App.java @@ -2,89 +2,129 @@ public class App { + private static final char PLAYER_SYMBOL = 'X'; + private static final char COMPUTER_SYMBOL = 'O'; + private static final int BOARD_SIZE = 9; + public static void main(String[] args) { - Scanner scan = new Scanner(System.in); - byte input; - byte rand; - byte i; - boolean boxAvailable = false; - byte winner = 0; - char box[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' }; - System.out.println("Enter box number to select. Enjoy!\n"); - - boolean boxEmpty = false; + Scanner scanner = new Scanner(System.in); + char[] board = initializeBoard(); + int winner = 0; + + System.out.println("Welcome to Tic-Tac-Toe!\nEnter a number (1-9) to place your symbol.\n"); + while (true) { - System.out.println("\n\n " + box[0] + " | " + box[1] + " | " + box[2] + " "); - System.out.println("-----------"); - System.out.println(" " + box[3] + " | " + box[4] + " | " + box[5] + " "); - System.out.println("-----------"); - System.out.println(" " + box[6] + " | " + box[7] + " | " + box[8] + " \n"); - if(!boxEmpty){ - for(i = 0; i < 9; i++) - box[i] = ' '; - boxEmpty = true; - } + printBoard(board); - if(winner == 1){ - System.out.println("You won the game!\nCreated by Shreyas Saha. Thanks for playing!"); - break; - } else if(winner == 2){ - System.out.println("You lost the game!\nCreated by Shreyas Saha. Thanks for playing!"); - break; - } else if(winner == 3){ - System.out.println("It's a draw!\nCreated by Shreyas Saha. Thanks for playing!"); + if (winner != 0) { + printResult(winner); break; } - while (true) { - input = scan.nextByte(); - if (input > 0 && input < 10) { - if (box[input - 1] == 'X' || box[input - 1] == 'O') - System.out.println("That one is already in use. Enter another."); - else { - box[input - 1] = 'X'; - break; - } - } - else - System.out.println("Invalid input. Enter again."); + int playerMove = getPlayerMove(scanner, board); + board[playerMove] = PLAYER_SYMBOL; + + if (checkWinner(board, PLAYER_SYMBOL)) { + winner = 1; + continue; } - if((box[0]=='X' && box[1]=='X' && box[2]=='X') || (box[3]=='X' && box[4]=='X' && box[5]=='X') || (box[6]=='X' && box[7]=='X' && box[8]=='X') || - (box[0]=='X' && box[3]=='X' && box[6]=='X') || (box[1]=='X' && box[4]=='X' && box[7]=='X') || (box[2]=='X' && box[5]=='X' && box[8]=='X') || - (box[0]=='X' && box[4]=='X' && box[8]=='X') || (box[2]=='X' && box[4]=='X' && box[6]=='X')){ - winner = 1; - continue; + if (isDraw(board)) { + winner = 3; + continue; } - boxAvailable = false; - for(i=0; i<9; i++){ - if(box[i] != 'X' && box[i] != 'O'){ - boxAvailable = true; - break; - } + int computerMove = getComputerMove(board); + board[computerMove] = COMPUTER_SYMBOL; + + if (checkWinner(board, COMPUTER_SYMBOL)) { + winner = 2; } - if(boxAvailable == false){ + if (isDraw(board)) { winner = 3; - continue; } + } - while (true) { - rand = (byte) (Math.random() * (9 - 1 + 1) + 1); - if (box[rand - 1] != 'X' && box[rand - 1] != 'O') { - box[rand - 1] = 'O'; - break; + scanner.close(); + } + + private static char[] initializeBoard() { + char[] board = new char[BOARD_SIZE]; + for (int i = 0; i < BOARD_SIZE; i++) { + board[i] = ' '; + } + return board; + } + + private static void printBoard(char[] board) { + System.out.println("\n"); + System.out.println(" " + board[0] + " | " + board[1] + " | " + board[2]); + System.out.println("-----------"); + System.out.println(" " + board[3] + " | " + board[4] + " | " + board[5]); + System.out.println("-----------"); + System.out.println(" " + board[6] + " | " + board[7] + " | " + board[8]); + System.out.println(); + } + + private static int getPlayerMove(Scanner scanner, char[] board) { + int move; + while (true) { + System.out.print("Your move (1-9): "); + if (scanner.hasNextInt()) { + move = scanner.nextInt() - 1; + if (move >= 0 && move < BOARD_SIZE) { + if (board[move] == ' ') { + return move; + } else { + System.out.println("Cell already taken. Try another."); + } + } else { + System.out.println("Please enter a number between 1 and 9."); } + } else { + System.out.println("Invalid input. Enter a number."); + scanner.next(); // clear non-integer input } + } + } - if((box[0]=='O' && box[1]=='O' && box[2]=='O') || (box[3]=='O' && box[4]=='O' && box[5]=='O') || (box[6]=='O' && box[7]=='O' && box[8]=='O') || - (box[0]=='O' && box[3]=='O' && box[6]=='O') || (box[1]=='O' && box[4]=='O' && box[7]=='O') || (box[2]=='O' && box[5]=='O' && box[8]=='O') || - (box[0]=='O' && box[4]=='O' && box[8]=='O') || (box[2]=='O' && box[4]=='O' && box[6]=='O')){ - winner = 2; - continue; + private static int getComputerMove(char[] board) { + int move; + while (true) { + move = (int) (Math.random() * BOARD_SIZE); + if (board[move] == ' ') { + return move; } } + } + private static boolean checkWinner(char[] board, char symbol) { + return (board[0] == symbol && board[1] == symbol && board[2] == symbol) || + (board[3] == symbol && board[4] == symbol && board[5] == symbol) || + (board[6] == symbol && board[7] == symbol && board[8] == symbol) || + (board[0] == symbol && board[3] == symbol && board[6] == symbol) || + (board[1] == symbol && board[4] == symbol && board[7] == symbol) || + (board[2] == symbol && board[5] == symbol && board[8] == symbol) || + (board[0] == symbol && board[4] == symbol && board[8] == symbol) || + (board[2] == symbol && board[4] == symbol && board[6] == symbol); + } + + private static boolean isDraw(char[] board) { + for (char c : board) { + if (c == ' ') { + return false; + } + } + return true; + } + + private static void printResult(int winner) { + switch (winner) { + case 1 -> System.out.println("🎉 You won the game!"); + case 2 -> System.out.println("😞 You lost the game!"); + case 3 -> System.out.println("🤝 It's a draw!"); + } + System.out.println("Created by Shreyas Saha. Thanks for playing!"); } -} \ No newline at end of file +}