Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package awale;
- import java.util.ArrayList;
- public class Minimax {
- private AwaleBoard firstBoard;
- private final int profondeurChoisie;
- /**
- * @param firstBoard
- */
- public Minimax(AwaleBoard firstBoard, int profondeur) {
- this.firstBoard = firstBoard;
- this.profondeurChoisie = profondeur;
- }
- public ArrayList<Integer> getCoupsPossible(AwaleBoard awaleBoard, int playerNumber) {
- ArrayList<Integer> indicesCoupsPossibles = new ArrayList<>();
- int[] getBoard = awaleBoard.getBoard();
- if (awaleBoard.getNbTurns() % 2 == 0 && playerNumber == 0) {
- for (int i = 0; i < 6; i++) {
- if (getBoard[i] > 0)
- indicesCoupsPossibles.add(i);
- }
- }
- if (awaleBoard.getNbTurns() % 2 == 1 && playerNumber == 1) {
- for (int i = 6; i < 12; i++) {
- if (getBoard[i] > 0)
- indicesCoupsPossibles.add(i);
- }
- }
- return indicesCoupsPossibles;
- }
- public int miniMax() {
- return max(new AwaleBoard(firstBoard), profondeurChoisie, -1).getIndiceDuCoupAyantEteJoue();
- }
- /**
- * This function returns the best min() if deep is > 1 for each possible plays of an AwaleBoard.
- * Else, returns the best move for this AwaleBoard.
- * @param awaleBoard
- * @param profondeur
- * @param premierCoup
- * @return
- */
- public Move max(AwaleBoard awaleBoard, int profondeur, int premierCoup) {
- ArrayList<Integer> indicesCoupsPossibles = getCoupsPossible(awaleBoard, 1);
- Moves coupsJoues = new Moves(new AwaleBoard(awaleBoard), new Move[indicesCoupsPossibles.size()]);
- Move bestCoup = new Move(new AwaleBoard(), new AwaleBoard(), -1, 0, 0);
- profondeur--;
- if (indicesCoupsPossibles.size() > 0) {
- for (int i = 0; i < coupsJoues.size(); i++) {
- coupsJoues.coups[i] = new Move(new AwaleBoard(awaleBoard),
- new AwaleBoard(awaleBoard).play(indicesCoupsPossibles.get(i)), indicesCoupsPossibles.get(i),
- awaleBoard.getScore(1), awaleBoard.getScore(0));
- coupsJoues.coups[i].setGainGenereMachine(
- coupsJoues.coups[i].gainGenereParMachine + coupsJoues.coups[i].calculeGainGenere(1));
- }
- // ATTENTION
- if (profondeur > 0) {
- for (int i = 0; i < coupsJoues.size(); i++) {
- if (getCoupsPossible(coupsJoues.coups[i].tableauApresCoup, 0).size() > 0)
- coupsJoues.coups[i] = min(coupsJoues.coups[i].tableauApresCoup, profondeur,
- coupsJoues.coups[i].indiceDuCoupAyantEteJoue);
- }
- }
- // ATTENTION
- bestCoup = coupsJoues.getBestCoup();
- }
- return bestCoup;
- }
- /**
- * This function returns the badest max() if deep is > 1 for each possible plays of an AwaleBoard.
- * Else, returns the badest move for this AwaleBoard.
- * @param awaleBoard
- * @param profondeur
- * @param premierCoup
- * @return
- */
- public Move min(AwaleBoard awaleBoard, int profondeur, int premierCoup) {
- ArrayList<Integer> indicesCoupsPossibles = getCoupsPossible(awaleBoard, 0);
- Moves coupsJoues = new Moves(awaleBoard, new Move[indicesCoupsPossibles.size()]);
- Move badestCoup = null;
- profondeur--;
- if (indicesCoupsPossibles.size() > 0) {
- for (int i = 0; i < coupsJoues.size(); i++) {
- coupsJoues.coups[i] = new Move(awaleBoard, awaleBoard.play(indicesCoupsPossibles.get(i)), premierCoup,
- awaleBoard.getScore(1), awaleBoard.getScore(0));
- coupsJoues.coups[i].setGainGenereHumain(
- coupsJoues.coups[i].gainGenereParHumain + coupsJoues.coups[i].calculeGainGenere(0));
- }
- // ATTENTION
- if (profondeur > 1) {
- for (int i = 0; i < coupsJoues.size(); i++) {
- if (getCoupsPossible(coupsJoues.coups[i].tableauApresCoup, 1).size() > 0)
- coupsJoues.coups[i] = max(coupsJoues.coups[i].tableauApresCoup, profondeur,
- coupsJoues.coups[i].indiceDuCoupAyantEteJoue);
- }
- }
- // ATTENTION
- badestCoup = coupsJoues.getBadestCoup();
- }
- return badestCoup;
- }
- /**
- * @return the firstBoard
- */
- public AwaleBoard getFirstBoard() {
- return new AwaleBoard(firstBoard);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement