Merge remote-tracking branch 'origin/felix-lek' into linus

This commit is contained in:
linhara 2020-10-03 16:09:22 +02:00
commit 97ebd73cf5
3 changed files with 19 additions and 1 deletions

1
api.py
View File

@ -52,6 +52,7 @@ def end_game(api_key, game_id=None):
sess = requests.Session() sess = requests.Session()
response = sess.get(base_api_path + "end" + game_id, headers={"x-api-key": api_key}) response = sess.get(base_api_path + "end" + game_id, headers={"x-api-key": api_key})
if response.status_code == 200: if response.status_code == 200:
print("Game ended succesfully")
return return
print("Fatal Error: could not end game") print("Fatal Error: could not end game")

View File

@ -26,6 +26,12 @@ class GameLayer:
""" """
api.end_game(self.api_key, self.game_state.game_id) api.end_game(self.api_key, self.game_state.game_id)
def force_end_game(self):
"""
End the current game without gameID
"""
api.end_game(self.api_key)
def start_game(self): def start_game(self):
""" """
Starts the game. Starts the game.

13
main.py
View File

@ -1,4 +1,6 @@
# import api # import api
import time
from sys import exit
from game_layer import GameLayer from game_layer import GameLayer
api_key = "74e3998d-ed3d-4d46-9ea8-6aab2efd8ae3" api_key = "74e3998d-ed3d-4d46-9ea8-6aab2efd8ae3"
@ -10,28 +12,37 @@ useTestStrategy = False
def main(): def main():
#game_layer.force_end_game()
game_layer.new_game(map_name) game_layer.new_game(map_name)
print("Starting game: " + game_layer.game_state.game_id) print("Starting game: " + game_layer.game_state.game_id)
game_layer.start_game() game_layer.start_game()
# exit game after timeout
start_time = time.time()
while game_layer.game_state.turn < game_layer.game_state.max_turns: while game_layer.game_state.turn < game_layer.game_state.max_turns:
take_turn() take_turn()
time_diff = time.time() - start_time
if time_diff > 5:
game_layer.end_game()
exit()
print("Done with game: " + game_layer.game_state.game_id) print("Done with game: " + game_layer.game_state.game_id)
print("Final score was: " + str(game_layer.get_score()["finalScore"])) print("Final score was: " + str(game_layer.get_score()["finalScore"]))
def take_turn(): def take_turn():
# TODO Implement your artificial intelligence here. # TODO Implement your artificial intelligence here.
# TODO Take one action per turn until the game ends. # TODO Take one action per turn until the game ends.
# TODO The following is a short example of how to use the StarterKit # TODO The following is a short example of how to use the StarterKit
if not useTestStrategy: if not useTestStrategy:
state = game_layer.game_state state = game_layer.game_state
print("testrunda")
# messages and errors for console log # messages and errors for console log
for message in game_layer.game_state.messages: for message in game_layer.game_state.messages:
print(message) print(message)
for error in game_layer.game_state.errors: for error in game_layer.game_state.errors:
print("Error: " + error) print("Error: " + error)
# pre-made test strategy # pre-made test strategy
# which came with # which came with
# starter kit # starter kit