| 177 | return self._state() |
| 178 | |
| 179 | def step(self, action): |
| 180 | self.counter += 1 |
| 181 | self.render() |
| 182 | if self.counter % 2 == 1: |
| 183 | for o in self.obstacles: |
| 184 | if o["state"][0] == WIDTH - 1: o["direction"] = 1 |
| 185 | elif o["state"][0] == 0: o["direction"] = -1 |
| 186 | o["state"][0] += 1 if o["direction"] == -1 else -1 |
| 187 | |
| 188 | x, y = self.agent |
| 189 | if action == 0 and y > 0: y -= 1 |
| 190 | elif action == 1 and y < HEIGHT - 1: y += 1 |
| 191 | elif action == 2 and x < WIDTH - 1: x += 1 |
| 192 | elif action == 3 and x > 0: x -= 1 |
| 193 | self.agent = [x, y] |
| 194 | |
| 195 | done = self.agent == self.goal |
| 196 | reward = 1.0 if done else sum(-1.0 for o in self.obstacles if o["state"] == self.agent) |
| 197 | reward -= self.step_penalty |
| 198 | self.score += reward |
| 199 | if reward < -self.step_penalty: |
| 200 | self._hit = 4 |
| 201 | return self._state(), reward, done |
| 202 | |
| 203 | def _state(self): |
| 204 | ax, ay = self.agent |