def take_action(self, context, cv, room_id=None, play_id=None): notice('Please choose 2 player to be couples.\n(type 2 numbers split by space)', room_id, play_id) ans = [] stage = self.get_stage() + ':' + str(play_id) cv.acquire() while True: if stage not in context: cv.wait() else: if len(context[stage]) < 2: notice('Please choose 2 players', room_id, play_id) del context[stage] cv.wait() elif context[stage][0] == context[stage][1]: notice('Please choose 2 different player', room_id, play_id) del context[stage] cv.wait() else: ans = context[stage][0:2] del context[stage] break notice('You are couple. The other is No. ' + str(ans[1]), room_id, ans[0]) notice('You are couple. The other is No. ' + str(ans[0]), room_id, ans[1]) cv.release() return ans
def game_begin(room_id): global _ROOMS, _USERS if _ROOMS[room_id].get_usernum() != _ROOMS[room_id].get_peoplenum(): alert('someone doesn\'t enter', room_id, 0) return notice('game start!\n', room_id) for i in range((_ROOMS[room_id].get_usernum())): user = _USERS[_ROOMS[room_id].get_user(i)] notice("username: "******" id: " + str(i), room_id) _ROOMS[room_id].start()
def __handle_dead(self, deads): deads = list(map(int, deads)) for dead in deads: if self.__couple is not None and dead in self.__couple: deads += self.__couple self.__couple = None deads = list(set(deads)) for dead in deads: deadnote(dead, self._room_id, dead) self._list[dead].dead() iswin, win = self.__is_win() if iswin: notice('Game ends. ' + str(win) + ' win!', self._room_id) return True n = [] for dead in deads: if isinstance(self._list[dead], Hunter): self._stage = self._list[dead].get_stage() n += self._list[dead].dead_action(self._context, self._cv, self._room_id, dead) if len(n) == 1: if self.__couple is not None and n[0] in self.__couple: if n[0] == self.__couple[0]: n += self.__couple[1] else: n += self.__couple[0] self.__couple = None for dead in n: deadnote(dead, self._room_id, dead) self._list[dead].dead() deads += n iswin, win = self.__is_win() if iswin: notice('Game ends. ' + str(win) + ' win!', self._room_id) return True else: if self.__cop in deads: notice('cop dead. Must set a new cop', self._room_id) self._stage = 'setting cop' self.__cop = self._list[self.__cop].vote( self._stage, self._context, self._cv, self._room_id, self.__cop) notice('new cop is No.' + str(self.__cop), self._room_id) return False
def take_action(self, context, cv, room_id=None, play_id=None): ans = [] dead = context['to be dead'] notice('No. ' + str(dead) + ' dead tonight.', room_id, play_id) stage = self.get_stage() + ":" + str(play_id) cv.acquire() del context['to be dead'] if dead is not None and self.__antidote: notice('Do you want save his life(0 for no,1 for yes)', room_id, play_id) while True: if stage not in context: cv.wait() else: if context[stage][0] == 1: self.__antidote = False ans.append(1) del context[stage] break elif context[stage][0] == 0: ans.append(0) del context[stage] break else: notice('(0 for no,1 for yes)', room_id, play_id) del context[stage] else: ans.append(0) if self.__poison: notice( 'You have a poison,Do you want to kill someone? ( -1 for nobody)', room_id, play_id) while True: if stage not in context: cv.wait() else: if context[stage][0] == -1: ans.append(-1) del context[stage] break else: ans.append(context[stage][0]) self.__poison = False del context[stage] break else: ans.append(-1) cv.release() return ans
def dead_action(self, context, cv, room_id=None, play_id=None): notice('Please choose one person to die (-1 for no one)!', room_id, play_id) stage = self.get_stage() + ":" + str(play_id) ans = [] cv.acquire() while True: if stage not in context: cv.wait() else: if context[stage][0] != -1: ans = context[stage][0:1] del context[stage] break cv.release() return ans
def take_action(self, context, cv, room_id=None, play_id=None): notice('Please choose one person to dectect !', room_id, play_id) stage = self.get_stage() + ":" + str(play_id) ans = [] cv.acquire() while True: if stage not in context: cv.wait() else: ans = context[stage][0:1] del context[stage] break cv.release() return ans
def take_action(self, context, cv, room_id=None, play_id=None): ans = [] dead = context['to be dead'] notice('No. ' + str(dead) + ' dead tonight.', room_id, play_id) stage = self.get_stage() + ":" + str(play_id) cv.acquire() del context['to be dead'] if dead is not None and self.__antidote: notice('Do you want save his life(0 for no,1 for yes)', room_id, play_id) while True: if stage not in context: cv.wait() else: if context[stage][0] == 1: self.__antidote = False ans.append(1) del context[stage] break elif context[stage][0] == 0: ans.append(0) del context[stage] break else: notice('(0 for no,1 for yes)', room_id, play_id) del context[stage] else: ans.append(0) if self.__poison: notice('You have a poison,Do you want to kill someone? ( -1 for nobody)', room_id, play_id) while True: if stage not in context: cv.wait() else: if context[stage][0] == -1: ans.append(-1) del context[stage] break else: ans.append(context[stage][0]) self.__poison = False del context[stage] break else: ans.append(-1) cv.release() return ans
def take_action(self, context, cv, room_id=None, play_id=None): notice('Please kill one person discussing with your teammate!(-1 for don\'t kill anyone)', room_id, play_id) ans = [] stage = self.get_stage() + ":" + str(play_id) cv.acquire() while True: if stage not in context: cv.wait() else: ans = context[stage][0:1] del context[stage] break cv.release() return ans
def take_action(self, context, cv, room_id=None, play_id=None): notice( 'Please kill one person discussing with your teammate!(-1 for don\'t kill anyone)', room_id, play_id) ans = [] stage = self.get_stage() + ":" + str(play_id) cv.acquire() while True: if stage not in context: cv.wait() else: ans = context[stage][0:1] del context[stage] break cv.release() return ans
def take_action(self, context, cv, room_id=None, play_id=None): notice('Please choose the person you want to guard!', room_id, play_id) stage = self.get_stage() + ":" + str(play_id) cv.acquire() while True: if stage not in context: cv.wait() else: if self.__guard == context[stage][0]: notice("You can't guard one person in consecutive 2 rounds", room_id, play_id) del context[stage] cv.wait() else: self.__guard = context[stage][0] del context[stage] break cv.release() return [self.__guard]
def next_day(self): # vote for cop if self.__turn == 1: self._stage = 'cop' play_ids = [] selfs = [] for i in range(len(self._list)): if self._list[i].is_alive(): play_ids.append(i) selfs.append(self._list[i]) while True: ans = self.__vote([x.vote for x in selfs], play_ids, [1 for _ in range(len(selfs))], False, self._stage) if len(ans) != 1: for play_id in play_ids: notice('Tie for' + str(ans) + ' . Please revote', self._room_id, play_id) else: self.__cop = ans[0] for play_id in play_ids: notice('Cop is No.' + str(self.__cop), self._room_id, play_id) break self._stage = 'dead' play_ids = [] selfs = [] weights = [] for i in range(len(self._list)): if self._list[i].is_alive(): play_ids.append(i) selfs.append(self._list[i]) if i == self.__cop: weights.append(3) else: weights.append(2) while True: ans = self.__vote([x.vote for x in selfs], play_ids, weights, False, self._stage) if len(ans) != 1: for play_id in play_ids: notice('Tie for' + str(ans) + ' . Please revote', self._room_id, play_id) else: return self.__handle_dead(ans) return False
def next_night(self): # wolf killing wolves = self.__get_characters(Wolf) self._stage = wolves[0][0].get_stage() play_ids = [] selfs = [] dead = None for wolf in wolves: if wolf[0].is_alive(): play_ids.append(wolf[1]) selfs.append(wolf[0]) deads = self.__vote([x.take_action for x in selfs], play_ids, [1 for _ in range(len(selfs))], True) if len(deads) == 0 or deads[0] == -1: for wolf in wolves: if wolf[0].is_alive(): notice('No one dead tonight', self._room_id, wolf[1]) else: dead = deads[0] # guard round guard = self.__get_characters(Guard) if len(guard) == 1: if guard[0][0].is_alive(): self._stage = guard[0][0].get_stage() guard[0][0].take_action(self._context, self._cv, self._room_id, guard[0][1]) else: guard[0][0].fate_action(self._room_id) # prophet round prophet = self.__get_characters(Prophet) if len(prophet) == 1: if prophet[0][0].is_alive(): self._stage = prophet[0][0].get_stage() ans = prophet[0][0].take_action(self._context, self._cv, self._room_id, prophet[0][1]) if ans is not None: if isinstance(self._list[ans[0]], Wolf): notice('He is a bad man!', self._room_id, prophet[0][1]) else: notice('He is a good man!', self._room_id, prophet[0][1]) else: prophet[0][0].fate_action(self._room_id) # witch round witch = self.__get_characters(Witch) if len(witch) == 1: if witch[0][0].is_alive(): self._stage = witch[0][0].get_stage() self._context['to be dead'] = dead ans = witch[0][0].take_action(self._context, self._cv, self._room_id, witch[0][1]) else: witch[0][0].fate_action(self._room_id) ans = [0, -1] else: ans = [0, -1] # final calculate g = guard[0][0].get_guardee() if len(guard) == 1 else -1 self._context.clear() if (dead is None or g == dead or ans[0] == 1) and ans[1] == -1: notice("No one die tonight!", self._room_id) return False elif g == dead or ans[0] == 1: return self.__handle_dead([ans[1]]) elif ans[1] != -1: return self.__handle_dead([dead, ans[1]]) else: return self.__handle_dead([dead])