forked from sfucmpt105-beta/risk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
risk.py
executable file
·121 lines (102 loc) · 4.03 KB
/
risk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
# std
import argparse
import sys
import os
# user
import risk
import risk.logger
import risk.game_master
from risk.board import board
from risk.game_master import GameMaster
# fixes the pathing so that the game doesn't need to be run from root
if '__file__' in globals():
root = os.path.dirname(__file__) or './'
os.path.join(root)
os.chdir(root)
# exit codes
_EXIT_BAD_ARGS = -1
###############################################################################
# CLI option parsing
#
def app_setup():
parser = argparse.ArgumentParser(description='Risk game with Python')
# dev build defaults to debug for now
parser.add_argument('--verbose', '-v', action='count',
help='extra output', default=risk.logger.LEVEL_DEBUG)
parser.add_argument('--cli', '-c', action='store_true',
help='commandline version of game', default=False)
settings = parser.parse_args()
risk.logger.LOG_LEVEL = settings.verbose
return settings
###############################################################################
# CLI functionsr
#
def print_banner():
print(
"""
--==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==--
|| PyRisk ||
||-----------------------------------------------------------------||
|| Risk is a turn-based game for two to six players. The standard ||
|| version is played on a board depicting a political map of the ||
|| Earth, divided into forty-two territories, which are grouped ||
|| into six continents. The primary object of the game is "world ||
|| domination," or "to occupy every territory on the board and in ||
|| so doing, eliminate all other players." Players control ||
|| armies with which they attempt to capture territories from ||
|| other players, with results determined by dice rolls. ||
||-----------------------------------------------------------------||
|| By: CMPT106 Group Beta ||
--==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==--
""")
###############################################################################
# Debug functions
#
def end_turn_debug_print(game_master):
risk.logger.debug('Ending turn...')
###############################################################################
# Main game functions
#
def game_setup(settings):
_DEV_HUMAN_PLAYERS = 1
game_board = board.generate_empty_board()
#game_board = board.generate_mini_board()
game_master = risk.game_master.GameMaster(game_board, settings)
game_master.generate_players(_DEV_HUMAN_PLAYERS, settings.cli)
game_master.add_end_turn_callback(end_turn_debug_print)
# dev
board.dev_random_assign_owners(game_master)
return game_master
def run_game(game_master):
print_banner()
risk.logger.debug('Starting risk game...')
try:
# game_master.choose_territories()
# game_master.deploy_troops()
while not game_master.ended:
run_turn(game_master)
except (risk.errors.input.UserQuitInput, KeyboardInterrupt, EOFError):
game_master.end_game()
except BaseException as e:
risk.logger.critical(repr(e))
risk.logger.critical('unknown error occured, attempting perform'
' graceful shutdown...')
game_master.end_game()
risk.logger.debug('User quit the game!')
def run_turn(game_master):
risk.logger.debug('Current player is: %s' %
game_master.current_player().name)
game_master.player_take_turn()
game_master.call_end_turn_callbacks()
game_master.end_turn()
if __name__ == '__main__':
settings = app_setup()
risk.logger.debug(settings)
master = game_setup(settings)
print('Initializing graphics...')
if not settings.cli:
from risk.graphics import graphics
graphics.init(master)
master.add_end_game_callback(graphics.shutdown)
run_game(master)