Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
V-Born authored Jul 24, 2020
1 parent bd34c7c commit c2d9d55
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions main/loadDiplomacyGame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

import random
from diplomacy import Game
from diplomacy.utils.export import from_saved_game_format
from diplomacy.utils.export import is_valid_saved_game
from diplomacy.utils.export import to_saved_game_format
import ujson as json
import sys, getopt, os

# Creating a game
# map file's baseName can be the map_name argument. e.g. Game(map_name='pure')

# the following script, from https://pypi.org/project/diplomacy/
# plays a game locally by submitting random valid orders until the game is completed.
def main():
try:
(options, arguments) = getopt.getopt(sys.argv[1:], 'h')
except getopt.error:
sys.exit("Unknown input parameter.")
if [] == arguments:
arguments = ["pure"]
if not os.path.exists(arguments[0] + ".map"):
sys.exit("%s.map could not be opened" % (arguments[0],))
input_path = "../unitTestPureGame.json"
if not os.path.exists(input_path):
sys.exit("File %s does not exist." % input_path)
with open(input_path, 'r') as file:
combinedLines = ''
for line in file:
combinedLines += line.strip()
if(line.strip()== ''):
saved_game = json.loads(combinedLines)
game = from_saved_game_format(saved_game)
break

if not is_valid_saved_game(saved_game):
sys.exit("File %s was evaluated as invalid." % input_path)
if not game.is_game_done:
# Getting the list of possible orders for all locations
possible_orders = game.get_all_possible_orders()

# For each power, randomly sampling a valid order
for power_name, power in game.powers.items():
power_orders = []
for loc in game.get_orderable_locations(power_name):
if '/' == loc[-1]:
loc = loc[:-1]
if possible_orders[loc]:
power_orders.append(random.choice(possible_orders[loc]))
game.set_orders(power_name, power_orders)
game.process()
# Exporting the game to disk to visualize
with open('unitTestResult_game.json', 'w') as outp:
outp.write(to_saved_game_format(game))

if __name__ == '__main__':
main()

0 comments on commit c2d9d55

Please sign in to comment.