pytron/headless.py

42 lines
1.5 KiB
Python
Raw Permalink Normal View History

2018-10-23 16:27:27 +02:00
#!/usr/bin/env python3
2019-03-13 20:45:36 +01:00
from tron.map import Map
from tron.game import Game, PositionPlayer
2019-03-13 22:26:39 +01:00
from tron.player import Direction, ConstantPlayer
# This script shows how to create a game with AI, that will run automatically.
# It is made to be fast and not to be used by humans. It especially doesn't
2019-03-22 09:57:45 +01:00
# display any window and doesn't listen to any keystrokes.
2018-10-23 16:27:27 +02:00
def main():
2019-03-13 22:26:39 +01:00
# Prepare the size for the game.
# Those values may be good if you want to play, they might not be so good
# to train your AI. Decreasing them will make the learning faster.
2019-03-27 15:19:54 +01:00
width = 10
height = 10
2018-10-23 16:27:27 +02:00
2019-03-13 22:26:39 +01:00
# Create a game from its size and its players
2018-10-23 16:27:27 +02:00
game = Game(width, height, [
2019-03-13 22:26:39 +01:00
# Here we create two players with constant direction.
# It's not very interesting but it's the basis of everything else.
PositionPlayer(1, ConstantPlayer(Direction.RIGHT), [0, 0]),
2019-03-13 18:00:46 +01:00
PositionPlayer(2, ConstantPlayer(Direction.LEFT), [width - 1, height - 1]),
2018-10-23 16:27:27 +02:00
])
2019-03-13 22:26:39 +01:00
# Run the game.
# Since no window is passed as parameter, not only the game will not
# display anything, which avoid doing useless computations, but it will
# also not be limited to a certain framerate, which would be necessary for
# human users.
2018-12-13 11:27:41 +01:00
game.main_loop()
2018-10-23 16:27:27 +02:00
2019-03-13 22:26:39 +01:00
# The game is done, you can get information about it and do what you want.
if game.winner is None:
print("It's a draw!")
else:
print('Player {} wins!'.format(game.winner))
2018-10-23 16:27:27 +02:00
if __name__ == '__main__':
main()