def test_returns_tie_for_straights(self): players = [Player(), Player()] game = Game(players) game.community.append(Card(suit=Suit.CLUB, number = 9)) game.community.append(Card(suit=Suit.CLUB, number = 10)) game.community.append(Card(suit=Suit.CLUB, number = 11)) game.community.append(Card(suit=Suit.CLUB, number = 12)) game.community.append(Card(suit=Suit.DIAMOND, number = 13)) player1 = game.players[0] player1.hand.append(Card(suit=Suit.DIAMOND, number = 2)) player1.hand.append(Card(suit=Suit.DIAMOND, number = 3)) player2 = game.players[1] player2.hand.append(Card(suit=Suit.DIAMOND, number = 4)) player2.hand.append(Card(suit=Suit.DIAMOND, number = 5)) players = [player1, player2] game.set_player_hands() winners = game.break_tie(players) self.assertTrue(player1 in winners) self.assertTrue(player2 in winners) self.assertEqual(player1.best_hand['score'], Hand.STRAIGHT) self.assertEqual(player2.best_hand['score'], Hand.STRAIGHT)
def test_get_winning_player_returns_both_players_with_true_tie(self): players = [Player(), Player()] game = Game(players) #community cards game.community.append(Card(suit=Suit.CLUB, number=2)) game.community.append(Card(suit=Suit.DIAMOND, number=11)) game.community.append(Card(suit=Suit.SPADE, number=5)) game.community.append(Card(suit=Suit.HEART, number=6)) game.community.append(Card(suit=Suit.DIAMOND, number=7)) #player1 cards player1 = game.players[0] player1.hand.append(Card(suit=Suit.CLUB, number=8)) player1.hand.append(Card(suit=Suit.CLUB, number=9)) #player2 cards player2 = game.players[1] player2.hand.append(Card(suit=Suit.DIAMOND, number=8)) player2.hand.append(Card(suit=Suit.DIAMOND, number=9)) game.set_player_hands() winners = game.get_winning_player() self.assertTrue(player1 in winners) self.assertTrue(player2 in winners)
def test_flush_breaker_returns_single_player(self): players = [Player(), Player(), Player()] game = Game(players) game.community.append(Card(suit=Suit.CLUB, number = 8)) game.community.append(Card(suit=Suit.CLUB, number = 14)) game.community.append(Card(suit=Suit.CLUB, number = 9)) game.community.append(Card(suit=Suit.CLUB, number = 10)) game.community.append(Card(suit=Suit.CLUB, number = 13)) player1 = game.players[0] player1.hand.append(Card(suit=Suit.CLUB, number = 2)) player1.hand.append(Card(suit=Suit.CLUB, number = 14)) player2 = game.players[1] player2.hand.append(Card(suit=Suit.CLUB, number = 4)) player2.hand.append(Card(suit=Suit.CLUB, number = 5)) player3 = game.players[2] player3.hand.append(Card(suit=Suit.CLUB, number = 6)) player3.hand.append(Card(suit=Suit.DIAMOND, number = 7)) players = [player1, player2, player3] game.set_player_hands() winners = game.break_tie(players) self.assertTrue(player1 in winners) self.assertFalse(player2 in winners) self.assertFalse(player3 in winners) self.assertEqual(player1.best_hand['score'], Hand.FLUSH) self.assertEqual(player2.best_hand['score'], Hand.FLUSH) self.assertEqual(player3.best_hand['score'], Hand.FLUSH)
def test_returns_winners_for_tied_pair(self): players = [Player(), Player()] game = Game(players) game.community.append(Card(suit=Suit.CLUB, number = 12)) game.community.append(Card(suit=Suit.DIAMOND, number = 12)) game.community.append(Card(suit=Suit.SPADE, number = 11)) game.community.append(Card(suit=Suit.HEART, number = 11)) game.community.append(Card(suit=Suit.DIAMOND, number = 11)) player1 = game.players[0] player1.hand.append(Card(suit=Suit.DIAMOND, number = 14)) player1.hand.append(Card(suit=Suit.DIAMOND, number = 3)) player2 = game.players[1] player2.hand.append(Card(suit=Suit.CLUB, number = 14)) player2.hand.append(Card(suit=Suit.DIAMOND, number = 5)) players = [player1, player2] game.set_player_hands() winners = game.break_tie(players) self.assertTrue(player1 in winners) self.assertTrue(player2 in winners) self.assertEqual(player1.best_hand['score'], Hand.FULL_HOUSE) self.assertEqual(player2.best_hand['score'], Hand.FULL_HOUSE)
def test_get_winning_player_returns_player1_with_tie(self): players = [Player(), Player()] game = Game(players) #community cards game.community.append(Card(suit=Suit.CLUB, number=2)) game.community.append(Card(suit=Suit.DIAMOND, number=11)) game.community.append(Card(suit=Suit.SPADE, number=5)) game.community.append(Card(suit=Suit.HEART, number=6)) game.community.append(Card(suit=Suit.DIAMOND, number=7)) #player1 cards player1 = game.players[0] player1.hand.append(Card(suit=Suit.CLUB, number=8)) player1.hand.append(Card(suit=Suit.CLUB, number=9)) #player2 cards player2 = game.players[1] player2.hand.append(Card(suit=Suit.DIAMOND, number=4)) player2.hand.append(Card(suit=Suit.SPADE, number=3)) game.set_player_hands() winner = game.get_winning_player() self.assertEqual(winner[0], player1) self.assertEqual(winner[0].best_hand['score'], Hand.STRAIGHT) self.assertEqual(player2.best_hand['score'], Hand.STRAIGHT) self.assertEqual(player2.best_hand['hand'][0].number, 7) self.assertEqual(player1.best_hand['hand'][0].number, 9)
def __init__(self, room): Thread.__init__(self) self.isPlaying = False self.channel_layer = get_channel_layer() self.room = room self.roomName = room.name self.groupName = "chat_%s" % str(room.id) self.members = json.loads(room.members) # id self.wolfs = [] # id self.hunter = "" self.witch = "" self.prophet = "" self.villagers = [] # id self.count = len(self.members) self.wolfCount = 0 self.alive = {} # index = id self.db = Game(name=room.id) self.seperate() self.occupation = {} self.db.save() for each in self.members: userName = getUsername(each) if each in self.villagers: self.occupation[userName] = '村民' elif each in self.wolfs: self.occupation[userName] = '狼人' elif each == self.hunter: self.occupation[userName] = '猎人' elif each == self.witch: self.occupation[userName] = '女巫' elif each == self.prophet: self.occupation[userName] = '预言家' random.shuffle(self.members)
def test_preflop_deal_gives_each_player_2_cards(self): players = [Player(), Player(), Player()] game = Game(players) game.deal() for player in game.players: self.assertEqual(len(player.hand), 2) self.assertEqual(len(game.deck.cards), 52 - len(game.players) * 2) self.assertEqual(game.stage, Stage.PREFLOP)
def test_flop_deal_puts_3_cards_in_community(self): players = [Player(), Player(), Player()] game = Game(players) game.deal() game.deal() for player in game.players: self.assertEqual(len(player.hand), 2) # Deck length is less the initial deal minus 3 community - 1 burn self.assertEqual(len(game.deck.cards), 52 - len(game.players) * 2 - 3 - 1) self.assertEqual(game.stage, Stage.FLOP)
def test_passed_flop_cards_removed_from_deck(self): flop_cards = ['D4', 'C14', 'D13'] players = [Player(), Player()] game = Game(players, flop_cards=flop_cards) self.assertEqual(len(game.deck.cards), 52 - 3) self.assertEqual(len(game.flop_cards), 3)
def test_game_creates_shuffled_deck(self): players = [Player(), Player(), Player()] game = Game(players) self.assertTrue(game.deck) self.assertEqual(len(game.deck.cards), 52) self.assertTrue(game.deck.cards[0].number != 2 or game.deck.cards[0].number != 3)
def test_set_player_best_hands(self): players = [Player(), Player()] game = Game(players) game.deal() game.deal() game.deal() game.deal() game.set_player_hands() player = game.players[0]
def test_passed_river_card_removed_from_deck(self): river_card = 'D14' players = [Player(), Player()] game = Game(players, river_card=river_card) self.assertEqual(len(game.deck.cards), 52 - 1) self.assertEqual(game.flop_cards, []) self.assertEqual(repr(game.river_card), 'D14')
def test_passed_turn_card_is_in_community(self): turn_card = 'D4' players = [Player(), Player()] game = Game(players, turn_card=turn_card) game.deal() game.deal() self.assertEqual(len(game.community), 3) game.deal() self.assertEqual(len(game.community), 4) game.deal() self.assertEqual(len(game.community), 5) self.assertEqual(turn_card, repr(game.turn_card)) self.assertEqual(repr(game.community[3]), 'D4')
def test_deal_maintains_player_start_hands(self): user_hand = ['D4', 'C14'] other_hand = [['D13', 'C13']] player1 = Player(is_user=True, starting_hand=user_hand) player2 = Player(starting_hand=other_hand[0]) players = [player1, player2] game = Game(players) game.deal() self.assertTrue( repr(player1.hand[0]) in [player1.starting_hand[0], player1.starting_hand[1]]) self.assertTrue( repr(player1.hand[1]) in [player1.starting_hand[0], player1.starting_hand[1]]) self.assertTrue( repr(player2.hand[0]) in [player2.starting_hand[0], player2.starting_hand[1]]) self.assertTrue( repr(player2.hand[1]) in [player2.starting_hand[0], player2.starting_hand[1]])
def test_passed_flop_cards_are_in_community(self): flop_cards = ['D4', 'C14', 'D13'] players = [Player(), Player()] game = Game(players, flop_cards=flop_cards) game.deal() game.deal() self.assertEqual(len(game.community), 3) game.deal() self.assertEqual(len(game.community), 4) game.deal() self.assertEqual(len(game.community), 5) self.assertEqual(3, len(game.flop_cards)) self.assertTrue(repr(game.community[0]) in flop_cards) self.assertTrue(repr(game.community[1]) in flop_cards) self.assertTrue(repr(game.community[2]) in flop_cards)
def test_can_create_player(self): players = [Player(), Player()] game = Game(players) game.deal() game.deal() game.deal() game.deal() player = game.players[0] self.assertEqual(len(player.hand), 2)
def test_passing_blank_list_returns_full_community(self): flop_cards = [] players = [Player(), Player()] game = Game(players, flop_cards=flop_cards) self.assertEqual(len(game.deck.cards), 52) self.assertEqual(len(game.flop_cards), 0) game.deal() game.deal() game.deal() game.deal() self.assertEqual(len(game.community), 5)
def test_passed_flop_cards_removed_from_deck_one_card(self): flop_cards = ['D4'] players = [Player(), Player()] game = Game(players, flop_cards=flop_cards) self.assertEqual(len(game.deck.cards), 52 - 1) self.assertEqual(len(game.flop_cards), 1) game.deal() game.deal() game.deal() game.deal() self.assertEqual(len(game.community), 5) self.assertTrue('D4' == repr(game.community[0]))
def test_passed_flop_cards_removed_from_deck_two_cards(self): flop_cards = ['D4', 'C14'] players = [Player(), Player()] game = Game(players, flop_cards=flop_cards) self.assertEqual(len(game.deck.cards), 52 - 2) self.assertEqual(len(game.flop_cards), 2) game.deal() game.deal() game.deal() game.deal() self.assertEqual(len(game.community), 5) self.assertTrue( 'D4' in [repr(game.community[0]), repr(game.community[1])]) self.assertTrue( 'C14' in [repr(game.community[0]), repr(game.community[1])])
class PlayGame(Thread): def __init__(self, room): Thread.__init__(self) self.isPlaying = False self.channel_layer = get_channel_layer() self.room = room self.roomName = room.name self.groupName = "chat_%s" % str(room.id) self.members = json.loads(room.members) # id self.wolfs = [] # id self.hunter = "" self.witch = "" self.prophet = "" self.villagers = [] # id self.count = len(self.members) self.wolfCount = 0 self.alive = {} # index = id self.db = Game(name=room.id) self.seperate() self.occupation = {} self.db.save() for each in self.members: userName = getUsername(each) if each in self.villagers: self.occupation[userName] = '村民' elif each in self.wolfs: self.occupation[userName] = '狼人' elif each == self.hunter: self.occupation[userName] = '猎人' elif each == self.witch: self.occupation[userName] = '女巫' elif each == self.prophet: self.occupation[userName] = '预言家' random.shuffle(self.members) def seperate(self): seperate = [] random.shuffle(self.members) memberCount = self.count if memberCount == 2: seperate = [1, 1] elif memberCount == 4: seperate = [1, 1] elif memberCount == 5: seperate = [2, 1] elif 6 <= memberCount <= 7: seperate = [2, memberCount - 3] elif memberCount <= 10: seperate = [3, memberCount - 5] else: seperate = [4, memberCount - 7] self.wolfs = self.members[:seperate[0]] self.wolfCount = seperate[0] self.villagers = self.members[seperate[0]:seperate[0] + seperate[1]] if memberCount == 4: self.witch = self.members[2] self.prophet = self.members[3] if memberCount == 5: self.witch = self.members[3] self.prophet = self.members[4] else: if memberCount - seperate[0] - seperate[1] > 0: self.witch = self.members[seperate[0] + seperate[1]] if memberCount - (seperate[0] + seperate[1]) - 1 > 0: self.prophet = self.members[seperate[0] + seperate[1] + 1] if memberCount - (seperate[0] + seperate[1]) - 2 > 0: self.hunter = self.members[-1] for each in self.members: self.alive[each] = True def sendMessage(self, message, Type): ''' Type: "occupation-message": 职业信息 "control": 发言控制 "normal-content": 一般发言 "statement": 游戏发言 ''' message['type'] = Type # print(self.channel_layer) # print("send " + str(message)) async_to_sync(self.channel_layer.group_send)(self.groupName, { 'type': 'sendMessage', 'message': message }) def systemMessage(self, message, to): submitData = {} submitData['user'] = "******" submitData['content'] = message submitData['to'] = to self.sendMessage(submitData, "System") def vote(self, data): choices = {} maxCount = 0 member = None for each in data: if self.alive[str(getUserId(each))]: choices[data[each]] = 0 for each in data: if self.alive[str(getUserId(each))]: choices[data[each]] += 1 for each in data: if choices[data[each]] > maxCount and self.alive[str( getUserId(each))]: (maxCount, member) = (choices[data[each]], data[each]) return member def alives(self, to): msg = "当前存活:<br />" for each in self.alive: if self.alive[each]: msg += "%s<br />" % getUsername(each) self.systemMessage(msg, to) def run(self): # real run names = [] for each in self.members: self.alive[each] = True names.append(getUsername(each)) self.sendMessage({'data': names}, 'members') self.isPlaying = True self.sendMessage({'data': self.occupation}, 'occupation-message') while self.wolfCount < self.count - self.wolfCount and self.wolfCount != 0: killed = [] # names self.systemMessage("狼人阶段", "All") self.sendMessage({'data': "狼人"}, 'control') self.systemMessage('狼人请选择淘汰对象, 以"/choose 序号"的形式发送', "狼人") self.alives("狼人") start = time() while time() - start < 60: self.db.refresh_from_db() if len(json.loads(self.db.wolf_choices)) == self.wolfCount: break sleep(1) self.db.refresh_from_db() self.systemMessage("女巫阶段", "All") self.sendMessage({'data': "女巫"}, 'control') self.systemMessage('以"/kill 序号"或者"/save 序号"的形式发送', "女巫") self.alives("女巫") self.systemMessage( "你还有%d瓶毒药,%d瓶解药" % (self.db.witchPoison, self.db.witchAntidote), "女巫") if len(json.loads(self.db.wolf_choices)) != 0: killed.append( getUsername(self.members[ self.vote(json.loads(self.db.wolf_choices)) - 1])) self.systemMessage("%s 将要被杀" % killed[0], "女巫") else: self.systemMessage("无人被杀", "女巫") self.db.wolf_choices = "{}" self.db.save() start = time() while time() - start < 60: self.db.refresh_from_db() if self.db.witch_choice != '': break sleep(1) self.db.refresh_from_db() if self.db.witch_choice != "": choice = self.members[int(self.db.witch_choice) - 1] if getUsername( choice) == killed[0] and self.db.witchAntidote == 1: self.db.witchAntidote = 0 killed = [] elif self.db.witchPoison == 1: self.db.witchPoison = 0 killed.append(getUsername(choice)) self.db.witch_choice = "" self.db.save() self.systemMessage("预言家阶段", 'All') self.sendMessage({'data': "预言家"}, "control") self.systemMessage('以"/check 序号"的形式发送', "预言家") self.alives("预言家") start = time() while time() - start < 60: self.db.refresh_from_db() if self.db.prophet_choice != '': break sleep(1) if len(self.db.prophet_choice) != 0: if self.members[int(self.db.prophet_choice) - 1] in self.wolfs: self.systemMessage( "%s:坏人" % getUsername( self.members[int(self.db.prophet_choice) - 1]), "预言家") else: self.systemMessage( "%s:好人" % getUsername( self.members[int(self.db.prophet_choice) - 1]), "预言家") self.db.prophet_choice = "" self.db.save() self.sendMessage({'data': "daytime"}, "control") if len(killed) == 0: self.systemMessage("天亮了 昨晚无人死亡", "All") else: msg = "天亮了 昨晚" for each in killed: msg = msg + each + " " msg += "死亡" self.systemMessage(msg, "All") for each in killed: if self.alive[str(getUserId(each))]: self.count -= 1 self.alive[str(getUserId(each))] = False if str(getUserId(each)) in self.wolfs: self.wolfCount -= 1 killed = [] sleep(3) if self.wolfCount == 0 or self.wolfCount >= self.count - self.wolfCount: if self.wolfCount == 0: self.systemMessage("好人胜利", "All") else: self.systemMessage("狼人胜利", "All") break self.systemMessage("发言阶段,每人15s", "All") for each in self.members: if self.alive[each]: self.systemMessage("请%s发言" % getUsername(each), "All") self.sendMessage({'data': getUsername(each)}, 'statement') sleep(15) self.systemMessage('30s请投票, 输入"/vote id"', "All") self.sendMessage({}, 'vote') start = time() while time() - start < 15: self.db.refresh_from_db() if len(json.loads(self.db.vote)) == self.count: break sleep(1) if self.db.vote != '{}': self.systemMessage( "%s 死了" % getUsername( self.members[self.vote(json.loads(self.db.vote)) - 1]), "All") killed.append( getUsername( self.members[self.vote(json.loads(self.db.vote)) - 1])) self.db.vote = '{}' self.db.save() else: self.systemMessage("无人投票", "All") for each in killed: if self.alive[str(getUserId(each))]: self.count -= 1 self.alive[each] = False if str(getUserId(each)) in self.wolfs: self.wolfCount -= 1 self.alives("All") self.systemMessage("游戏结束", "All") self.isPlaying = False self.db.delete() def run2(self): # test # sleep(3) names = [] for each in self.members: names.append(getUsername(each)) self.sendMessage({'data': names}, 'members') self.sendMessage({'data': self.occupation}, 'occupation-message') #self.systemMessage('狼人请选择淘汰对象, 以"/choose 序号"的形式发送') #self.alives() self.sendMessage({'data': "狼人"}, 'control') start = time() while time() - start < 10: self.db.refresh_from_db() if len(json.loads(self.db.wolf_choices)) == len(self.wolfs): break sleep(1) print(time()) self.db.refresh_from_db() self.sendMessage({}, "free")
def new_game(): game = Game() game.save() return game.id