HoangHai-portliofio-projects / Projects / 5.Tictactoe_java / src / AI.java
AI.java
Raw
import java.util.ArrayList;
import java.util.Random;

public class AI {
    public int pickSpot(TicTacToe game) {
        ArrayList<Integer> choice = new ArrayList();
        for (int i = 0; i<9; i++) {
            if (game.board[i] == '-') {
                choice.add(i+1);
            }
        }
        Random rand = new Random();
        int aiChoice = choice.get(Math.abs(rand.nextInt() % choice.size()));
        return aiChoice; 
    }
}