def visit_current_space(self) -> float: """ :returns time taken """ total_time = 0 message = Message(self.communication) # objective reading already_have = self.data.get_objective( self.interface.position) != Knowledge.UNKNOWN data, time_taken = self.interface.read_sensor() total_time += time_taken self.data.set_objective(self.interface.position, data) if not already_have: message.add_objective( self.interface.position, self.data.get_objective(self.interface.position)) # check obstacles for direction in COORDINATE_CHANGE: looking_at = self.interface.position + COORDINATE_CHANGE[direction] if not self.data.out_of_bounds(looking_at): previous = self.data.get_obstacle(looking_at) obstacle_reading, time_taken = self.interface.see_obstacles( direction) total_time += time_taken new = Knowledge.YES if obstacle_reading else Knowledge.NO if new != previous: self.data.set_obstacle(looking_at, new) message.add_obstacle(looking_at, new) self.communication.send_message(message) return total_time
def test_message(): print(Message.coord_str(Coordinate(536, 0))) assert Message.coord_str(Coordinate(536, 0)) == "536 0 " print("passed Message.coord_str") Message.set_my_robot_id(1) m = Message() m.add_objective(Coordinate(3, 5), 2.0) print(m.get_data()) assert m.get_data() == Message.BEGIN + "1 1 j3 5 2.0k" + Message.END print("passed add_objective") m._cursor = 10 ov = m.extract_objective_value() print(ov) assert ov == 2.0 print("passed extract_objective_value") m = Message() m.add_obstacle(Coordinate(0, 6), Knowledge.YES) print(m.get_data()) assert m.get_data() == Message.BEGIN + "1 2 s0 6 yt" + Message.END print("passed add_obstacle") m._cursor = 6 co = m.extract_coordinates() print(co) assert co == Coordinate(0, 6) print("passed extract coordinate") d = DataRepository(10, 10) m.add_objective(Coordinate(6, 0), 7.0) m.handle(d) obs = d.get_obstacle(Coordinate(0, 6)) obj = d.get_objective(Coordinate(6, 0)) print(obs, obj) assert obs == Knowledge.YES assert obj == 7.0 print("passed handle")