argparse
This commit is contained in:
parent
bd3026a854
commit
9cba685957
2 changed files with 42 additions and 25 deletions
|
@ -6,6 +6,7 @@ from time import sleep
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import random
|
||||||
|
|
||||||
from widgets import *
|
from widgets import *
|
||||||
from state import Table, Hand
|
from state import Table, Hand
|
||||||
|
@ -266,7 +267,7 @@ def make_auto_move(original, game, difficulty):
|
||||||
game.next_turn()
|
game.next_turn()
|
||||||
|
|
||||||
|
|
||||||
def main(difficulty, dbg=False):
|
def main(difficulty, game, dbg=False):
|
||||||
from animation.octopus import intro
|
from animation.octopus import intro
|
||||||
|
|
||||||
global exit, action
|
global exit, action
|
||||||
|
@ -275,7 +276,6 @@ def main(difficulty, dbg=False):
|
||||||
|
|
||||||
intro()
|
intro()
|
||||||
sleep(1)
|
sleep(1)
|
||||||
game = state.State(ID, ["PVR", ID])
|
|
||||||
|
|
||||||
game.next_turn()
|
game.next_turn()
|
||||||
|
|
||||||
|
@ -357,27 +357,44 @@ def main(difficulty, dbg=False):
|
||||||
if game.hasEnded:
|
if game.hasEnded:
|
||||||
print(f'{Fore.RED}' + "Game has ended, player '" + game.winner + "' has won"+f'{Style.RESET_ALL}')
|
print(f'{Fore.RED}' + "Game has ended, player '" + game.winner + "' has won"+f'{Style.RESET_ALL}')
|
||||||
else:
|
else:
|
||||||
s = input(f'{Fore.MAGENTA}Do you want to save the game? (y/n)\n')
|
s = input(f'{Fore.MAGENTA}Write a filename if you want to save the game status or press enter to exit\n')
|
||||||
while s.lower() not in ['y', 'yes', 'n', 'no']:
|
|
||||||
s = input(f"{Fore.MAGENTA}Please write 'y' or 'n'\n")
|
|
||||||
print(f'{Style.RESET_ALL}')
|
print(f'{Style.RESET_ALL}')
|
||||||
if s.lower() == 'y' or s.lower() == 'yes':
|
if s != '':
|
||||||
game.dump()
|
game.dump(s)
|
||||||
|
|
||||||
|
def parse_args(argv):
|
||||||
|
import argparse
|
||||||
|
parser = argparse.ArgumentParser(description='Octopus Carnival.')
|
||||||
|
parser.add_argument('--difficulty', type=str, nargs=1, default='medium',
|
||||||
|
help='[easy|medium|hard|n]')
|
||||||
|
parser.add_argument('--seed', type=int, nargs=1, default=None,
|
||||||
|
help='Seed for randomness')
|
||||||
|
parser.add_argument('--debug', action='store_const', const=True,
|
||||||
|
help='provide access to the REPL')
|
||||||
|
parser.add_argument('--load', type=str, nargs=1, default='',
|
||||||
|
help='load a savefile')
|
||||||
|
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
return vars(args)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from sys import argv
|
from sys import argv
|
||||||
|
|
||||||
diff = argv[1]
|
args = parse_args(argv[1:])
|
||||||
dbg = 'DEBUG' in argv
|
print(args)
|
||||||
if diff == 'easy':
|
diff = args['difficulty'][0]
|
||||||
main(7, dbg)
|
d = {'medium':14, 'hard':21, 'easy':7}
|
||||||
elif diff == 'medium':
|
try:
|
||||||
main(14, dbg)
|
diff = int(diff) if diff.isdigit() else d[diff]
|
||||||
elif diff == 'hard':
|
except:
|
||||||
main(21, dbg)
|
print('Can\'t parse difficulty')
|
||||||
else:
|
|
||||||
try:
|
random.seed = args['seed'][0] if args['seed'] is not None else os.urandom(1)
|
||||||
diff = int(diff)
|
dbg = args['debug'] is not None
|
||||||
except:
|
|
||||||
print('Wrong argument for difficulty')
|
game = state.State(ID, ["PVR", ID])
|
||||||
main(int(diff), dbg)
|
if args['load']:
|
||||||
|
game.load(args['load'][0])
|
||||||
|
|
||||||
|
# start the game
|
||||||
|
main(diff, game, dbg)
|
||||||
|
|
|
@ -153,7 +153,7 @@ class State:
|
||||||
self.turn.append((table, hand))
|
self.turn.append((table, hand))
|
||||||
return table, hand
|
return table, hand
|
||||||
|
|
||||||
def dump(self):
|
def dump(self, fname):
|
||||||
j = dict()
|
j = dict()
|
||||||
j['table'] = [tc.cards for tc in self.table.cards]
|
j['table'] = [tc.cards for tc in self.table.cards]
|
||||||
for pl, hand in self.players.items():
|
for pl, hand in self.players.items():
|
||||||
|
@ -161,12 +161,12 @@ class State:
|
||||||
j['nrounds'] = self.nrounds
|
j['nrounds'] = self.nrounds
|
||||||
j['deck'] = self.deck
|
j['deck'] = self.deck
|
||||||
j['players'] = list(self.players.keys())
|
j['players'] = list(self.players.keys())
|
||||||
with open('save.machiavelli', 'w') as f:
|
with open(fname, 'w') as f:
|
||||||
f.write(json.dumps(j))
|
f.write(json.dumps(j))
|
||||||
return j
|
return j
|
||||||
|
|
||||||
def load(self):
|
def load(self, fname):
|
||||||
with open('save.machiavelli', 'r') as f:
|
with open(fname, 'r') as f:
|
||||||
j = json.loads(f.read())
|
j = json.loads(f.read())
|
||||||
self.nrounds = j['nrounds']
|
self.nrounds = j['nrounds']
|
||||||
self.deck = [Card(seed, value) for seed, value in j['deck']]
|
self.deck = [Card(seed, value) for seed, value in j['deck']]
|
||||||
|
|
Loading…
Reference in a new issue