Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Player {
- static int mainPlayer;
- /**
- * Performs a move
- *
- * @param pState
- * the current state of the board
- * @param pDue
- * time before which we must have returned
- * @return the next state the board is in after our move
- */
- public GameState play(final GameState pState, final Deadline pDue) {
- Vector<GameState> lNextStates = new Vector<GameState>();
- pState.findPossibleMoves(lNextStates);
- mainPlayer = pState.getNextPlayer();
- System.err.println("Jag är:" + pState.getNextPlayer());
- if (lNextStates.size() == 0) {
- // Must play "pass" move if there are no other moves possible.
- return new GameState(pState, new Move());
- }
- /**
- * Here you should write your algorithms to get the best next move, i.e.
- * the best next state. This skeleton returns a random move instead.
- */
- int score = Integer.MIN_VALUE;
- int tempScore = 0;
- int index = 0;
- for (int i = 0; i < lNextStates.size(); i++) {
- tempScore = alphaBeta(lNextStates.elementAt(i), 9, Integer.MIN_VALUE, Integer.MAX_VALUE, 2);
- System.err.println("Potentiella drag: " + tempScore);
- if (tempScore > score){
- score = tempScore;
- index = i;
- }
- }
- System.err.println("Det bästa draget har value: " + score);
- return lNextStates.elementAt(index);
- }
- /*
- * and to check if it is a king, you would check if
- *
- * (lBoard.At(23)&CELL_KING)
- */
- /**
- * This function counts the normal pieces as 1 point and the kings as 2 points.
- *
- */
- public static int evalFunction(GameState gameState, int player) {
- int redValue = 0;
- int whiteValue = 0;
- for (int cell = 0; cell < gameState.NUMBER_OF_SQUARES; cell++) {
- int row = gameState.cellToRow(cell);
- int col = gameState.cellToCol(cell);
- int cellValue = gameState.get(row, col);
- //System.err.println(cellValue);
- if (cellValue == Constants.CELL_RED){
- redValue += 1;
- }
- else if (cellValue == Constants.CELL_RED + Constants.CELL_KING){
- redValue += 1.5;
- }
- else if (cellValue == Constants.CELL_WHITE){
- whiteValue += 1;
- }
- else if (cellValue == Constants.CELL_WHITE + Constants.CELL_KING){
- whiteValue += 1.5;
- }
- }
- if (redValue == 0) {
- whiteValue += 10;
- } else if (whiteValue == 0) {
- redValue += 10;
- }
- int value;
- if (mainPlayer == 1) {
- value = redValue - whiteValue;
- } else{
- value = whiteValue - redValue;
- }
- return value;
- }
- public static int alphaBeta (GameState gameState, int depth, int alpha, int beta, int player){
- Vector<GameState> nextStates = new Vector<GameState>();
- gameState.findPossibleMoves(nextStates);
- int value;
- if (depth == 0 || nextStates.size() == 0){
- value = evalFunction(gameState, player);
- return value;
- }
- else if (player == 1){
- value = Integer.MIN_VALUE;
- for (GameState nextState: nextStates) {
- value = Math.max(value, alphaBeta(nextState, depth-1, alpha, beta, 2));
- alpha = Math.max(alpha, value);
- if (beta <= alpha){
- break;
- }
- }
- }
- else{
- value = Integer.MAX_VALUE;
- for (GameState nextState:nextStates) {
- value = Math.min(value, alphaBeta(nextState, depth-1, alpha, beta, 1));
- beta = Math.min(beta, value);
- if (beta <= alpha){
- break;
- }
- }
- }
- return value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement