Changeset 8
- Timestamp:
- 08/24/2016 03:19:36 AM (8 years ago)
- branch-nick:
- main
- parents:
Rev Tree Chgset 7 @7 [7] 6.1.60 @6.1.60 [6.1.60] - revision id:
- dsowen@fugue88.ws-20160824031936-f6h8zejvnpcuqr4z
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main.py
- Property executable set to True
r7 r6.1.60 1 #!/usr/bin/python3 2 1 3 from random import shuffle 2 4 … … 32 34 else: 33 35 return 1 36 37 38 class Shared(object): 39 40 def __init__(self, player_count, players, role_deck): 41 self.player_count = player_count 42 self.players = players 43 self.role_deck = role_deck 44 self.mission = 0 45 self.captain = 0 46 self.team_failures = 0 47 self.wins = 0 48 self.losses = 0 49 50 def current_mission_size(self): 51 team_size = mission_size(self.player_count, self.mission) 52 print("Mission {} needs {} members".format(self.mission, team_size)) 53 return team_size 54 55 def next_mission(self): 56 self.mission += 1 57 58 def get_proposed_team(self, team_size): 59 print("Captain is {}".format(self.captain)) 60 team = self.players[self.captain].propose_team(team_size) 61 print("He proposed {}".format(team)) 62 return team 63 64 def next_team_captain(self): 65 self.captain = (self.captain + 1) % self.player_count 66 67 def reset_team_failures(self): 68 self.team_failures = 0 69 70 def record_team_failure(self): 71 self.team_failures += 1 72 73 def get_mission_failures(self, team): 74 failures = 0 75 for i in team: 76 if self.role_deck[i] == "SPY": 77 if not self.players[i].perform_mission(): 78 failures = failures + 1 79 return failures 80 81 def required_failures(self): 82 return required_failures(self.player_count, self.mission) 83 84 def mission_succeeded(self): 85 print("Mission succeeded") 86 self.wins += 1 87 88 def mission_failed(self, failures): 89 print("Mission had {} failures".format(failures)) 90 self.losses += 1 91 92 def game_over(self): 93 if self.team_failures == 5: 94 print("The spies won!") 95 for p in self.players: 96 p.observe_game(False) 97 return True 98 elif self.wins >= 3: 99 print("The Resistance won!") 100 for p in self.players: 101 p.observe_game(True) 102 return True 103 elif self.losses >= 3: 104 print("The Spies won!") 105 for p in self.players: 106 p.observe_game(False) 107 return True 108 else: 109 return False 110 111 112 class AdvanceMission(object): 113 114 def __init__(self, shared): 115 self.shared = shared 116 117 def do(self): 118 self.shared.next_mission() 119 team_size = self.shared.current_mission_size() 120 return AssembleTeam(self.shared, team_size) 121 122 123 class AssembleTeam(object): 124 125 def __init__(self, shared, team_size): 126 self.shared = shared 127 self.team_size = team_size 128 129 def do(self): 130 team = self.shared.get_proposed_team(self.team_size) 131 132 votes = [p.approve_team(team) for p in self.shared.players] 133 approvals = sum(votes) 134 approved = approvals > self.shared.player_count / 2 135 136 print("Team {}: {} for, {} against".format( 137 {True: "approved", False: "rejected"}[approved], 138 approvals, self.shared.player_count - approvals 139 )) 140 141 for p in self.shared.players: 142 p.observe_team_vote(approved, votes) 143 144 self.shared.next_team_captain() 145 146 if approved: 147 self.shared.reset_team_failures() 148 return PerformMission(self.shared, team) 149 else: 150 self.shared.record_team_failure() 151 return AssembleTeam(self.shared, self.team_size) 152 153 154 class PerformMission(object): 155 156 def __init__(self, shared, team): 157 self.shared = shared 158 self.team = team 159 160 def do(self): 161 failures = self.shared.get_mission_failures(self.team) 162 success = failures < self.shared.required_failures() 163 164 for p in self.shared.players: 165 p.observe_mission(success, failures) 166 167 if success: 168 self.shared.mission_succeeded() 169 else: 170 self.shared.mission_failed(failures) 171 172 return AdvanceMission(self.shared) 34 173 35 174 … … 51 190 players[i].reveal_spies(spy_nums) 52 191 53 wins = 0 54 losses = 0 55 captain = 0 56 team_failures = 0 57 58 for mission in range(1, 6): 59 team_size = mission_size(player_count, mission) 60 print("Mission {} needs {} members".format(mission, team_size)) 61 for attempt in range(5): 62 print("Captain is {}".format(captain)) 63 team = players[captain].propose_team(team_size) 64 print("He proposed {}".format(team)) 65 votes = [p.approve_team(team) for p in players] 66 approvals = sum(votes) 67 approved = approvals > player_count / 2 68 print("Team {}: {} for, {} against".format( 69 {True: "approved", False: "rejected"}[approved], 70 approvals, player_count - approvals 71 )) 72 73 for p in players: 74 p.observe_team_vote(approved, votes) 75 76 captain = (captain + 1) % player_count 77 if approved: 78 team_failures = 0 79 failures = 0 80 for i in team: 81 if role_deck[i] == "SPY": 82 if not players[i].perform_mission(): 83 failures = failures + 1 84 success = failures < required_failures(player_count, mission) 85 86 for p in players: 87 p.observe_mission(success, failures) 88 89 if success: 90 print("Mission succeeded") 91 wins += 1 92 if wins >= 3: 93 print("The Resistance won!") 94 for p in players: 95 p.observe_game(True) 96 return 97 else: 98 print("Mission had {} failures".format(failures)) 99 losses += 1 100 if losses >= 3: 101 print("The Spies won!") 102 for p in players: 103 p.observe_game(False) 104 return 105 106 break 107 else: 108 team_failures += 1 109 if team_failures == 5: 110 print("The spies won!") 111 for p in players: 112 p.observe_game(False) 113 return 192 shared = Shared(player_count, players, role_deck) 193 state = AdvanceMission(shared) 194 195 while not shared.game_over(): 196 state = state.do() 114 197 115 198
Note: See TracChangeset
for help on using the changeset viewer.