aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/jopek/pai_fillo_nai/Bot.java
diff options
context:
space:
mode:
authorMaksymilian Jopek <maks@jopek.eu>2022-12-07 22:54:17 +0100
committerMaksymilian Jopek <maks@jopek.eu>2022-12-07 22:54:17 +0100
commit82652521d71c7fb01762203d740b7c73068098c0 (patch)
treeede94d3c8a14138485157815d57116cc56536159 /app/src/main/java/com/jopek/pai_fillo_nai/Bot.java
parentd606f2e9708301b26903bfde6f7c945af790fdee (diff)
downloadpai-fillo-nai-82652521d71c7fb01762203d740b7c73068098c0.tar.gz
pai-fillo-nai-82652521d71c7fb01762203d740b7c73068098c0.tar.zst
pai-fillo-nai-82652521d71c7fb01762203d740b7c73068098c0.zip
v1.1.0
Diffstat (limited to 'app/src/main/java/com/jopek/pai_fillo_nai/Bot.java')
-rw-r--r--app/src/main/java/com/jopek/pai_fillo_nai/Bot.java98
1 files changed, 98 insertions, 0 deletions
diff --git a/app/src/main/java/com/jopek/pai_fillo_nai/Bot.java b/app/src/main/java/com/jopek/pai_fillo_nai/Bot.java
new file mode 100644
index 0000000..4b04a55
--- /dev/null
+++ b/app/src/main/java/com/jopek/pai_fillo_nai/Bot.java
@@ -0,0 +1,98 @@
+package com.jopek.pai_fillo_nai;
+
+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() == false)
+ 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(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;
+ }
+}