ITI0011RUS:Gomoku
Вернуться на страницу предмета
MyStrategy.java: <source lang="java"> package gomoku.strategies;
import java.util.ArrayList;
import gomoku.*;
public class MyStrategy implements ComputerStrategy {
private static final int MINIMAX_DEPTH = 4;
@Override public Location getMove(SimpleBoard board, int player) {
int maxScore = -Integer.MAX_VALUE; Location bestMove = null; ArrayList<Location> possibleMoves = getPossibleMoves(board, player); for(Location move : possibleMoves) { int moveScore = minimax(board, player, MINIMAX_DEPTH); if(moveScore > maxScore) { maxScore = moveScore; bestMove = move; } } return bestMove; }
private ArrayList<Location> getPossibleMoves(SimpleBoard board, int player) { return new ArrayList<Location>(); }
private static final int MAX_SCORE = 100;
private int minimax(SimpleBoard board, int player, int depth) { /* * Stop condition */ int score = getScore(board,player); if ( score == MAX_SCORE || // WIN-condition score == -MAX_SCORE || // LOSS-condition timeIsOver() || // time limit depth == 0 ) return score;
/* * Iterate over possible moves */ ArrayList<Location> possibleMoves = getPossibleMoves(board, player); int bestScore; if(player == SimpleBoard.PLAYER_WHITE) { // maximizing player
bestScore = Integer.MAX_VALUE;
for(Location move : possibleMoves) { makeMove(board, move, player); // board contains the new state int curScore = minimax(board, 3-player, depth-1); if(curScore > bestScore) bestScore = curScore; undoMove(board, move); // rollback }
} else { bestScore = -Integer.MAX_VALUE;
for(Location move : possibleMoves) { makeMove(board, move, player); // board contains the new state int curScore = minimax(board, 3-player, depth-1); if(curScore < bestScore) bestScore = curScore; undoMove(board, move); // rollback } }
return bestScore; }
private void makeMove(SimpleBoard board, Location move, int player) {
}
private void undoMove(SimpleBoard board, Location move) {
}
private boolean timeIsOver() { return false; }
private int getScore(SimpleBoard board, int player) { return 0; }
@Override public String getName() { return "Tudengi nimi"; }
}
</source>