aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/jopek/pai_fillo_nai/Bot.java
blob: 523da7bbbbc769e3d0e6bb97947df6e5309fe662 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.jopek.pai_fillo_nai;

import android.os.Build;

import java.util.Arrays;

public class Bot {
    static int meTheBot, opponent;
    static int[] board;

    static boolean isntBoardFull() {
        for (int j : board)
            if (j == -1)
                return true;
        return false;
    }

    static int goodOrBad() {
        int[][] possibilities = new int[][]{
                {0, 1, 2}, {3, 4, 5}, {6, 7, 8},
                {0, 3, 6}, {1, 4, 7}, {2, 5, 8},
                {0, 4, 8}, {2, 4, 6}
        };
        for (int[] poss : possibilities) {
            if (chkTriEqOnGB(poss[0], poss[1], poss[2])) {
                if (board[poss[0]] == meTheBot) return +10;
                else return -10;
            }
        }

        return 0;
    }

    static int minimax(int depth, Boolean isMax) {
        int score = goodOrBad();

        if (score == 10)
            return score;

        if (score == -10)
            return score;

        if (!isntBoardFull())
            return 0;

        int best;
        if (isMax) {
            best = -1000;

            for (int i = 0; i < board.length; i++) {
                if (board[i] == -1) {
                    board[i] = meTheBot;
                    best = Math.max(best, minimax(depth + 1, !isMax));
                    board[i] = -1;
                }
            }
        } else {
            best = 1000;

            for (int i = 0; i < board.length; i++) {
                if (board[i] == -1) {
                    board[i] = opponent;
                    best = Math.min(best, minimax(depth + 1, !isMax));
                    board[i] = -1;
                }
            }
        }
        return best;
    }

    static int getBestMove(int[] board, int bot, int opp) {
        Bot.board = board;
        Bot.meTheBot = bot;
        Bot.opponent = opp;
        int bestVal = -1000;
        int bestMove = -1;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            if(Arrays.stream(board).filter(c -> c == -1).count() == 9) return 4;
        }

        for (int i = 0; i < board.length; i++) {
            if (board[i] == -1) {
                board[i] = meTheBot;
                int moveVal = minimax(0, false);
                board[i] = -1;

                if (moveVal > bestVal) {
                    bestMove = i;
                    bestVal = moveVal;
                }
            }
        }

        return bestMove;
    }

    // CheckTripleEqualityOnGameBoard
    public static boolean chkTriEqOnGB(int a, int b, int c) {
        return board[a] == board[b] && board[b] == board[c] && board[a] != -1;
    }
}