/ui.py

https://github.com/Shaywei/Innovation · Python · 157 lines · 146 code · 3 blank · 8 comment · 0 complexity · 62a7069407ff7f570c937262b3c5255a MD5 · raw file

  1. '''
  2. Created on 18/10/2011
  3. @author: lost_dm
  4. This class implements a UI for the game.
  5. '''
  6. import player
  7. from intput import intput
  8. import os
  9. class UI:
  10. def __init__(self, thegame):
  11. self.thegame = thegame
  12. self.players = []
  13. '''self.root = Tk()
  14. self.root.title("Innovation")
  15. '''
  16. def make_valid_choices_from_int(self, j):
  17. '''Gets integer j and returns a set {1, 2, 3, ... , j}'''
  18. valid_choices = set()
  19. for i in range(j): valid_choices.add(i+1)
  20. return valid_choices
  21. def prompt(self, msg, valid_options):
  22. choice = None
  23. while choice not in valid_options:
  24. choice = intput(msg)
  25. return choice
  26. def str_prompt(self, msg, valid_options):
  27. choice = None
  28. while choice not in valid_options:
  29. choice = input(msg)
  30. return choice
  31. def choose_number_of_players(self):
  32. '''frame = Frame(self.root)
  33. frame.pack()
  34. for i in range(4):
  35. self.button = Button(frame, text="{0} player".format(str(i+1)), command=self.make_assign_number_of_players(i+1))
  36. self.button.pack(side=LEFT)'''
  37. number_of_players = self.prompt('Choose number of players (1-4): ', {1,2,3,4})
  38. return number_of_players
  39. def assign_players(self, players):
  40. self.players = players
  41. def reveal(self, card_to_reveal):
  42. print('Revealing:')
  43. card_to_reveal.print_self()
  44. def get_player_name(self):
  45. name = ''
  46. while name == '':
  47. name = input('Enter Player\'s name (must be non-empty):')
  48. return name
  49. def get_player_action(self, player_name):
  50. print('{0}, points Choose an action:'.format(player_name))
  51. print()
  52. print('1. Draw')
  53. print('2. Meld')
  54. print('3. Dogma')
  55. print('4. Achieve')
  56. print('Does not count as an action:')
  57. print('5. Show hand.')
  58. print('6. Show the melded cards of some color.')
  59. print('7. Show symbol count.')
  60. print()
  61. action = self.prompt('Choose an action (1-7): ', {1,2,3,4,5,6,7,99})
  62. print()
  63. os.system("cls")
  64. return action
  65. def get_color(self):
  66. color = self.prompt('Choose color: Red - 0, Blue - 1, Yellow - 2, Purple - 3, Green - 4' , {1,2,3,4,0})
  67. return color
  68. def may(self, action_desc):
  69. choice = self.str_prompt('You may choose to execute:\n' + action_desc +'\n Execute? (y/n)' , {'y','n'})
  70. if choice == 'y':
  71. return True
  72. elif choice == 'n':
  73. return False
  74. def player_choose_top_card(self, player):
  75. #TODO: What if all piles are empty?
  76. print('Your top cards:')
  77. j = 0
  78. tmp_dict = {}
  79. for i in range(5):
  80. top_card = player.get_top_card_reference_from_pile(i)
  81. if top_card is not None:
  82. j += 1
  83. tmp_dict[j] = i
  84. print('choice {0}:'.format(str(j)))
  85. top_card.print_self()
  86. print()
  87. valid_choices = self.make_valid_choices_from_int(j)
  88. choice = self.prompt('Choose top card from valid choices: ' , valid_choices)
  89. return tmp_dict[choice]
  90. def choose_card_from_hand(self, hand_size):
  91. valid_choices = self.make_valid_choices_from_int(hand_size)
  92. card_index_choice = self.prompt('Choose a card from your hand (one of the above): ', valid_choices) - 1
  93. return card_index_choice
  94. def print_card_list(self, list):
  95. ''' This method prints a card list. '''
  96. assert len(list)>0 , 'list is empty'
  97. for i in range(len(list)):
  98. print('{0}. '.format(i+1),)
  99. list[i].print_self()
  100. print()
  101. print()
  102. def choose_card_from_list(self, list):
  103. self.print_card_list(list)
  104. valid_choices = self.make_valid_choices_from_int(len(list))
  105. card_index_choice = self.prompt('Choose a card (one of the above): ', valid_choices)
  106. return list[card_index_choice - 1]
  107. def choose_color_from_list(self, list):
  108. #TODO: TEST and GUI
  109. if list == []: return None
  110. for i in range(len(list)):
  111. print('{0}. {1}'.format(i+1, list[i]),)
  112. valid_choices = self.make_valid_choices_from_int(len(list))
  113. valid_choices.add(0)
  114. choice = self.prompt('Choose a color (one of the above) or 0 for None: ', valid_choices)
  115. if choice == 0: return None
  116. return list[choice - 1]
  117. def choose_player(self, list_of_players):
  118. ''' This method recieves a list of players, takes a choice from current player and return a reference to the player chosen. A post condition is that list of players is not empty!'''
  119. for i in range(len(list)):
  120. print('{0}. {1}'.format(i+1, list[i].name),)
  121. valid_choices = self.make_valid_choices_from_int(len(list))
  122. choice = self.prompt('Choose a color (one of the above): ', valid_choices)
  123. return list_of_players[choice - 1]
  124. '''def make_assign_number_of_players(self, num):
  125. def assign_number_of_players():
  126. print('Players {0}'.format(str(num)))
  127. self.number_of_players = num
  128. return assign_number_of_players'''