コード例 #1
0
ファイル: Mancala.java プロジェクト: girikuncoro/mancala
  /**
   * Evaluates the scores for the board position
   *
   * @param currPlayer
   * @param tempBoard
   * @return scores
   */
  public int getEvalFunc(Player currPlayer, int[] tempBoard) {
    int mancalaIndex = mancalaBoard.getMancala(currPlayer.playerNum);
    // System.out.println("i am player " + currPlayer.playerNum + "  & my mancala is "  +
    // mancalaIndex );

    int oppMancalaIndex = mancalaBoard.getOpponentsMancala(currPlayer.playerNum);
    // System.out.println("i am opponent & my mancala is "  + oppMancalaIndex );

    // System.out.println("value in board for the indices are : " + tempBoard[mancalaIndex] + " & "
    // + tempBoard[oppMancalaIndex] );
    return (tempBoard[mancalaIndex] - tempBoard[oppMancalaIndex]);
  }
コード例 #2
0
ファイル: Mancala.java プロジェクト: girikuncoro/mancala
  /**
   * Makes the move temporarily for seeing the moves ahead
   *
   * @param move
   * @param currPlayer
   * @return the board
   */
  public int[] makeTempMove(Move move, Player currPlayer) {

    int moveIndex = move.getMoveIndex();
    mancalaBoard.displayBoard();

    int boardSize = mancalaBoard.getBoardSize();
    int[] tempBoard = new int[boardSize];
    int[] srcBoard = mancalaBoard.getBoard();

    System.arraycopy(srcBoard, 0, tempBoard, 0, boardSize);

    int numOfStones = tempBoard[moveIndex];

    tempBoard[moveIndex] = 0;
    moveIndex++;

    while (numOfStones > 0) {
      if ((moveIndex % boardSize) == mancalaBoard.getOpponentsMancala(currPlayer.playerNum)) {
        moveIndex++;
        continue;
      }
      tempBoard[(moveIndex) % boardSize]++;
      moveIndex++;
      numOfStones--;
    }

    // Just to check if we get a free turn
    int indexToCompare = (moveIndex - 1) % boardSize;
    int myMancala = mancalaBoard.getMancala(currPlayer.playerNum);

    if (!(indexToCompare == myMancala)) {
      if (tempBoard[indexToCompare] == 1) {
        if ((currPlayer.playerNum == 1 && indexToCompare < mancalaBoard.getPitSize())
            || (currPlayer.playerNum == 2 && indexToCompare > mancalaBoard.getPitSize())) {

          // check opponents opp pit - 2*p - index
          int oppPit = (mancalaBoard.getPitSize() * 2) - indexToCompare;
          if (tempBoard[oppPit] > 0) {
            tempBoard[myMancala] += tempBoard[oppPit] + tempBoard[indexToCompare];
            tempBoard[oppPit] = 0;
            tempBoard[indexToCompare] = 0;
          }
        }
      }
    }
    return tempBoard;
  }
コード例 #3
0
ファイル: Mancala.java プロジェクト: girikuncoro/mancala
  /**
   * Make the selected move
   *
   * @param move : the move the player wants to take
   * @param currPlayer
   * @return true if after making a turn we get a free turn
   */
  public boolean makeMove(Move move, Player currPlayer) {

    boolean freeTurn = false;
    int moveIndex = move.getMoveIndex();
    int[] tempBoard = mancalaBoard.getBoard();
    int boardSize = mancalaBoard.getBoardSize();
    int numOfStones = tempBoard[moveIndex];

    tempBoard[moveIndex] = 0;
    moveIndex++;

    while (numOfStones > 0) {
      if ((moveIndex % boardSize) == mancalaBoard.getOpponentsMancala(currPlayer.playerNum)) {
        moveIndex++;
        continue;
      }
      tempBoard[(moveIndex) % boardSize]++;
      moveIndex++;
      numOfStones--;
    }

    int indexToCompare = (moveIndex - 1) % boardSize;
    int myMancala = mancalaBoard.getMancala(currPlayer.playerNum);

    // Check if you get a free turn
    if (indexToCompare == myMancala) {
      freeTurn = true;
      System.out.println("You got a free turn with this move");
    } else {
      // Check if you can steal the stones
      if (tempBoard[indexToCompare] == 1) {
        if ((currPlayer.playerNum == 1 && indexToCompare < mancalaBoard.getPitSize())
            || (currPlayer.playerNum == 2 && indexToCompare > mancalaBoard.getPitSize())) {
          int oppPit = (mancalaBoard.getPitSize() * 2) - indexToCompare;
          if (tempBoard[oppPit] > 0) {
            tempBoard[myMancala] += tempBoard[oppPit] + tempBoard[indexToCompare];
            tempBoard[oppPit] = 0;
            tempBoard[indexToCompare] = 0;
          }
        }
      }
    }

    mancalaBoard.setBoard(tempBoard);
    return freeTurn;
  }
コード例 #4
0
ファイル: Mancala.java プロジェクト: girikuncoro/mancala
  /**
   * Generate nextMove according to greedy algorithm
   *
   * @param currPlayer
   * @return the move according to the greedy algorithm
   */
  public Move getGreedyMove(Player currPlayer) {

    TreeMap<Integer, ArrayList<Integer>> scoreMoveMap = new TreeMap<>();

    int mancalaIndex = mancalaBoard.getMancala(currPlayer.playerNum);
    int oppMancalaIndex = mancalaBoard.getOpponentsMancala(currPlayer.playerNum);

    int startIndex = 0;
    int endIndex = mancalaBoard.getPitSize();
    if (currPlayer.playerNum == 2) {
      startIndex = mancalaBoard.getPitSize() + 1;
      endIndex = mancalaBoard.getBoardSize() - 1;
    }

    for (int i = startIndex; i < endIndex; i++) {

      Move currMove = new Move(i);
      int[] tempBoard = new int[mancalaBoard.getBoardSize()];
      tempBoard = makeTempMove(currMove, currPlayer);

      int numToInc = tempBoard[mancalaIndex];
      ArrayList<Integer> possibleMoves = new ArrayList<Integer>();

      if (scoreMoveMap.containsKey(numToInc)) {
        possibleMoves = (ArrayList<Integer>) scoreMoveMap.get(numToInc);
      }

      possibleMoves.add(i);
      scoreMoveMap.put(numToInc, possibleMoves);
    }

    int highestEvalVal = scoreMoveMap.lastKey();
    List<Integer> highestMoves = scoreMoveMap.get(highestEvalVal);

    Move finalMove = new Move(highestMoves.remove(0));
    while (isIllegalMove(finalMove)) {
      finalMove = new Move(highestMoves.remove(0));
    }

    return finalMove;
  }
コード例 #5
0
ファイル: Mancala.java プロジェクト: girikuncoro/mancala
  public BoardFTCheck makeTempMiniMaxMove(Move move, Player currPlayer, int[] srcBoard) {

    int moveIndex = move.getMoveIndex();

    int boardSize = mancalaBoard.getBoardSize();
    int[] tempBoard = new int[boardSize];

    System.arraycopy(srcBoard, 0, tempBoard, 0, boardSize);
    int numOfStones = tempBoard[moveIndex];

    tempBoard[moveIndex] = 0;
    moveIndex++;

    while (numOfStones > 0) {
      if ((moveIndex % boardSize) == mancalaBoard.getOpponentsMancala(currPlayer.playerNum)) {
        moveIndex++;
        continue;
      }
      tempBoard[(moveIndex) % boardSize]++;
      moveIndex++;
      numOfStones--;
    }

    // if just to check if we get a free turn
    int indexToCompare = (moveIndex - 1) % boardSize;
    // System.out.println("indexTocomp : " +  indexToCompare);
    int myMancala = mancalaBoard.getMancala(currPlayer.playerNum);

    boolean freeTurn = false;

    // if just to check if we get a free turn
    if (indexToCompare == myMancala) {
      freeTurn = true;
      System.out.println("Yayyyyii , I got a free turn");
    } else {
      if (tempBoard[indexToCompare] == 1) {
        if ((currPlayer.playerNum == 1 && indexToCompare < mancalaBoard.getPitSize())
            || (currPlayer.playerNum == 2 && indexToCompare > mancalaBoard.getPitSize())) {
          // check opponents opp pit - 2*p - index
          int oppPit = (mancalaBoard.getPitSize() * 2) - indexToCompare;
          if (tempBoard[oppPit] > 0) {
            tempBoard[myMancala] += tempBoard[oppPit] + tempBoard[indexToCompare];
            tempBoard[oppPit] = 0;
            tempBoard[indexToCompare] = 0;
          }
        }
      }
    }
    // System.out.println("tempBoard in make temp move: " + tempBoard.toString());

    // if the game is over we need to evaluate the function
    boolean gameOver = false;
    if (isGameOver(tempBoard)) {

      tempBoard = getAllStones(tempBoard);
      gameOver = true;
    }

    System.out.println();
    BoardFTCheck bft = new BoardFTCheck(tempBoard, freeTurn, gameOver);
    return bft;
  }
コード例 #6
0
ファイル: Mancala.java プロジェクト: girikuncoro/mancala
 /**
  * Find the score for a player
  *
  * @param currPlayer
  * @return the score
  */
 public int getScore(Player currPlayer) {
   return (mancalaBoard.getBoard())[(mancalaBoard.getMancala(currPlayer.playerNum))];
 }