Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ipvc.estg.prog2.LinesAction.players;
- import ipvc.estg.prog2.LinesAction.Player;
- import ipvc.estg.prog2.LinesAction.Result;
- import ipvc.estg.prog2.LinesAction.Space;
- import ipvc.estg.prog2.LinesAction.State;
- public class MinimaxPlayer extends Player {
- public MinimaxPlayer(String name) {
- super(name);
- }
- private int heuristic(State state) {
- Space[][] grid = state.getGrid();
- int contador=0;
- for(int c=0;c<grid.length;c++){
- for(int l=0;l<grid.length;l++){
- if(grid[l][c].getPiece() == state.getPlayingColor()){
- contador++;
- }
- if(grid[l][c].getPiece()!= state.getPlayingColor() && grid[l][c].getPiece() != ' '){
- contador--;
- }
- }
- }
- return contador;
- /**
- // verificar todas as linhas
- int longest = 0;
- // por cada linha
- for (int row = 0; row < grid.length; ++row) {
- int seq = 0;
- // por cada coluna dessa linha
- for (int col = 0; col < grid[row].length; ++col) {
- if (grid[row][col].getPiece() == this.getCurrentColor()) {
- seq++;
- } else {
- if (seq > longest) {
- longest = seq;
- }
- seq = 0;
- }
- }
- if (seq > longest) {
- longest = seq;
- }
- }
- // por cada coluna
- for (int col = 0; col < grid[0].length; ++col) {
- int seq = 0;
- // por cada linha dessa coluna
- for (int row = 0; row < grid.length; ++row) {
- if (grid[row][col].getPiece() == this.getCurrentColor()) {
- seq++;
- } else {
- if (seq > longest) {
- longest = seq;
- }
- seq = 0;
- }
- }
- if (seq > longest) {
- longest = seq;
- }
- }
- //check upward diagonal
- for (int row = 3; row < grid.length; row++) {
- for (int col = 0; col < grid[0].length - 3; col++) {
- int seq1 = (grid[row][col].getPiece() == this.getCurrentColor() ? 1 : 0) +
- (grid[row - 1][col + 1].getPiece() == this.getCurrentColor() ? 1 : 0) +
- (grid[row - 2][col + 2].getPiece() == this.getCurrentColor() ? 1 : 0);
- int seq2 = (grid[row - 1][col + 1].getPiece() == this.getCurrentColor() ? 1 : 0) +
- (grid[row - 2][col + 2].getPiece() == this.getCurrentColor() ? 1 : 0) +
- (grid[row - 3][col + 3].getPiece() == this.getCurrentColor() ? 1 : 0);
- if (seq1 > longest) {
- longest = seq1;
- }
- if (seq2 > longest) {
- longest = seq2;
- }
- }
- }
- //check downward diagonal
- for (int row = 0; row < state.getGrid().length - 3; row++) {
- for (int col = 0; col < state.getGrid()[0].length - 3; col++) {
- int seq1 = (grid[row][col].getPiece() == this.getCurrentColor() ? 1 : 0) +
- (grid[row + 1][col + 1].getPiece() == this.getCurrentColor() ? 1 : 0) +
- (grid[row + 2][col + 2].getPiece() == this.getCurrentColor() ? 1 : 0);
- int seq2 = (grid[row + 1][col + 1].getPiece() == this.getCurrentColor() ? 1 : 0) +
- (grid[row + 2][col + 2].getPiece() == this.getCurrentColor() ? 1 : 0) +
- (grid[row + 3][col + 3].getPiece() == this.getCurrentColor() ? 1 : 0);
- if (seq1 > longest) {
- longest = seq1;
- }
- if (seq2 > longest) {
- longest = seq2;
- }
- }
- }
- return longest;**/
- }
- private class MinimaxOutput{
- public int score;
- public Space[] action;
- public MinimaxOutput(int score, Space[] action) {
- this.score = score;
- this.action = action;
- }
- }
- /**
- * Efetua uma pesquisa minimax (recursivamente)
- *
- * @param state estado atual sobre o qual a pesquisa deve ser feita
- * @param depth profundidade máxima de pesquisa
- * @param alpha parametro alpha para pruning
- * @param beta parametro beta para pruning
- * @return
- */
- private MinimaxOutput minimax(State state, int depth, int alpha, int beta) {
- // verificar se o jogo terminou (vitória ou derrota)
- if (state.hasWinner()) {
- Result result = state.getResult(this.getCurrentColor());
- switch (result) {
- case WIN:
- return new MinimaxOutput(4,null);
- case LOOSE:
- return new MinimaxOutput(-4,null);
- }
- }
- // profundidade máxima atingida
- if (depth == 0) {
- return new MinimaxOutput(heuristic(state),null);
- }
- Space[] jogPossiveis = new Space[8];
- // se for o minimax a jogar
- if (this.getCurrentColor() == state.getPlayingColor()) {
- int value = Integer.MIN_VALUE;
- Space[] selectedAction= null;
- for (int posI = 0; posI < state.getGrid()[0].length; posI++) {
- for (int posF = 0; posF < state.getGrid()[0].length; posF++) {
- Space[] action = new Space[2];
- if(state.getGrid()[posI][posF].getPiece()==state.getPlayingColor()){
- action[0] = new Space(posI, posF);
- jogPossiveis = state.possiblePlay(action[0]);
- for(int i =0; i<jogPossiveis.length;i++){
- if (jogPossiveis[i]!=null){
- action[1] = jogPossiveis[i];
- if (state.validate(action[0], action[1])) {
- int previousA = value;
- State nextState = state.getClone();
- nextState.next(action);
- value = Math.max(value, minimax(nextState, depth - 1, alpha, beta).score);
- alpha = Math.max(alpha, value);
- if (value >= previousA) {
- selectedAction = action;
- }
- /**System.out.println("\nPeça Inicial "+selectedAction[0].getRow()+" "+selectedAction[0].getColumn()+
- " - Peça Final "+selectedAction[1].getRow()+" "+selectedAction[1].getColumn()+" "+value+"\n");**/
- if (alpha >= beta) {
- break;
- }
- }
- }
- }
- }
- }
- }
- return new MinimaxOutput(value,selectedAction);
- }
- else { // adversário a jogar
- int value = Integer.MAX_VALUE;
- Space[] selectedAction= null;
- for (int posI = 0; posI < state.getGrid()[0].length; posI++) {
- for (int posF = 0; posF < state.getGrid()[0].length; posF++) {
- Space[] action = new Space[2];
- if(state.getGrid()[posI][posF].getPiece()==state.getPlayingColor()) {
- action[0] = new Space(posI, posF);
- jogPossiveis = state.possiblePlay(action[0]);
- for (int i = 0; i < jogPossiveis.length; i++) {
- if (jogPossiveis[i] != null) {
- action[1] = jogPossiveis[i];
- if (state.validate(action[0], action[1])) {
- int previousA = value;
- State nextState = state.getClone();
- nextState.next(action);
- value = Math.min(value, minimax(nextState, depth - 1, alpha, beta).score);
- beta = Math.min(beta, value);
- if (value <= previousA) {
- selectedAction = action;
- }
- if (beta <= alpha) {
- break;
- }
- }
- }
- }
- }
- }
- }
- return new MinimaxOutput(value,selectedAction);
- }
- }
- @Override
- public Space[] getAction(State state) {
- Space[] action = new Space[2];
- action = minimax(state, 3, Integer.MIN_VALUE, Integer.MAX_VALUE).action;
- System.out.println("\nBot HARD - "+"Peça Inicial "+action[0].getRow()+" "+action[0].getColumn()+
- " - Peça Final "+action[1].getRow()+" "+action[1].getColumn()+"\n");
- return action;
- }
- @Override
- public void eventNewGame(char color) {
- }
- @Override
- public void eventWin(State state) {
- }
- @Override
- public void eventLoose(State state) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement