Advertisement
demiurg_906

Game (group 2)

Nov 16th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.62 KB | None | 0 0
  1. class Player:
  2.     def __init__(self, health, action_points, damage, x, y, name, marker):
  3.         self.x = x
  4.         self.y = y
  5.         self.health = health
  6.         self.action_points = action_points
  7.         self.maximum_action_points = action_points
  8.         self.name = name
  9.         self.marker = marker
  10.         self.damage = damage
  11.  
  12.     def hit(self, enemy):
  13.         enemy.health -= self.damage
  14.         self.action_points -= 1
  15.  
  16.     def move(self, x, y):
  17.         self.x = x
  18.         self.y = y
  19.         self.action_points -= 1
  20.  
  21.     def is_alive(self):
  22.         return self.health > 0
  23.  
  24.     def has_action_points(self):
  25.         return self.action_points > 0
  26.  
  27.     def restore_action_points(self):
  28.         self.action_points = self.maximum_action_points
  29.  
  30.  
  31. class Game:
  32.     cells = {0: '#', 1: ' ', 2: '~', 3: '`'}
  33.     cells_rev = {value: key for key, value in cells.items()}
  34.     directions = {
  35.         'w': (-1, 0),
  36.         's': (1, 0),
  37.         'a': (0, -1),
  38.         'd': (0, 1)
  39.     }
  40.  
  41.     def __init__(self, map, player_1: Player, player_2: Player, levers):
  42.         self.map = map
  43.         self.player_1 = player_1
  44.         self.player_2 = player_2
  45.         self.levers = levers
  46.         self.portals = []  # хранит пары координат
  47.  
  48.         n = len(self.map)
  49.         m = len(self.map[0])
  50.         for x in range(n):
  51.             for y in range(m):
  52.                 if map[x][y] == 2:
  53.                     self.portals.append((x, y))
  54.  
  55.     def print_map(self):
  56.         n = len(self.map)
  57.         m = len(self.map[0])
  58.         for x in range(n):
  59.             for y in range(m):
  60.                 if x == self.player_1.x and y == self.player_1.y:
  61.                     print(self.player_1.marker, end='')
  62.                 elif x == self.player_2.x and y == self.player_2.y:
  63.                     print(self.player_2.marker, end='')
  64.                 else:
  65.                     cell = self.map[x][y]
  66.                     print(self.cells[cell], end='')
  67.             print()
  68.  
  69.     def next_cell(self, direction, player: Player):
  70.         if direction not in self.directions:
  71.             raise ValueError('Некорректное направление: ' + direction)
  72.         # direction -- w, s, a или d
  73.         dx, dy = self.directions[direction]
  74.         return player.x + dx, player.y + dy
  75.  
  76.     def process_move(self, command, player: Player, other_player: Player):
  77.         # w s a d
  78.         # if command not in 'wsad':
  79.         # if command not in ['w', 's', 'a', 'd']:
  80.         x, y = self.next_cell(command, player)
  81.         if self.map[x][y] == 0 or x == other_player.x and y == other_player.y:
  82.             return
  83.         player.move(x, y)
  84.  
  85.     def process_command(self, command, player: Player, other_player: Player):
  86.         # command: 'w|s|a|d' -- process_move
  87.         # command: 'h w|s|a|d' -- process_hit
  88.         # command.split() = ['w|s|a|d']
  89.         # command.split() = ['h', 'w|s|a|d']
  90.         try:
  91.             # write tour code here
  92.             commands = command.split()
  93.             if len(commands) == 0:
  94.                 return
  95.             if commands[0] == 'u':
  96.                 self.process_use(player)
  97.             elif commands[0] == 'h':
  98.                 self.process_hit(commands[1], player, other_player)
  99.             else:
  100.                 self.process_move(command, player, other_player)
  101.         except ValueError as e:
  102.             print(e)
  103.  
  104.     def process_use(self, player: Player):
  105.         cell = self.map[player.x][player.y]
  106.         if cell == 2:
  107.             for (x, y) in self.portals:
  108.                 if x != player.x or y != player.y:
  109.                     player.move(x, y)
  110.                     break
  111.         elif cell == 3:
  112.             pass
  113.  
  114.  
  115.     def process_hit(self, command, player: Player, other_player: Player):
  116.         x, y = self.next_cell(command, player)
  117.         if x == other_player.x and y == other_player.y:
  118.             player.hit(other_player)
  119.  
  120.     def print_info(self, player: Player):
  121.         print('{} ({}) has {} HP and {} AP'.format(
  122.             player.name,
  123.             player.marker,
  124.             player.health,
  125.             player.action_points
  126.         ))
  127.         pass
  128.  
  129.     def one_step(self):
  130.         # player 1
  131.         while self.player_1.has_action_points():
  132.             self.print_map()
  133.             self.print_info(self.player_1)
  134.             self.print_info(self.player_2)
  135.             command = input()
  136.             self.process_command(command, self.player_1, self.player_2)
  137.  
  138.         # player 2
  139.         while self.player_2.has_action_points():
  140.             self.print_map()
  141.             self.print_info(self.player_2)
  142.             self.print_info(self.player_1)
  143.             command = input()
  144.             self.process_command(command, self.player_2, self.player_1)
  145.  
  146.         self.player_1.restore_action_points()
  147.         self.player_2.restore_action_points()
  148.  
  149.  
  150. def map_converter(string_map):
  151.     return [[Game.cells_rev[cell] for cell in row] for row in string_map.split('\n')]
  152.  
  153.  
  154. string_map = '''
  155. ###################
  156. #  #             ~#
  157. #~   #      ####  #
  158. #  # #            #
  159. #    ##    #   #  #
  160. #  #           #  #
  161. #  # ##    #   #  #
  162. #`    #    #      #
  163. ###################
  164. '''.strip()
  165.  
  166. game_map = map_converter(string_map)
  167.  
  168. player_1 = Player(
  169.     name='John',
  170.     marker='@',
  171.     damage=1,
  172.     health=3,
  173.     action_points=20,
  174.     x=1,
  175.     y=1
  176. )
  177.  
  178. player_2 = Player(
  179.     name='Peter',
  180.     marker='$',
  181.     damage=1,
  182.     health=3,
  183.     action_points=1,
  184.     x=1,
  185.     y=2
  186. )
  187.  
  188. levers = {
  189.     (7, 1): (6, 4)
  190. }
  191. game = Game(game_map, player_1, player_2, levers)
  192. game.print_map()
  193. while True:
  194.     game.one_step()
  195. # https://pastebin.com/XYjXtp3K
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement