async def aCreateMatch(self, _user, _message): if _user.match == None: m = Match(_message["matchname"], _message["matchpassword"], _user) self.matches[m.id] = m await _user.send({"match": m.info()}, _message, 200) else: await _user.send({}, _message, errorcodes["ERR_MATCH_OWNED"])
def main(argv): args = parse_args() d = os.path.dirname('.\logs') print(d) print(os.getcwd()) if not os.path.exists(d): os.makedirs('logs') log.verbose = False names = [x[:-3] for x in os.listdir("bots") if x[-3:] == ".py"] names.remove("__init__") started = time.clock() bots = [ "kerpowski_bot", "kerpowski_conservative_bot", "kerpowski_bot", "kerpowski_conservative_bot" ] sys.path.append("bots") battle = Match([__import__(x) for x in bots]) battle.play(args.rubberCount) print("") log.summary("Completed in", str(int(time.clock() - started)), "seconds") log.dump_log()
def play_card(self, playedCards, dummyHand): """Invoked when the bot has an opportunity to play a card""" chosenCard = max(self.cards, key=lambda x: x.value) if(len(playedCards) > 0): ledSuitCards = [x for x in self.cards if x.suit == playedCards[0].suit] trumpCards = [x for x in self.cards if x.suit == self.winningBid.bidSuit] nonTrumpCards = [x for x in self.cards if x.suit != self.winningBid.bidSuit] topCard = Match.winning_card(playedCards, self.winningBid.bidSuit) if len(ledSuitCards) > 0: if len(playedCards) >= 2: if playedCards.index(topCard) == (len(playedCards) - 2): # Don't overthrow our partner chosenCard = min(ledSuitCards, key=lambda x: x.value) else: # If we're not winning try to take it chosenCard = max(ledSuitCards, key=lambda x: x.value) else: if playedCards.index(topCard) == (len(playedCards) - 2): # Don't trump in on our partner if he's winning if len(nonTrumpCards) > 0: chosenCard = min(nonTrumpCards, key=lambda x: x.value) else: # Trump in if he isn't if len(trumpCards) > 0: chosenCard = min(trumpCards, key=lambda x: x.value) self.cards.remove(chosenCard) return chosenCard
def test_finish_empty_match(self): match = Match(max_players=2) match.add_player(Player("Player 1")) match.add_player(Player("Player 2")) self.assertEqual(match.status, Match.Status.RUN) match.remove_player(match.players[0]) self.assertEqual(match.status, Match.Status.RUN) match.remove_player(match.players[1]) self.assertEqual(match.status, Match.Status.FINISH)
def match_detail(uid: str): player: Optional[Player] = get_player_from_request() if not player: return ErrorResponse.no_player() match: Optional[Match] = Match.get(get_uuid(uid)) if not match: return ErrorResponse.not_found() return jsonify(match.export())
def test_legal_bids(self): for test_sequence, actual_legal in self.test_bid_sequences: bid_list = [Bid.from_string(b, i) for i, b in enumerate(test_sequence)] legal_bids = [] for i, bid in enumerate(bid_list): legal_bids.append(Match.legal_bid(bid, bid_list[:i], i % 4, (i +2) % 4)) nose.tools.assert_list_equal(legal_bids, actual_legal, msg='Bid sequence {0} failed'.format(test_sequence))
def test_status(self): match = Match(max_players=1) self.assertEqual(match.status, Match.Status.WAIT) player1 = Player("Player 1") match.add_player(player1) self.assertEqual(match.status, Match.Status.RUN) match.get_player_board(player1).solve() match.update_status() self.assertEqual(match.status, Match.Status.FINISH) self.assertIsNone(player1.match)
def test_winner_is_board_solver(self): match = Match(max_players=2) player1 = Player("Player 1") player2 = Player("Player 2") match.add_player(player1) match.add_player(player2) match.get_player_board(player1).solve() match.update_status() self.assertEqual(match.status, Match.Status.FINISH) self.assertEqual(match.winner, player1)
def find_match(max_players: int): """Find a match for the player to join.""" # Get the player. player: Optional[Player] = get_player_from_request() if not player: return ErrorResponse.no_player() # Match to put player in. match: Optional[Match] = None # If player is already in a match, kick them out. if player.match: prev_match: Match = player.match prev_match.remove_player(player) pusher.trigger(f"match-{prev_match.uid}", "update", prev_match.export()) # Let's find a match for player to join. for item in Match.list(Match): item: Match = item # Match must be in waiting status with same number of max players. if item.status is Match.Status.WAIT and item.max_players == max_players: match = item # Could not find any match to put player in. if not match: # Create a new match. match = Match(max_players) # Add the player to the match. match.add_player(player) # Trigger update. pusher.trigger(f"match-{match.uid}", "update", match.export()) # Return the match. return jsonify(match.export())
def test_legal_bids(self): for test_sequence, actual_legal in self.test_bid_sequences: bid_list = [ Bid.from_string(b, i) for i, b in enumerate(test_sequence) ] legal_bids = [] for i, bid in enumerate(bid_list): legal_bids.append( Match.legal_bid(bid, bid_list[:i], i % 4, (i + 2) % 4)) nose.tools.assert_list_equal( legal_bids, actual_legal, msg='Bid sequence {0} failed'.format(test_sequence))
def test_winning_cards(self): winning_cards = [(['4s', 'Ks', '3d', 'As'], 's', 'As'), (['4s', 'Ad', '3d', 'As'], 'nt', 'As'), (['4s', '4d', '4h', '4c'], 'nt', '4s'), (['4s', '4d', '4h', '4c'], 'c', '4c'), (['2s', '4d', '4h', '5h'], 'c', '2s')] for played, trump, winner in winning_cards: played_cards = [Card.from_string(c) for c in played] nose.tools.assert_equal( Match.winning_card(played_cards, trump), Card.from_string(winner), 'Error in playing sequence {0} with trump {1}'.format( played, trump))
def test_winning_cards(self): winning_cards = [ (['4s', 'Ks', '3d', 'As'], 's', 'As'), (['4s', 'Ad', '3d', 'As'], 'nt', 'As'), (['4s', '4d', '4h', '4c'], 'nt', '4s'), (['4s', '4d', '4h', '4c'], 'c', '4c'), (['2s', '4d', '4h', '5h'], 'c', '2s') ] for played, trump, winner in winning_cards: played_cards = [Card.from_string(c) for c in played] nose.tools.assert_equal(Match.winning_card(played_cards, trump), Card.from_string(winner), 'Error in playing sequence {0} with trump {1}'.format(played, trump))
def main(argv): args = parse_args() d = os.path.dirname('.\logs') print(d) print(os.getcwd()) if not os.path.exists(d): os.makedirs('logs') log.verbose = False names = [x[:-3] for x in os.listdir("bots") if x[-3:] == ".py"] names.remove("__init__") started = time.clock() bots = ["kerpowski_bot", "kerpowski_conservative_bot", "kerpowski_bot", "kerpowski_conservative_bot"] sys.path.append("bots") battle = Match([__import__(x) for x in bots]) battle.play(args.rubberCount) print("") log.summary("Completed in", str(int(time.clock() - started)), "seconds") log.dump_log()
def play_card(self, playedCards, dummyHand): """Invoked when the bot has an opportunity to play a card""" chosenCard = max(self.cards, key=lambda x: x.value) if (len(playedCards) > 0): ledSuitCards = [ x for x in self.cards if x.suit == playedCards[0].suit ] trumpCards = [ x for x in self.cards if x.suit == self.winningBid.bidSuit ] nonTrumpCards = [ x for x in self.cards if x.suit != self.winningBid.bidSuit ] topCard = Match.winning_card(playedCards, self.winningBid.bidSuit) if len(ledSuitCards) > 0: if len(playedCards) >= 2: if playedCards.index(topCard) == (len(playedCards) - 2): # Don't overthrow our partner chosenCard = min(ledSuitCards, key=lambda x: x.value) else: # If we're not winning try to take it chosenCard = max(ledSuitCards, key=lambda x: x.value) else: if playedCards.index(topCard) == (len(playedCards) - 2): # Don't trump in on our partner if he's winning if len(nonTrumpCards) > 0: chosenCard = min(nonTrumpCards, key=lambda x: x.value) else: # Trump in if he isn't if len(trumpCards) > 0: chosenCard = min(trumpCards, key=lambda x: x.value) self.cards.remove(chosenCard) return chosenCard
from game import ( Match, Bot, ) SEEDS = 2000 matches = [] for seed0 in range(SEEDS): for seed1 in range(SEEDS, 2 * SEEDS): bot0 = Bot(seed0) bot1 = Bot(seed1) match = Match(bot0, bot1) match.run() matches.append([seed0, seed1, round(match.bot0_winnings, 3)]) print(seed0) with open('matches/results.csv', 'w') as file: print('Bot 0;Bot 1;Winnings', file=file) for match in matches: print(*match, sep=';', file=file)
def test_player_count_and_capacity(self): match = Match(max_players=2) match.add_player(Player("Player 1")) self.assertEqual(len(match.players), 1) match.add_player(Player("Player 2")) self.assertEqual(len(match.players), 2)
def match_worker(bots, iterations): sys.path.append("bots") match = Match([__import__(x) for x in bots]) winners = match.repeat(iterations) return [[(x, w == x) for x in bots] for w in winners]