minmax algorithm implementation in java

Solutions on MaxInterview for minmax algorithm implementation in java by the best coders in the world

showing results for - "minmax algorithm implementation in java"
Olivia
26 Jun 2017
1class Board {
2
3    List<Point> availablePoints;
4    Scanner scan = new Scanner(System.in);
5    int[][] board = new int[3][3];
6
7    public Board() {
8    }
9
10    public boolean isGameOver() {
11        return (hasXWon() || hasOWon() || getAvailableStates().isEmpty());
12    }
13
14    public boolean hasXWon() {
15        if ((board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] == 1) || (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] == 1)) {
16            return true;
17        }
18        for (int i = 0; i < 3; ++i) {
19            if (((board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] == 1)
20                    || (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] == 1))) {
21                return true;
22            }
23        }
24        return false;
25    }
26
27    public boolean hasOWon() {
28        if ((board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] == 2) || (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] == 2)) {
29            return true;
30        }
31        for (int i = 0; i < 3; ++i) {
32            if ((board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] == 2)
33                    || (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] == 2)) {
34                return true;
35            }
36        }
37
38        return false;
39    }
40
41    public List<Point> getAvailableStates() {
42        availablePoints = new ArrayList<>();
43        for (int i = 0; i < 3; ++i) {
44            for (int j = 0; j < 3; ++j) {
45                if (board[i][j] == 0) {
46                    availablePoints.add(new Point(i, j));
47                }
48            }
49        }
50        return availablePoints;
51    }
52
53    public void placeAMove(Point point, int player) {
54        board[point.x][point.y] = player; //player = 1 for X, 2 for O
55    }
56
57    void takeHumanInput() {
58        System.out.println("Your move: ");
59        int x = scan.nextInt();
60        int y = scan.nextInt();
61        Point point = new Point(x, y);
62        placeAMove(point, 2);
63    }
64
65    public void displayBoard() {
66        System.out.println();
67
68        for (int i = 0; i < 3; ++i) {
69            for (int j = 0; j < 3; ++j) {
70                System.out.print(board[i][j] + " ");
71            }
72            System.out.println();
73
74        }
75    }
76
77    Point computersMove;
78
79    public int minimax(int depth, int turn) {
80        if (hasXWon()) return +1;
81        if (hasOWon()) return -1;
82
83        List<Point> pointsAvailable = getAvailableStates();
84        if (pointsAvailable.isEmpty()) return 0;
85
86        int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
87
88        for (int i = 0; i < pointsAvailable.size(); ++i) {
89            Point point = pointsAvailable.get(i);
90            if (turn == 1) {
91                placeAMove(point, 1);
92                int currentScore = minimax(depth + 1, 2);
93                max = Math.max(currentScore, max);
94
95                if(depth == 0)System.out.println("Score for position "+(i+1)+" = "+currentScore);
96                if(currentScore >= 0){ if(depth == 0) computersMove = point;}
97                if(currentScore == 1){board[point.x][point.y] = 0; break;}
98                if(i == pointsAvailable.size()-1 && max < 0){if(depth == 0)computersMove = point;}
99            } else if (turn == 2) {
100                placeAMove(point, 2);
101                int currentScore = minimax(depth + 1, 1);
102                min = Math.min(currentScore, min);
103                if(min == -1){board[point.x][point.y] = 0; break;}
104            }
105            board[point.x][point.y] = 0; //Reset this point
106        }
107        return turn == 1?max:min;
108    }
109}
110