def execute(self): assert self.isPossible(self.n, self.board, self.car) for i in range(self.n): oneMove = MoveDown(self.board, self.car).execute() self.board = oneMove[0] if len(oneMove[1]) == 2: self.car = Car(oneMove[1][0], oneMove[1][1], self.car.carName) else: self.car = Truck(oneMove[1][0], oneMove[1][1], oneMove[1][2], \ self.car.carName) return (self.board, self.car.getLocation())
def stdInputInitState(): TrafficBoard.n = input("What are the board dimensions(n x n)? ") r = input("Enter row coordinate for end gate: ") c = input("Enter column coordinate for end gate:") TrafficBoard.endGate = (r, c) TrafficBoard.numCars = input("How many cars? (excluding SPECIAL car)") TrafficBoard.numTrucks = input("How many trucks?") cars = {} trucks = {} carName = 'A' for i in range(TrafficBoard.numCars): r1 = input("Enter #1 row coordinate for (front) car #" + str(i) + " :") c1 = input("Enter #1 column coordinate for (front) car #" + str(i) + " :") r2 = input("Enter #2 row coordinate for (front) car #" + str(i) + " :") c2 = input("Enter #2 column coordinate for (front) car #" + str(i) + " :") cars[carName] = Car((r1, c1), (r2, c2), carName) carName = chr(ord(carName) + 1) for i in range(TrafficBoard.numTrucks): r1 = input("Enter #1 row coordinate for (front) truck #" + str(i) + " :") c1 = input("Enter #1 column coordinate for (front) truck #" + str(i) + " :") r2 = input("Enter #2 row coordinate for (front) truck #" + str(i) + " :") c2 = input("Enter #2 column coordinate for (front) truck #" + str(i) + " :") r3 = input("Enter #3 row coordinate for (front) truck #" + str(i) + " :") c3 = input("Enter #3 column coordinate for (front) truck #" + str(i) + " :") trucks[carName] = Truck((r1, c1), (r2, c2), (r3, c3), carName) carName = chr(ord(carName) + 1) r1 = input("Enter #1 row coordinate for RED car #" + str(i) + " :") c1 = input("Enter #1 column coordinate for RED car #" + str(i) + " :") r2 = input("Enter #2 row coordinate for (front) RED #" + str(i) + " :") c2 = input("Enter #2 column coordinate for (front) RED #" + str(i) + " :") cars["**"] = Car((r1, c1), (r2, c2), "**") board = TrafficBoard.createEmptyMatrix(TrafficBoard.n) for key in cars: car = cars[key] for loc in car.getLocation(): board[loc[0]][loc[1]] = car.carName carName = chr(ord(carName) + 1) for key in trucks: truck = trucks[key] for loc in truck.getLocation(): board[loc[0]][loc[1]] = truck.carName cars.update(trucks) return TrafficBoard(board, cars)
def isPossible(n, board, car): for i in range(n): if not MoveDown.isPossible(board, car): return False else: oneMove = MoveDown(board, car).execute() board = oneMove[0] if len(oneMove[1]) == 2: car = Car(oneMove[1][0], oneMove[1][1], car.carName) else: car = Truck(oneMove[1][0], oneMove[1][1], oneMove[1][2], \ car.carName) return True
def FileInputInitState(filename): file = open(filename, 'r') TrafficBoard.n = int(file.readline()) r = int(file.readline()) c = int(file.readline()) TrafficBoard.endGate = (r, c) TrafficBoard.numCars = int(file.readline()) TrafficBoard.numTrucks = int(file.readline()) cars = {} trucks = {} carName = 'A' for i in range(TrafficBoard.numCars): r1 = int(file.readline()) c1 = int(file.readline()) r2 = int(file.readline()) c2 = int(file.readline()) cars[carName] = Car((r1, c1), (r2, c2), carName) carName = chr(ord(carName) + 1) for i in range(TrafficBoard.numTrucks): r1 = int(file.readline()) c1 = int(file.readline()) r2 = int(file.readline()) c2 = int(file.readline()) r3 = int(file.readline()) c3 = int(file.readline()) trucks[carName] = Truck((r1, c1), (r2, c2), (r3, c3), carName) carName = chr(ord(carName) + 1) r1 = int(file.readline()) c1 = int(file.readline()) r2 = int(file.readline()) c2 = int(file.readline()) cars["**"] = Car((r1, c1), (r2, c2), "**") board = TrafficBoard.createEmptyMatrix(TrafficBoard.n) for key in cars: car = cars[key] for loc in car.getLocation(): board[loc[0]][loc[1]] = car.carName carName = chr(ord(carName) + 1) for key in trucks: truck = trucks[key] for loc in truck.getLocation(): board[loc[0]][loc[1]] = truck.carName cars.update(trucks) return TrafficBoard(board, cars)
class MoveDownBy: # CONSTRUCTOR # @param n number of steps to move right by # @param board board configuration # @param car car to be moved def __init__(self, n, board, car): self.n = n self.board = board self.car = car # Executes the given action # @return tuple: (resulting board config, new location of car) def execute(self): assert self.isPossible(self.n, self.board, self.car) for i in range(self.n): oneMove = MoveDown(self.board, self.car).execute() self.board = oneMove[0] if len(oneMove[1]) == 2: self.car = Car(oneMove[1][0], oneMove[1][1], self.car.carName) else: self.car = Truck(oneMove[1][0], oneMove[1][1], oneMove[1][2], \ self.car.carName) return (self.board, self.car.getLocation()) # @return boolean is this action possible? @staticmethod def isPossible(n, board, car): for i in range(n): if not MoveDown.isPossible(board, car): return False else: oneMove = MoveDown(board, car).execute() board = oneMove[0] if len(oneMove[1]) == 2: car = Car(oneMove[1][0], oneMove[1][1], car.carName) else: car = Truck(oneMove[1][0], oneMove[1][1], oneMove[1][2], \ car.carName) return True # @return string represenation of this action def __str__(self): return "Moving " + self.car.carName + " DOWN by " + str(self.n)
def execute(self): totalCount = 0 visitedNodes = set() q = PriorityQueue() q.put(self.init) while not q.empty(): thisNode = q.get() if not thisNode.action is None and \ thisNode.action.car.carName == "**" and\ self.isGoal(self, thisNode.board.board, thisNode.board.endGate): # goal state is reachable only if action moves red car return thisNode possibleActions = thisNode.getPossibleActions() thisBoard = thisNode.board thisVehicles = thisBoard.vehicles for action in possibleActions: matrix, locations = action.execute() if not isVisited(matrix, visitedNodes): newVehicles = copy(thisVehicles) if len(locations) == 2: newVehicles[action.car.carName] = Car(locations[0], \ locations[1], \ action.car.carName) else: newVehicles[action.car.carName] = Truck(locations[0], \ locations[1], \ locations[2], \ action.car.carName) newBoard = TrafficBoard(matrix, newVehicles) newNode = Node(newBoard, thisNode, action, thisNode.pathCost + 1, self.h) q.put(newNode) addVisitedNode(thisNode, visitedNodes) #addVisitedNode(thisNode, visitedNodes) return None
# Test 4 board = [[0 for _ in range(5)] for _ in range(5)] board[0][0] = "A" board[0][1] = "A" board[0][2] = "B" board[0][3] = "B" assert MoveRight.isPossible(board, Car((0,0), (0,1), "A")) == False assert MoveRight.isPossible(board, Car((0,2), (0,3), "B")) == True # Test 5 board = [[0 for _ in range(5)] for _ in range(5)] board[0][0] = "A" board[0][1] = "A" board[0][2] = "A" assert MoveRight.isPossible(board, Truck((0,0), (0,1), (0,2), "A")) == True ## This class represents the action of moving the given car one square to the ## left class MoveLeft: # CONSTRUCTIOR # @param board board configuration # @param car car to be moved def __init__(self, board, car): self.board = board self.car = car # Executes the action, returning resulting board config # @return tuple of resulting board config, new location of cars def execute(self):