Skip to content

Commit

Permalink
py3 conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
cktang88 committed Mar 15, 2020
1 parent 79add39 commit 9a0d885
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 72 deletions.
9 changes: 5 additions & 4 deletions risk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# std
import argparse
import sys
Expand All @@ -7,7 +7,7 @@
import risk
import risk.logger
import risk.game_master
from risk import board
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
Expand Down Expand Up @@ -37,7 +37,7 @@ def app_setup():
## CLI functionsr
#
def print_banner():
print \
print(\
"""
--==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==--
|| PyRisk ||
Expand All @@ -53,7 +53,7 @@ def print_banner():
||-----------------------------------------------------------------||
|| By: CMPT106 Group Beta ||
--==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==--
"""
""")


###############################################################################
Expand Down Expand Up @@ -104,6 +104,7 @@ def run_turn(game_master):
settings = app_setup()
risk.logger.debug(settings)
master = game_setup(settings)
print('hello')
if not settings.cli:
import risk.graphics
risk.graphics.init(master)
Expand Down
4 changes: 2 additions & 2 deletions risk/ai/bots.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def deploy_order(self, game_master, number_of_turns, continent):
for territory in game_master.board.continents[continent].values():
if self.reserves > 0:
if territory.owner == self:
print self.reserves
print(self.reserves)
game_master.player_add_army(self, territory.name, self.reserves)
if number_of_turns > 10:
for territory in game_master.board.continents[continent].values():
Expand Down Expand Up @@ -161,5 +161,5 @@ def deploy_all_to_continent(self, game_master, continent):
for territory in game_master.board.continents[continent].values():
if self.reserves > 0:
if territory.owner == self:
print self.reserves
print(self.reserves)
game_master.player_add_army(self, territory.name, self.reserves)
2 changes: 1 addition & 1 deletion risk/battle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import random

import risk
import board.territory as Territory
import risk.board.territory as Territory

from risk.errors.battle import *

Expand Down
2 changes: 1 addition & 1 deletion risk/board/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from board import *
from risk.board import *
2 changes: 1 addition & 1 deletion risk/board/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
import random

import territory
from risk.board import territory

import risk.logger
from risk.errors.board import *
Expand Down
15 changes: 7 additions & 8 deletions risk/board/territory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
###############################################################################
## Defines risk territories
#
from sets import Set

import risk.logger

Expand Down Expand Up @@ -34,7 +33,7 @@ def closest_enemy_distance(self):
# -1 represents infinite distance
# n >= 0 means distance from this node
NOT_FOUND = -1
visited = Set()
visited = set()
to_visit = [(self, [])]
closest = None
while len(to_visit) > 0 and not closest:
Expand All @@ -55,7 +54,7 @@ def closest_enemy_distance(self):
def is_connected(self, target):
# naive depth first search
# warning, this operation is VERY expensive!
return Territory._graph_connection_search(self, target, Set([self]))
return Territory._graph_connection_search(self, target, set([self]))

def __str__(self):
return "[%s]\n" \
Expand Down Expand Up @@ -98,7 +97,7 @@ def borders(self, list_of_borders):
self.border(border[0], border[1])

def create_territory_if_needed(self, territory):
if not self.graph.has_key(territory):
if territory not in self.graph:
self.graph[territory] = Territory(territory)

def validate(self):
Expand All @@ -121,9 +120,9 @@ def get_mapping(self):

@staticmethod
def flood_graph(graph):
start = graph[graph.keys()[0]]
visited = Set([start])
targets = Set(start.neighbours.values())
start = graph[list(graph.keys())[0]]
visited = set([start])
targets = set(start.neighbours.values())
while len(targets) > 0:
current = targets.pop()
if not current in visited:
Expand All @@ -133,7 +132,7 @@ def flood_graph(graph):
targets -= visited
if len(current.neighbours) <= 1:
risk.logger.warn("%s looks suspicious..." % current.name)
return Set(graph.values()) - visited
return set(graph.values()) - visited



Expand Down
56 changes: 28 additions & 28 deletions risk/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ def help_info(player, game_master):
"""
help - prints help
"""
risk.logger.debug(prompt_user.available_commands.keys())
print 'Available commands:'
risk.logger.debug(list(prompt_user.available_commands.keys()))
print('Available commands:')
for command in prompt_user.available_commands.values():
if command.__doc__:
print command.__doc__,
print
print(command.__doc__,)
print("")

def status_info(player, game_master):
print "Player [%s]: " % player.name
print("Player [%s]: " % player.name)
player_territories = game_master.player_territories(player)
display_user_armies(player, player_territories)

Expand All @@ -31,21 +31,21 @@ def next_info(player, game_master):
next - ends current phase
"""
risk.logger.debug('User finished phase')
print 'next'
print('next')

def attack_info(player, game_master, target_name, _, origin_name):
"""
attack [target] from [origin] - attack [target] from [origin], "from" is
needed in the command, player must own origin
"""
print player.name
print(player.name)
success = game_master.player_attack(player, origin_name, target_name)
if success:
print "successfully attacked %s!" % target_name
print("successfully attacked %s!" % target_name)
player.move_after_attack(game_master, origin_name, target_name)
else:
print "failed to attack %s... %s reduced to 1 army" % \
(target_name, origin_name)
print("failed to attack %s... %s reduced to 1 army" % \
(target_name, origin_name))


def fortify_info(player, game_master, destination_territory_name, _, armies, _2, origin_territory_name):
Expand All @@ -54,19 +54,19 @@ def fortify_info(player, game_master, destination_territory_name, _, armies, _2,
"""
origin_armies, destination_armies = game_master.player_move_armies(player, origin_territory_name, destination_territory_name, int(armies))
print "%s now has: %s armies" %(origin_territory_name, origin_armies)
print "%s now has: %s armies" %(destination_territory_name, destination_armies)
print("%s now has: %s armies" %(origin_territory_name, origin_armies))
print("%s now has: %s armies" %(destination_territory_name, destination_armies))

def map_info(player, game_master, continent=None):
"""
map - print ascii map for entire board
map [continent] - print ascii map for continent
map - print(ascii map for entire board
map [continent] - print(ascii map for continent
"""
risk.logger.debug('printing risk map!')
if continent:
map_printer(continent, player, game_master)
else:
for continent in risk.printer.ASCII_MAPS.keys():
for continent in list(risk.printer.ASCII_MAPS.keys()):
map_printer(continent, player, game_master)

def add_armies(player, game_master, number_of_armies, _, territory_name):
Expand All @@ -75,8 +75,8 @@ def add_armies(player, game_master, number_of_armies, _, territory_name):
needed in the command
"""
armies, reserves = game_master.player_add_army(player, territory_name, int(number_of_armies))
print "[%s] now has : [%s] units" %(territory_name, armies)
print "[%s] unit(s) on reserve" % (player.reserves)
print("[%s] now has : [%s] units" %(territory_name, armies))
print("[%s] unit(s) on reserve" % (player.reserves))

def quit_game(player, game_master):
risk.logger.debug('User wants to quit game')
Expand Down Expand Up @@ -127,8 +127,8 @@ def prompt_user(player, game_master, available_commands,
risk.logger.error('%s is not a valid command in the ' \
'reinforcement stage' % user_input)

risk.logger.debug(available_commands.keys())
print 'invalid command'
risk.logger.debug(list(available_commands.keys()))
print('invalid command')

def execute_command(command, player, game_master, *args):
try:
Expand All @@ -137,21 +137,21 @@ def execute_command(command, player, game_master, *args):
except (RiskGameError, ValueError, TypeError, IndexError, KeyError) as e:
risk.logger.error(str(e))
if command.__doc__:
print "usage: %s" % command.__doc__
print("usage: %s" % command.__doc__)
else:
print command
print user_input
print args
print(command)
print(user_input)
print(args)
risk.logger.warn("%s syntax error and no usage. "\
"User input: '%s', args: '%s'" %
(command, user_input, args))

def prompt_choose_territory(availables):
print "Available territories: "
print "---------------------------------------------------"
print availables.keys()
print "---------------------------------------------------"
return risk_ll_input('Choose from availables [empty input to reprint availables]: ')
print("Available territories: ")
print("---------------------------------------------------")
print(list(availables.keys()))
print("---------------------------------------------------")
return risk_ll_input('Choose from availables [empty input to reprint(availables]: ')

def user_input_finished(user_input):
quit_commands = ['quit', 'next']
Expand Down
2 changes: 1 addition & 1 deletion risk/errors/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from base import RiskGameError
from risk.errors.base import RiskGameError
7 changes: 3 additions & 4 deletions risk/game_master.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from sets import Set

import risk
import risk.logger
Expand Down Expand Up @@ -106,7 +105,7 @@ def generate_players(self, number_of_human_players, cli=False):
risk.logger.debug("Generating %s human players" % \
number_of_human_players)

for i in xrange(number_of_human_players):
for i in range(0,number_of_human_players):
if cli:
self.players.append(HumonRiskPlayer("Human %s" % i))
else:
Expand Down Expand Up @@ -177,11 +176,11 @@ def check_player_elimination(self, function, result, args):
def eliminate_player(self, player):
self.players.remove(player)
for _ in xrange(10):
print "%s eliminated!" % player.name
print("%s eliminated!" % player.name)

if len(self.players) <= 1:
for _ in xrange(100):
print "%s WINS!!!!" % self.current_player().name
print("%s WINS!!!!" % self.current_player().name)
self.end_game()

def set_phase(self, new_phase):
Expand Down
2 changes: 1 addition & 1 deletion risk/graphics/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from graphics import *
from risk.graphics import *
2 changes: 1 addition & 1 deletion risk/graphics/assets/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def get_result(self, poll_sleep):
self.drag_dialog()
elif self.finished_button.mouse_hovering(event.pos):
done = self.finished_button.confirmed_click()
#print done
#print(done
return self.current

def reset(self):
Expand Down
4 changes: 2 additions & 2 deletions risk/graphics/assets/territory.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ def draw(self):
dimension = font.size(str(self.count) * 2)
text_diagonal_length = math.sqrt(math.pow(dimension[0] / 2, 2) + math.pow(dimension[1], 2))
circle_radius = int(math.ceil(text_diagonal_length / 2))
#print circle_radius
#print dimension [0]
#print(circle_radius
#print(dimension [0]
self.surface = pygame.Surface([44, 44], pygame.SRCALPHA, 32)
self.surface = self.surface.convert_alpha()
pygame.draw.circle(self.surface, base.BLACK,
Expand Down
4 changes: 2 additions & 2 deletions risk/graphics/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def attack_success_move_armies(player, game_master, origin, target):
number_to_move)
done = True
except GameMasterError as e:
print e
print(e)
dialog.reset()
except ValueError:
# we really shouldn't get a parsing error from numeric dialog
Expand Down Expand Up @@ -373,7 +373,7 @@ def fortify_failed(player, origin, target, reason):
NotConnected: 'The selected territories are not connected!',
TerritoryNotOwnedByPlayer: "You do not own %s!" % target.name,
}
msg = expected[reason.__class__] if reason.__class__ in expected.keys() \
msg = expected[reason.__class__] if reason.__class__ in list(expected.keys()) \
else "Unkonwn reason for failure..."
dialog = PopupDialogAsset(DIALOG_X, DIALOG_Y, "Fortify Failed!", msg)
picasso.add_asset(POPUP_DIALOG_LAYER, dialog)
Expand Down
16 changes: 8 additions & 8 deletions risk/player/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,24 @@ def __init__(self, name):

def reinforce(self, game_master):
while self.reserves > 0:
print 'player has armies to deploy'
print "%s's territories: " % self.name
print "---------------------------------------------------"
print game_master.player_territories(self).keys()
print('player has armies to deploy')
print("%s's territories: " % self.name)
print("---------------------------------------------------")
print(list(game_master.player_territories(self).keys()))
risk.printer.display_user_armies(self,
game_master.player_territories(self))
commands.prompt_user(self, game_master,
reinforce_commands, HumonRiskPlayer._no_more_reserves)
def attack(self, game_master):
print "Attack phase, next to enter fortification phase"
print("Attack phase, next to enter fortification phase")
commands.prompt_user(self, game_master, attack_commands)

def fortify(self, game_master):
print "Fortification phase, next to end turn"
print("Fortification phase, next to end turn")
commands.prompt_user(self, game_master, fortify_commands)

def choose_territory(self, availables):
print "%s's turn..." % self.name
print("%s's turn..." % self.name)
return commands.prompt_choose_territory(availables)

def deploy_reserve(self, game_master, max_deploys=0):
Expand Down Expand Up @@ -88,7 +88,7 @@ def move_after_attack(self, game_master, origin_name, target_name):
armies_to_move = None
while armies_to_move < 1 or armies_to_move >= origin.armies:
if armies_to_move < 1 or armies_to_move >= origin.armies:
print "You can't move %s armies!" % armies_to_move
print("You can't move %s armies!" % armies_to_move)
armies_to_move = input("How many armies will you move? >>> ")
origin_armies, destination_armies = game_master.player_move_armies(self, origin_name, target_name, int(armies_to_move))
risk.logger.debug("%s now has: %s armies" %(origin_name, origin_armies))
Expand Down
Loading

0 comments on commit 9a0d885

Please sign in to comment.