Example #1
0
        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;
            }

        }
Example #2
0
	public GameState makeMove(GameState state, int x, int y) {
		GameState temp = getMove(state, x, y);
		if (temp != null) {
			presentState = temp;
		}
		return presentState;
	}
Example #3
0
	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;
	}
Example #4
0
	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;
	}
Example #5
0
	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);
		}
	}
Example #6
0
	public override bool terminalTest(GameState state) {
		TicTacToeBoard board = (TicTacToeBoard) state.get("board");
		bool line = board.lineThroughBoard();
		bool filled = board.getNumberOfMarkedPositions() == 9;
		return (line || filled);
	}
Example #7
0
	public override int computeUtility(GameState state) {
		int utility = computeUtility((TicTacToeBoard) state.get("board"),
				(getPlayerToMove(state)));
		return utility;
	}
Example #8
0
 protected abstract bool terminalTest(GameState state);
Example #9
0
 public int getLevel(GameState g)
 {
     return (((int)g.get("level")).intValue());
 }
Example #10
0
 public abstract int getAlphaBetaValue(GameState state);
Example #11
0
 public abstract int getMiniMaxValue(GameState state);
Example #12
0
 public abstract GameState makeMove(GameState state, Object o);
Example #13
0
 public abstract List<GameState> getSuccessorStates(GameState state);
Example #14
0
 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;
     }
 }
Example #15
0
	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);
		}
	}
Example #16
0
	public TicTacToeBoard getBoard(GameState state) {

		return (TicTacToeBoard) state.get("board");
	}
Example #17
0
 public List getMoves(GameState state)
 {
     return (List)state.get("moves");
 }
Example #18
0
	public override GameState makeMove(GameState state, Object o) {
		XYLocation loc = (XYLocation) o;
		return makeMove(state, loc.getXCoOrdinate(), loc.getYCoOrdinate());
	}
Example #19
0
 public String getPlayerToMove(GameState state)
 {
     return (String)state.get("player");
 }
Example #20
0
 public int getUtility(GameState h)
 {
     return ((int)h.get("utility")).intValue();
 }
Example #21
0
 //
 // PROTECTED METHODS
 //
 protected abstract int computeUtility(GameState state);