public int minValue(GameState state) { int v = int.MAX_VALUE; if (terminalTest(state)) { return computeUtility(state); } else { List<GameState> successorList = getSuccessorStates(state); for (int i = 0; i < successorList.Count; i++) { GameState successor = successorList.get(i); int maximumValueOfSuccessors = maxValue(successor); if (maximumValueOfSuccessors < v) { v = maximumValueOfSuccessors; state.put("next", successor); } } return v; } }
public GameState makeMove(GameState state, int x, int y) { GameState temp = getMove(state, x, y); if (temp != null) { presentState = temp; } return presentState; }
public GameState getMove(GameState state, int x, int y) { GameState retVal = null; XYLocation loc = new XYLocation(x, y); List moves = getMoves(state); List newMoves = (List) moves.clone(); if (moves.contains(loc)) { int index = newMoves.indexOf(loc); newMoves.remove(index); retVal = new GameState(); retVal.put("moves", newMoves); TicTacToeBoard newBoard = getBoard(state).cloneBoard(); if (getPlayerToMove(state) == "X") { newBoard.markX(x, y); retVal.put("player", "O"); } else { newBoard.markO(x, y); retVal.put("player", "X"); } retVal.put("board", newBoard); retVal.put("utility", new int(computeUtility(newBoard, getPlayerToMove(getState())))); retVal.put("level", new int(getLevel(state) + 1)); // presentState = retVal; } return retVal; }
public override List<GameState> getSuccessorStates(GameState state) { GameState temp = presentState; List<GameState> retVal = new List<GameState>(); int parentLevel = getLevel(state); for (int i = 0; i < getMoves(state).Count; i++) { XYLocation loc = (XYLocation) getMoves(state).get(i); GameState aState = makeMove(state, loc); aState.put("moveMade", loc); aState.put("level", new int(parentLevel + 1)); retVal.Add(aState); } presentState = temp; return retVal; }
public override int getMiniMaxValue(GameState state) { // statesSeen = new List(); // System.Console.WriteLine("In get Minimax Value"); // System.Console.WriteLine("Received state "); // ((TicTacToeBoard)state.get("board")).print(); if (getPlayerToMove(state).equalsIgnoreCase("X")) { return maxValue(state); } else { return minValue(state); } }
public override bool terminalTest(GameState state) { TicTacToeBoard board = (TicTacToeBoard) state.get("board"); bool line = board.lineThroughBoard(); bool filled = board.getNumberOfMarkedPositions() == 9; return (line || filled); }
public override int computeUtility(GameState state) { int utility = computeUtility((TicTacToeBoard) state.get("board"), (getPlayerToMove(state))); return utility; }
protected abstract bool terminalTest(GameState state);
public int getLevel(GameState g) { return (((int)g.get("level")).intValue()); }
public abstract int getAlphaBetaValue(GameState state);
public abstract int getMiniMaxValue(GameState state);
public abstract GameState makeMove(GameState state, Object o);
public abstract List<GameState> getSuccessorStates(GameState state);
protected int maxValue(GameState state, AlphaBeta ab) { int v = int.MIN_VALUE; if (terminalTest(state)) { return computeUtility(state); } else { List<GameState> successorList = getSuccessorStates(state); for (int i = 0; i < successorList.Count; i++) { GameState successor = (GameState)successorList.get(i); int minimumValueOfSuccessor = minValue(successor, ab.copy()); if (minimumValueOfSuccessor > v) { v = minimumValueOfSuccessor; state.put("next", successor); } if (v >= ab.beta()) { // System.Console.WriteLine("pruning from max"); return v; } ab.setAlpha(Util.max(ab.alpha(), v)); } return v; } }
public override int getAlphaBetaValue(GameState state) { if (getPlayerToMove(state).equalsIgnoreCase("X")) { AlphaBeta initial = new AlphaBeta(int.MIN_VALUE, int.MAX_VALUE); int max = maxValue(state, initial); return max; } else { // invert? AlphaBeta initial = new AlphaBeta(int.MIN_VALUE, int.MAX_VALUE); return minValue(state, initial); } }
public TicTacToeBoard getBoard(GameState state) { return (TicTacToeBoard) state.get("board"); }
public List getMoves(GameState state) { return (List)state.get("moves"); }
public override GameState makeMove(GameState state, Object o) { XYLocation loc = (XYLocation) o; return makeMove(state, loc.getXCoOrdinate(), loc.getYCoOrdinate()); }
public String getPlayerToMove(GameState state) { return (String)state.get("player"); }
public int getUtility(GameState h) { return ((int)h.get("utility")).intValue(); }
// // PROTECTED METHODS // protected abstract int computeUtility(GameState state);