Facebook
From Innocent Pig, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 242
  1. public Point minimax(int[][] gameState) {//player - true = MAX
  2.         boolean player = true;
  3.         int highestScoreDifference = Integer.MIN_VALUE;
  4.         //highestScoreDifference = player1Score - player2Score;
  5.         Point bestMove = null;//best move returns highest difference (above 0)
  6.         ArrayList<Point> possible = checkPossible(gameState);//all possible moves for NEW STATE
  7. //tu był score, gdyby coś przestało działać
  8.         for(Point move : possible) {
  9.             int score;
  10.             int[][] state = copyTab(gameState);
  11.             state[move.x][move.y] = player ? 2 : 1;//the new state
  12.             score = game.countScoreAdded(move, state);
  13.             player = false;
  14.             score += minimaxRec(player, state);
  15.             if(score > highestScoreDifference) {
  16.                 highestScoreDifference = score;
  17.                 bestMove = move;
  18.             }
  19.         }
  20.         return bestMove;
  21.     }
  22.  
  23.     private int minimaxRec(boolean player, int[][] gameState) {
  24.         ArrayList<Point> possible = checkPossible(gameState);
  25.         if(possible.size() == 0) {
  26.             return 0;
  27.         }
  28.         int lowestScore = Integer.MAX_VALUE;
  29.         int highestScore = Integer.MIN_VALUE;
  30. //tu był score, gdyby coś przestało działać
  31.         for(Point move : possible) {
  32.             int score = 0;
  33.             int[][] state = copyTab(gameState);
  34.             state[move.x][move.y] = player ? 2 : 1;
  35.             //tu był wcześniej zmeiniany newPlayer
  36.             if(player){//tu był wcześniej newPlayer
  37.                 score += game.countScoreAdded(move, state);//tu wcześniej był gameState
  38.             }
  39.             else {
  40.                 score -= game.countScoreAdded(move, state);//tu wcześniej był gameState
  41.             }
  42.             boolean newPlayer = !player;
  43.             score += minimaxRec(newPlayer, state);
  44.             if(score < lowestScore) {
  45.                 lowestScore = score;
  46.             }
  47.             if(score > highestScore) {
  48.                 highestScore = score;
  49.             }
  50.         }
  51.         return player ? highestScore : lowestScore;
  52.     }