diff --git a/.gitignore b/.gitignore index 8d35cb3..739fd39 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__ *.pyc +ais diff --git a/play.py b/play.py index 8b52ca4..13c4441 100755 --- a/play.py +++ b/play.py @@ -8,6 +8,8 @@ from tron.game import Game, PositionPlayer from tron.window import Window from tron.player import Direction, KeyboardPlayer, Mode +from ais.wallavoider import Ai + # This script shows how to create a game with human players and play it interactively. # It shows how to create the game, to setup interactive controls for users, and # to run the game while rendering it on a window with a reasonnable framerate. @@ -29,7 +31,7 @@ def main(): # default direction that will be to the right, and that will use the Z, # Q, S and D keys. # The last array defines the initial position of the player. - PositionPlayer(1, KeyboardPlayer(Direction.RIGHT, Mode.ZQSD), [0, 0]), + PositionPlayer(1, Ai(), [0, 0]), # We create a second player that will use the arrow keys. PositionPlayer(2, KeyboardPlayer(Direction.LEFT, Mode.ARROWS), [width - 1, height - 1]), diff --git a/tron/player.py b/tron/player.py index 28b821a..9e90b0d 100644 --- a/tron/player.py +++ b/tron/player.py @@ -34,6 +34,19 @@ class Player(object): """ return '/'.join(self.__module__.split('.')[:-1]) + '/' + name + def next_position(self, current_position, direction): + """ + Computes the next position if the player would go on towards a specific + direction. + """ + if direction == Direction.UP: + return (current_position[0] - 1, current_position[1]) + if direction == Direction.RIGHT: + return (current_position[0], current_position[1] + 1) + if direction == Direction.DOWN: + return (current_position[0] + 1, current_position[1]) + if direction == Direction.LEFT: + return (current_position[0], current_position[1] - 1) def next_position_and_direction(self, current_position, id, map): """ @@ -41,14 +54,7 @@ class Player(object): depending on the current position. """ direction = self.action(map, id) - if direction == Direction.UP: - return ((current_position[0] - 1, current_position[1]), direction) - if direction == Direction.RIGHT: - return ((current_position[0], current_position[1] + 1), direction) - if direction == Direction.DOWN: - return ((current_position[0] + 1, current_position[1]), direction) - if direction == Direction.LEFT: - return ((current_position[0], current_position[1] - 1), direction) + return (self.next_position(current_position, direction), direction) def action(self, map, id): """