Ejemplo n.º 1
0
Archivo: Game.cs Proyecto: langeds/aima
		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;
			}

		}
Ejemplo n.º 2
0
		public GameState makeMove(GameState state, int x, int y) 
		{
			GameState temp = getMove(state, x, y);
			if (temp != null) 
			{
				presentState = temp;
			}
			return presentState;
		}
Ejemplo n.º 3
0
		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;
		}
Ejemplo n.º 4
0
		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;
		}
Ejemplo n.º 5
0
		//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;
		}
Ejemplo n.º 6
0
		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;
		}
Ejemplo n.º 7
0
		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;
		}
Ejemplo n.º 8
0
		public override int computeUtility(GameState state) 
		{
			int utility = computeUtility((TicTacToeBoard) state.get("board"),
				(getPlayerToMove(state)));
			return utility;
		}
Ejemplo n.º 9
0
Archivo: Game.cs Proyecto: langeds/aima
		public int getLevel(GameState g) 
		{
			return (((int) g.get("level")));
		}
Ejemplo n.º 10
0
		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);
			}
		}
Ejemplo n.º 11
0
Archivo: Game.cs Proyecto: langeds/aima
		public abstract GameState makeMove(GameState state, Object o);
Ejemplo n.º 12
0
		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);
			}
		}
Ejemplo n.º 13
0
		public DecapawnBoard getBoard(GameState state) 
		{

			return (DecapawnBoard) state.get("board");
		}
Ejemplo n.º 14
0
Archivo: Game.cs Proyecto: langeds/aima
		public abstract int computeUtility(GameState state);
Ejemplo n.º 15
0
Archivo: Game.cs Proyecto: langeds/aima
		public abstract bool terminalTest(GameState state);
Ejemplo n.º 16
0
Archivo: Game.cs Proyecto: langeds/aima
		public string getPlayerToMove(GameState state) 
		{
			return state.get("player").ToString();
		}
Ejemplo n.º 17
0
Archivo: Game.cs Proyecto: langeds/aima
		public int getUtility(GameState h) 
		{
			return ((int) h.get("utility"));
		}
Ejemplo n.º 18
0
Archivo: Game.cs Proyecto: langeds/aima
		public ArrayList getMoves(GameState state) 
		{
			return (ArrayList) state.get("moves");
		}
Ejemplo n.º 19
0
Archivo: Game.cs Proyecto: langeds/aima
		public abstract int getAlphaBetaValue(GameState state);
Ejemplo n.º 20
0
Archivo: Game.cs Proyecto: langeds/aima
		public abstract int getMiniMaxValue(GameState state);
Ejemplo n.º 21
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);
		}
Ejemplo n.º 22
0
		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);
		}
Ejemplo n.º 23
0
//		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);
			}
		}
Ejemplo n.º 24
0
Archivo: Game.cs Proyecto: langeds/aima
		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;
			}

		}
Ejemplo n.º 25
0
		public TicTacToeBoard getBoard(GameState state) 
		{

			return (TicTacToeBoard) state.get("board");
		}
Ejemplo n.º 26
0
		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"));
		}
Ejemplo n.º 27
0
		public override GameState makeMove(GameState state, Object o) 
		{
			XYLocation loc = (XYLocation) o;
			return makeMove(state, loc.getXCoOrdinate(), loc.getYCoOrdinate());
		}
Ejemplo n.º 28
0
		public HexapawnBoard getBoard(GameState state) 
		{

			return (HexapawnBoard) state.get("board");
		}
Ejemplo n.º 29
0
		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;
			}
				

		}
Ejemplo n.º 30
0
Archivo: Game.cs Proyecto: langeds/aima
		public abstract ArrayList getSuccessorStates(GameState state);