Advertisement
Guest User

Untitled

a guest
May 1st, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. '''
  2. This defines an object to keep track of the game
  3.  
  4. This allows us to abstract the game logic from the rendering
  5. '''
  6. from board import Board
  7. from key import Team
  8. from key import Orientation
  9. import enum
  10. import itertools
  11.  
  12.  
  13. class States(enum.Enum):
  14. gold_play = 0
  15. silver_play = 1
  16. gold_respawning = 2
  17. silver_respawning = 3
  18. game_over = 4
  19. gold_piece_selected = 5
  20. silver_piece_selected = 6
  21.  
  22. class GameState(object):
  23. '''
  24. defines game actions and keeps track of the state of the game
  25. '''
  26. def __init__(self):
  27. '''
  28. Default constructor
  29. '''
  30. self._gameboard = Board.default_board()
  31. self._state = States.gold_play
  32. #_piece_selected is a location tuple or None
  33. self._piece_selected = None
  34.  
  35. def is_a_team_respawning(self):
  36. '''
  37. returns True if a team is currently responding
  38. '''
  39. if self._state == States.gold_respawning:
  40. return True
  41. elif self._state == States.silver_respawning:
  42. return True
  43. else:
  44. return False
  45.  
  46. def get_possible_moves_of_key(self, location):
  47. '''
  48. returns a list of tuples that represent the allowable moves of a key
  49. at the given location
  50.  
  51. returns an empty list if there is no piece at the location
  52. '''
  53. piece = self._gameboard.get_piece_at_location(location)
  54. if not piece:
  55. return []
  56.  
  57. possible_moves = []
  58.  
  59. #we create a small lambda function depending on orientation of the key
  60. #this says how we step through the locations of the board
  61. if piece.orientation == Orientation.north:
  62. stepper_function = lambda x, y: (x, y+1)
  63. elif piece.orientation == Orientation.northeast:
  64. stepper_function = lambda x, y: (x+1, y+1)
  65. elif piece.orientation == Orientation.east:
  66. stepper_function = lambda x, y: (x+1, y)
  67. elif piece.orientation == Orientation.southeast:
  68. stepper_function = lambda x, y: (x+1, y-1)
  69. elif piece.orientation == Orientation.south:
  70. stepper_function = lambda x, y: (x, y-1)
  71. elif piece.orientation == Orientation.southwest:
  72. stepper_function = lambda x, y: (x-1, y-1)
  73. elif piece.orientation == Orientation.west:
  74. stepper_function = lambda x, y: (x-1, y)
  75. elif piece.orientation == Orientation.northwest:
  76. stepper_function = lambda x, y: (x-1, y+1)
  77.  
  78. #uses 0 and 1 as indexes even though a location will usually
  79. #have attributes accessable through loc.x and loc.y
  80. possible_location = stepper_function(piece.location[0],
  81. piece.location[1])
  82.  
  83. while self._gameboard.is_location_in_bounds(possible_location):
  84. piece_at_location = self._gameboard.get_piece_at_location(location)
  85.  
  86. if not piece_at_location:
  87. possible_moves.append(possible_location)
  88. elif piece_at_location.team = piece.team
  89. break
  90. else:
  91. possible_moves.append(possible_location)
  92. break
  93.  
  94. possible_location = stepper_function(possible_location[0],
  95. possible_location[1])
  96.  
  97. return possible_moves
  98.  
  99. def get_rotation_points_of_key(self, location):
  100. '''
  101. returns a list of tuples that represent the allowable rotation points
  102. of a key at a given location
  103.  
  104. returns an empty list of there is no piece at the location
  105. '''
  106. piece = self._gameboard.get_piece_at_location(location)
  107.  
  108. if not piece:
  109. return []
  110.  
  111. modifiers = (-1, 0, 1)
  112. rotation_points = []
  113.  
  114. for x_modifier in modifiers:
  115. for y_modifier in modifiers:
  116. rotation_points.append((piece.location[0] + x_modifier,
  117. piece.location[1] + y_modifier))
  118.  
  119.  
  120. rotation_points = itertools.ifilter(self._gameboard.is_location_in_bounds,
  121. rotation_points)
  122.  
  123. #we eliminate the possible moves from the rotation points
  124. possible_moves = self.get_possible_moves_of_key(location)
  125. rotation_points = [i for i in rotation_points if i not in possible_moves]
  126.  
  127. return rotation_points
  128.  
  129. def location_clicked(self, location):
  130. '''
  131. performs the proper action if a location is clicked
  132.  
  133. Will be a large method unless I can break it better
  134. '''
  135. #If we have a team playing then we allow for selecting a piece to move
  136. if self._state == States.gold_play:
  137. if self._gameboard.is_gold_piece_at_location(location):
  138. self._piece_selected = location
  139. self._state = States.gold_piece_selected
  140.  
  141. elif self._state == States.silver_play:
  142. if self.is_silver_piece_at_location(location):
  143. self._piece_selected = location
  144. self._state = States.silver_piece_selected
  145.  
  146. #if a piece is selected already we have to handle moving
  147. elif self._state == States.gold_piece_selected:
  148. if location in self.get_possible_moves_of_key(self._piece_selected):
  149. if self._gameboard.is_piece_at_location(location):
  150. pass
  151. if self._gameboard.is_locked_piece_at_location(location):
  152. locked_piece = self._gameboard.get_piece_at_location(location)
  153. else:
  154. self._gameboard.move_piece_to_location()
  155. else:
  156. self._piece_selected = None
  157. self._state = States.gold_play
  158.  
  159. elif self._state == States.silver_piece_selected:
  160. pass
  161. elif self._state == States.gold_respawning:
  162. pass
  163. elif self._state == States.silver_respawning:
  164. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement