public int minValue(GameState state) { int v = int.MaxValue; if (terminalTest(state)) { return computeUtility(state); } else { ArrayList successorList = getSuccessorStates(state); for (int i = 0; i < successorList.Count; i++) { GameState successor = (GameState) successorList[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 override GameState makeMove(GameState state, Object o) { GameState retVal = new GameState(); //a move is the board itself DecapawnBoard move = ((DecapawnBoard)o).cloneBoard(); //we need to: //find out whose move it is string playerToMove = getPlayerToMove(state); ArrayList newMoves; //set the player whose move is next if (playerToMove.Equals("W")) { retVal.put("player", "B"); //add the new moves newMoves = move.getSuccessorBoards("B"); } else { retVal.put("player", "W"); //add the new moves newMoves = move.getSuccessorBoards("W"); } int parentLevel = getLevel(state); // if (parentLevel <= this.MaxSearchDepth && this.computeUtilityCallsThisMove < this.MaxMovesToConsider) // { retVal.put("moves", newMoves); retVal.put("utility",computeUtility(move, getPlayerToMove(getState()))); // } // else // { // for (int i = 1; i < newMoves.Count; i++) // { // newMoves.RemoveAt(i); // } // // retVal.put("moves",newMoves); // retVal.put("utility",0); // } retVal.put("board", move); retVal.put("level", parentLevel + 1); //if (newMoves.Count != 0) presentState = retVal; return retVal; }
public override ArrayList getSuccessorStates(GameState state) { GameState temp = presentState; ArrayList retVal = new ArrayList(); int parentLevel = getLevel(state); for (int i = 0; i < getMoves(state).Count; i++) { XYLocation loc = (XYLocation) getMoves(state)[i]; GameState aState = makeMove(state, loc); aState.put("moveMade", loc); aState.put("level", parentLevel + 1); retVal.Add(aState); } presentState = temp; return retVal; }
//get all possible successors from the current state public override ArrayList getSuccessorStates(GameState state) { GameState temp = presentState; ArrayList retVal = new ArrayList(); int parentLevel = getLevel(state); for (int i = 0; i < getMoves(state).Count; i++) { //XYLocation loc = (XYLocation) getMoves(state)[i]; //for each move, actually make the move HexapawnBoard move = (HexapawnBoard) getMoves(state)[i]; GameState aState = makeMove(state, move); aState.put("moveMade", move); aState.put("level", parentLevel + 1); retVal.Add(aState); } presentState = temp; return retVal; }
public GameState getMove(GameState state, int x, int y) { GameState retVal = null; XYLocation loc = new XYLocation(x, y); ArrayList moves = getMoves(state); ArrayList newMoves = (ArrayList) moves.Clone(); if (moves.Contains(loc)) { //int index = newMoves.indexOf(loc); int index = newMoves.IndexOf(loc); //newMoves.Remove(index); //.remove function is not overloaded in .net like it is in java //in .NET .remove only removes the instance of the parameter, //not the object at the index newMoves.RemoveAt(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",computeUtility(newBoard, getPlayerToMove(getState()))); retVal.put("level", getLevel(state) + 1); //presentState = retVal; } return retVal; }
public override GameState makeMove(GameState state, Object o) { GameState retVal = new GameState(); //a move is the board itself HexapawnBoard move = ((HexapawnBoard)o).cloneBoard(); //we need to: //find out whose move it is string playerToMove = getPlayerToMove(state); ArrayList newMoves; //set the player whose move is next if (playerToMove.Equals("W")) { retVal.put("player", "B"); //add the new moves newMoves = move.getSuccessorBoards("B"); } else { retVal.put("player", "W"); //add the new moves newMoves = move.getSuccessorBoards("W"); } retVal.put("moves", newMoves); retVal.put("board", move); retVal.put("utility",computeUtility(move, getPlayerToMove(getState()))); retVal.put("level", getLevel(state) + 1); //if (newMoves.Count != 0) presentState = retVal; return retVal; }
public override int computeUtility(GameState state) { int utility = computeUtility((TicTacToeBoard) state.get("board"), (getPlayerToMove(state))); return utility; }
public int getLevel(GameState g) { return (((int) g.get("level"))); }
public override int getAlphaBetaValue(GameState state) { this.computeUtilityCallsThisMove = 0; if (getPlayerToMove(state).ToUpper().Equals("W")) { AlphaBeta initial = new AlphaBeta(int.MinValue, int.MaxValue); int max = maxValue(state, initial); return max; } else { //invert? AlphaBeta initial = new AlphaBeta(int.MinValue, int.MaxValue); return minValue(state, initial); } }
public abstract GameState makeMove(GameState state, Object o);
public override int getAlphaBetaValue(GameState state) { if (getPlayerToMove(state).ToUpper().Equals("X")) { AlphaBeta initial = new AlphaBeta(int.MinValue, int.MaxValue); int max = maxValue(state, initial); return max; } else { //invert? AlphaBeta initial = new AlphaBeta(int.MinValue, int.MaxValue); return minValue(state, initial); } }
public DecapawnBoard getBoard(GameState state) { return (DecapawnBoard) state.get("board"); }
public abstract int computeUtility(GameState state);
public abstract bool terminalTest(GameState state);
public string getPlayerToMove(GameState state) { return state.get("player").ToString(); }
public int getUtility(GameState h) { return ((int) h.get("utility")); }
public ArrayList getMoves(GameState state) { return (ArrayList) state.get("moves"); }
public abstract int getAlphaBetaValue(GameState state);
public abstract int getMiniMaxValue(GameState 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 bool terminalTest(GameState state) { DecapawnBoard board = (DecapawnBoard) state.get("board"); // bool line = board.lineThroughBoard(); // bool filled = board.getNumberOfMarkedPositions() == 9; // return (line || filled); int thisLevel = getLevel(state); bool bailOut = false; if (thisLevel >= this.MaxSearchDepth && this.computeUtilityCallsThisMove > this.MaxMovesToConsider) bailOut = true; return (board.gameLost("B") || board.gameLost("W") || bailOut); }
// public void printPossibleMoves() // { // System.out.println("Possible moves"); // // ArrayList moves = getMoves(presentState); // for (int i = 0; i < moves.size(); i++) // { // XYLocation moveLoc = (XYLocation) moves.get(i); // GameState newState = getMove(presentState, // moveLoc.getXCoOrdinate(), moveLoc.getYCoOrdinate()); // TicTacToeBoard board = (TicTacToeBoard) newState.get("board"); // System.out.println("utility = " + computeUtility(newState)); // System.out.println(""); // } // // } public override int getMiniMaxValue(GameState state) { //statesSeen = new ArrayList(); // System.out.println("In get Minimax Value"); // System.out.println("Received state "); //((TicTacToeBoard)state.get("board")).print(); //if (getPlayerToMove(state).equalsIgnoreCase("X")) if (getPlayerToMove(state).ToUpper().Equals("X")) { return maxValue(state); } else { return minValue(state); } }
protected int maxValue(GameState state, AlphaBeta ab) { int v = int.MinValue; if (terminalTest(state)) { return computeUtility(state); } else { ArrayList successorList = getSuccessorStates(state); for (int i = 0; i < successorList.Count; i++) { GameState successor = (GameState) successorList[i]; int minimumValueOfSuccessor = minValue(successor, ab.copy()); if (minimumValueOfSuccessor > v) { v = minimumValueOfSuccessor; state.put("next", successor); } if (v >= ab.beta()) { //System.out.println("pruning from max"); return v; } //ab.setAlpha(Util.max(ab.alpha(), v)); ab.setAlpha(System.Math.Max(ab.alpha(),v)); } return v; } }
public TicTacToeBoard getBoard(GameState state) { return (TicTacToeBoard) state.get("board"); }
public override bool terminalTest(GameState state) { HexapawnBoard board = (HexapawnBoard) state.get("board"); // bool line = board.lineThroughBoard(); // bool filled = board.getNumberOfMarkedPositions() == 9; // return (line || filled); return (board.gameLost("B") || board.gameLost("W")); }
public override GameState makeMove(GameState state, Object o) { XYLocation loc = (XYLocation) o; return makeMove(state, loc.getXCoOrdinate(), loc.getYCoOrdinate()); }
public HexapawnBoard getBoard(GameState state) { return (HexapawnBoard) state.get("board"); }
public override int computeUtility(GameState state) { computeUtilityCalls++; this.computeUtilityCallsThisMove++; if (this.UseBetterCompute) { int thisLevel = getLevel(state); int utility = computeUtilityBetter((DecapawnBoard) state.get("board"), (getPlayerToMove(state)),thisLevel); return utility; } else { int utility = computeUtility((DecapawnBoard) state.get("board"), (getPlayerToMove(state))); return utility; } }
public abstract ArrayList getSuccessorStates(GameState state);