Advertisement
Guest User

AIPlayer

a guest
Jan 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.13 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package game;
  7.  
  8. import java.util.Vector;
  9.  
  10. /**
  11.  *
  12.  * @author Mahy
  13.  */
  14. public class AIPlayer {
  15.  
  16.     private final int AI_PLAYER=Game.PLAYER_ONE;
  17.     private final int HUMAN_PLAYER=Game.PLAYER_TWO;
  18.     private GameLevel level;
  19.  
  20.     public AIPlayer(GameLevel level) {
  21. //        init(playersTurn);
  22.         this.level = level;
  23.     }
  24. // remove O's turn and set ai and human to final
  25. //    public void init(int playersTurn) {
  26. //        //intializa AI Player
  27. //        switch (playersTurn) {
  28. //            case 1:
  29. //                ai_player = 1;
  30. //                human_player = 2;
  31. //                break;
  32. //            case 2:
  33. //                ai_player = 2;
  34. //                human_player = 1;
  35. //                break;
  36. //
  37. //        }
  38. //    }
  39.  
  40.     public int performMove(Board b) {
  41.         int index;
  42.         if (level == GameLevel.EASY) {
  43.             index = findFirstEmptySlot(b);
  44.         } else {
  45.             if (b.isBoardEmpty()) {
  46.                 index = (int) (Math.random() * 9);
  47.             } else {
  48.                 index = getBestMove(b, AI_PLAYER).index;
  49.             }
  50.             b.setBoardSlot(getToken(AI_PLAYER), index);
  51.         }
  52.  
  53.         return index;
  54.     }
  55.  
  56.     private char getToken(int currentTurn) {
  57.         if (currentTurn == AI_PLAYER) {
  58.             return 'X';
  59.         } else {
  60.             return 'O';
  61.         }
  62.     }
  63.  
  64.     private int findFirstEmptySlot(Board b) {
  65.         boolean emptySlotFound = false;
  66.         int randomIndex;
  67.         do {
  68.             randomIndex = (int) (Math.random() * 9);
  69.             if (b.isSlotEmpty(randomIndex)) {
  70.                 b.setBoardSlot(getToken(AI_PLAYER), randomIndex);
  71.                 emptySlotFound = true;
  72.  
  73.             }
  74.         } while (!emptySlotFound);
  75.         return randomIndex;
  76.     }
  77.  
  78.     private class AIMove {
  79.  
  80.         public int index;
  81.         public int score;
  82.  
  83.         AIMove() {
  84.         }
  85.  
  86.         AIMove(int score) {
  87.             this.score = score;
  88.         }
  89.     }
  90.  
  91.     private int checkWinner(Board gameBoard, int currentTurn) {
  92.         char token = getToken(currentTurn);
  93. //        if (currentTurn == 1) {
  94. //            token = 'X';
  95. //        } else {
  96. //            token = 'O';
  97. //        }
  98.  
  99.         if ((gameBoard.getSlotToken(0) == token && gameBoard.getSlotToken(1) == token && gameBoard.getSlotToken(2) == token)) {
  100.             return currentTurn;
  101.         } else if ((gameBoard.getSlotToken(3) == token && gameBoard.getSlotToken(4) == token && gameBoard.getSlotToken(5) == token)) {
  102.             return currentTurn;
  103.         } else if ((gameBoard.getSlotToken(6) == token && gameBoard.getSlotToken(7) == token && gameBoard.getSlotToken(8) == token)) {
  104.             return currentTurn;
  105.         } else if ((gameBoard.getSlotToken(0) == token && gameBoard.getSlotToken(3) == token && gameBoard.getSlotToken(6) == token)) {
  106.             return currentTurn;
  107.         } else if ((gameBoard.getSlotToken(1) == token && gameBoard.getSlotToken(4) == token && gameBoard.getSlotToken(7) == token)) {
  108.             return currentTurn;
  109.         } else if (gameBoard.getSlotToken(2) == token && gameBoard.getSlotToken(5) == token && gameBoard.getSlotToken(8) == token) {
  110.             return currentTurn;
  111.         } else if (gameBoard.getSlotToken(0) == token && gameBoard.getSlotToken(4) == token && gameBoard.getSlotToken(8) == token) {
  112.             return currentTurn;
  113.         } else if (gameBoard.getSlotToken(2) == token && gameBoard.getSlotToken(4) == token && gameBoard.getSlotToken(6) == token) {
  114.             return currentTurn;
  115.         } else {
  116.             //check ties
  117.             if (gameBoard.isBoardFull()) {
  118.                 return 0;
  119.             }
  120.  
  121.         }
  122.         return -1;
  123.     }
  124.  
  125.     private AIMove getBestMove(Board b, int player) {
  126.         int rv = checkWinner(b, player);
  127.         switch (rv) {
  128.             case AI_PLAYER:
  129.                 //            System.out.println("10");
  130.                 return new AIMove(10);
  131.             case HUMAN_PLAYER:
  132.                 //            System.out.println("-10");
  133.                 return new AIMove(-10);
  134.             case 0:
  135.                 //            System.out.println("0");
  136.                 return new AIMove(0);
  137.             default:
  138.                 break;
  139.         }
  140.  
  141.         Vector<AIMove> movesList = new Vector<>();
  142.  
  143.         for (int i = 0; i < b.getSize(); i++) {
  144.             if (b.getSlotToken(i) == 0) {
  145. //                System.out.println("true");
  146.                 AIMove move = new AIMove();
  147.                 move.index = i;
  148.                 b.setBoardSlot(getToken(player), i);
  149. //                if (player == AI_PLAYER) {
  150. //                    b.setBoardSlot('X', i);
  151. //                } else {
  152. //                    b.setBoardSlot('O', i);
  153. //                }
  154.                 if (player == AI_PLAYER) {
  155.                     move.score = getBestMove(b, HUMAN_PLAYER).score;
  156.                 } else {
  157.                     move.score = getBestMove(b, AI_PLAYER).score;
  158.                 }
  159.                 movesList.add(move);
  160.                 b.clearSlot(i);
  161.             }
  162.         }
  163.         AIMove bestMove = new AIMove();
  164.         if (player == AI_PLAYER) {
  165.             int bestScore = -100000;
  166.             for (AIMove move : movesList) {
  167.                 if (move.score > bestScore) {
  168.                     bestMove = move;
  169.                     bestScore = move.score;
  170.                 }
  171.             }
  172.         } else {
  173.             int bestScore = 100000;
  174.             for (AIMove move : movesList) {
  175.                 if (move.score < bestScore) {
  176.                     bestMove = move;
  177.                     bestScore = move.score;
  178.                 }
  179.             }
  180.         }
  181.         return bestMove;
  182.     }
  183. }
  184. //        int randomIndex = (int) Math.random()*9;
  185. //        
  186. //        for (int i =0; i<b.getSize();i++)
  187. //        {
  188. //            if(b.isSlotEmpty(i))
  189. //            {
  190. //                b.setBoardSlot(getToken(), i);
  191. //                break;
  192. //            }
  193. //        }
  194.  
  195. //}
  196. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement