HoangHai-portliofio-projects / Projects / 5.Tictactoe_java / src / TicTacToeApplication.java
TicTacToeApplication.java
Raw
import java.util.Scanner;

public class TicTacToeApplication {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean wantToPlay = true;
        while (wantToPlay) {
            //setting up the token and Ai
            System.out.println("Welcome to TicTacToe");
            System.out.println("Enter your Marker: ");
            char playerToken = scan.next().charAt(0);
            System.out.println("Enter Ai marker: ");
            char aiToken = scan.next().charAt(0);

            TicTacToe game = new TicTacToe(playerToken, aiToken);
            AI ai = new AI();
            
            //Set up the game
            TicTacToe.printBoardIndex();
            TicTacToe.printBoard(game.board);

            while (game.gameOver().equals("notOver")) {
                if (game.currentMarker == game.userMarker) {
                    System.out.println("your turn, enter an address : ");
                    int spot = scan.nextInt();
                    while (!game.playTurn(spot)) {
                        System.out.println("try again! you enter an invalid spot... ");
                        spot = scan.nextInt();
                    }
                    System.out.println("You picked "+ spot);
                } else {
                    //AI turn
                    System.out.println("My Turn muahahaha ");
                    int aiSpot = ai.pickSpot(game);
                    game.playTurn(aiSpot);
                    System.out.println("I picked " + aiSpot);
                }
                TicTacToe.printBoard(game.board);
            }

            System.out.println(game.gameOver());
            System.out.println();
            //Set up new game and End the loop Escape:
            System.out.println("Want another game??  Y or N");
            char response = scan.next().charAt(0);
            wantToPlay = (response == 'Y');
            System.out.println(); 
        }
        scan.close();
    }
}