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 void removeQueenFrom(XYLocation l) { if (squares[l.getXCoOrdinate()][l.getYCoOrdinate()] == 1) { squares[l.getXCoOrdinate()][l.getYCoOrdinate()] = 0; } }
public void moveObjectToAbsoluteLocation(EnvironmentObject eo, XYLocation loc) { // Ensure the object is not already at a location envState.moveObjectToAbsoluteLocation(eo, loc); // Ensure is added to the environment addEnvironmentObject(eo); }
public override bool Equals(Object o) { if (null == o || !(o is XYLocation)) { return(base.Equals(o)); } XYLocation anotherLoc = (XYLocation)o; return((anotherLoc.getXCoOrdinate() == xCoOrdinate) && (anotherLoc .getYCoOrdinate() == yCoOrdinate)); }
public void moveObject(EnvironmentObject eo, XYLocation.Direction direction) { XYLocation presentLocation = envState.getCurrentLocationFor(eo); if (null != presentLocation) { XYLocation locationToMoveTo = presentLocation.locationAt(direction); if (!(isBlocked(locationToMoveTo))) { moveObjectToAbsoluteLocation(eo, locationToMoveTo); } } }
public TicTacToe() { List<XYLocation> moves = new List<XYLocation>(); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { XYLocation loc = new XYLocation(i, j); moves.Add(loc); } } initialState.put("moves", moves); initialState.put("player", "X"); initialState.put("utility", new int(0)); initialState.put("board", new TicTacToeBoard()); initialState.put("level", new int(0)); presentState = initialState; }
public HashSet<Action> actions(Object state) { NQueensBoard board = (NQueensBoard)state; HashSet<Action> actions = new LinkedHashSet<Action>(); int numQueens = board.getNumberOfQueensOnBoard(); int boardSize = board.getSize(); for (int i = 0; i < boardSize; i++) { XYLocation newLocation = new XYLocation(numQueens, i); if (!(board.isSquareUnderAttack(newLocation))) { actions.Add(new QueenAction(QueenAction.PLACE_QUEEN, newLocation)); } } return actions; }
public int evaluateManhattanDistanceOf(int i, XYLocation loc) { int retVal = -1; int xpos = loc.getXCoOrdinate(); int ypos = loc.getYCoOrdinate(); switch (i) { case 1: retVal = Math.abs(xpos - 0) + Math.abs(ypos - 1); break; case 2: retVal = Math.abs(xpos - 0) + Math.abs(ypos - 2); break; case 3: retVal = Math.abs(xpos - 1) + Math.abs(ypos - 0); break; case 4: retVal = Math.abs(xpos - 1) + Math.abs(ypos - 1); break; case 5: retVal = Math.abs(xpos - 1) + Math.abs(ypos - 2); break; case 6: retVal = Math.abs(xpos - 2) + Math.abs(ypos - 0); break; case 7: retVal = Math.abs(xpos - 2) + Math.abs(ypos - 1); break; case 8: retVal = Math.abs(xpos - 2) + Math.abs(ypos - 2); break; } return retVal; }
/** * Creates a queen action. Supported values of type are {@link #PLACE_QUEEN} * , {@link #REMOVE_QUEEN}, or {@link #MOVE_QUEEN}. */ public QueenAction(String type, XYLocation loc) { super(type); setAttribute(ATTRIBUTE_QUEEN_LOC, loc); }
public HashSet<EnvironmentObject> getObjectsAt(XYLocation loc) { HashSet<EnvironmentObject> objectsAt = objsAtLocation.get(loc); if (null == objectsAt) { // Always ensure an empty Set is returned objectsAt = new LinkedHashSet<EnvironmentObject>(); objsAtLocation.put(loc, objectsAt); } return objectsAt; }
public void testXYLocationAtributeSettingOnConstruction() { XYLocation loc = new XYLocation(3, 4); Assert.AreEqual(3, loc.getXCoOrdinate()); Assert.AreEqual(4, loc.getYCoOrdinate()); }
public void testEquality() { XYLocation loc1 = new XYLocation(3, 4); XYLocation loc2 = new XYLocation(3, 4); Assert.AreEqual(loc1, loc2); }
public bool isBlocked(XYLocation loc) { foreach (EnvironmentObject eo in envState.getObjectsAt(loc)) { if (eo is Wall) { return true; } } return false; }
public void makePerimeter() { for (int i = 0; i < envState.width; i++) { XYLocation loc = new XYLocation(i, 0); XYLocation loc2 = new XYLocation(i, envState.height - 1); envState.moveObjectToAbsoluteLocation(new Wall(), loc); envState.moveObjectToAbsoluteLocation(new Wall(), loc2); } for (int i = 0; i < envState.height; i++) { XYLocation loc = new XYLocation(0, i); XYLocation loc2 = new XYLocation(envState.width - 1, i); envState.moveObjectToAbsoluteLocation(new Wall(), loc); envState.moveObjectToAbsoluteLocation(new Wall(), loc2); } }
public int getNumberOfAttacksOn(XYLocation l) { int x = l.getXCoOrdinate(); int y = l.getYCoOrdinate(); return numberOfHorizontalAttacksOn(x, y) + numberOfVerticalAttacksOn(x, y) + numberOfDiagonalAttacksOn(x, y); }
public HashSet<EnvironmentObject> getObjectsAt(XYLocation loc) { return envState.getObjectsAt(loc); }
public void addQueenAt(XYLocation l) { if (!(queenExistsAt(l))) squares[l.getXCoOrdinate()][l.getYCoOrdinate()] = 1; }
public bool isSquareUnderAttack(XYLocation l) { int x = l.getXCoOrdinate(); int y = l.getYCoOrdinate(); return (isSquareHorizontallyAttacked(x, y) || isSquareVerticallyAttacked(x, y) || isSquareDiagonallyAttacked( x, y)); }
public List<XYLocation> getPositions() { List<XYLocation> retVal = new List<XYLocation>(); for (int i = 0; i < 9; i++) { int absPos = getPositionOf(i); XYLocation loc = new XYLocation(getXCoord(absPos), getYCoord(absPos)); retVal.Add(loc); } return retVal; }
/** * Moves the queen in the specified column (x-value of <code>l</code>) to * the specified row (y-value of <code>l</code>). The action assumes a * complete-state formulation of the n-queens problem. * * @param l */ public void moveQueenTo(XYLocation l) { for (int i = 0; i < size; i++) squares[l.getXCoOrdinate()][i] = 0; squares[l.getXCoOrdinate()][l.getYCoOrdinate()] = 1; }
public void moveObjectToAbsoluteLocation(EnvironmentObject eo, XYLocation loc) { // Ensure is not already at another location foreach (Set<EnvironmentObject> eos in objsAtLocation.values()) { if (eos.remove(eo)) { break; // Should only every be at 1 location } } // Add it to the location specified getObjectsAt(loc).Add(eo); }
public bool queenExistsAt(XYLocation l) { return (queenExistsAt(l.getXCoOrdinate(), l.getYCoOrdinate())); }
public void moveQueen(XYLocation from, XYLocation to) { if ((queenExistsAt(from)) && (!(queenExistsAt(to)))) { removeQueenFrom(from); addQueenAt(to); } }
// // PRIVATE METHODS // private bool withinRadius(int radius, XYLocation agentLocation, XYLocation objectLocation) { int xdifference = agentLocation.getXCoOrdinate() - objectLocation.getXCoOrdinate(); int ydifference = agentLocation.getYCoOrdinate() - objectLocation.getYCoOrdinate(); return Math.sqrt((xdifference * xdifference) + (ydifference * ydifference)) <= radius; }
public void addObjectToLocation(EnvironmentObject eo, XYLocation loc) { moveObjectToAbsoluteLocation(eo, loc); }
public int getValueAt(XYLocation loc) { return getValueAt(loc.getXCoOrdinate(), loc.getYCoOrdinate()); }
public HashSet<Action> actions(Object state) { HashSet<Action> actions = new LinkedHashSet<Action>(); NQueensBoard board = (NQueensBoard)state; for (int i = 0; i < board.getSize(); i++) for (int j = 0; j < board.getSize(); j++) { XYLocation loc = new XYLocation(i, j); if (!board.queenExistsAt(loc)) actions .Add(new QueenAction(QueenAction.MOVE_QUEEN, loc)); } return actions; }