Esempio n. 1
0
def show_all_bid_options(chat_id,message_id,min_bids):
    all_bids = []
    list = []
    j = min_bids[0]
    for i in range(5):
        if i==0: suit = CLUBS
        elif i==1: suit = DIAMONDS
        elif i==2: suit = HEARTS
        elif i==3: suit = SPADES
        elif i==4: suit = NOTRUMP
        if i!=0 and min_bids[i] < min_bids[i-1]:
            list = []
            j = min_bids[i]
        list.append("{}{}".format(min_bids[i],suit))
    all_bids.append(list)
    j += 1
    while j < 8:
        list = []
        for i in range(5):
            if i==0: suit = CLUBS
            elif i==1: suit = DIAMONDS
            elif i==2: suit = HEARTS
            elif i==3: suit = SPADES
            elif i==4: suit = NOTRUMP
            list.append("{}{}".format(j,suit))
        all_bids.append(list)
        j += 1
    all_bids.append(["pass"])
    keyboard = bot.create_inline_keyboard(all_bids)
    bot.edit_message_markup(chat_id, message_id, keyboard)
Esempio n. 2
0
def create_partner_keyboard():
    return bot.create_inline_keyboard([
        ["J{}".format(CLUBS),"Q{}".format(CLUBS),"K{}".format(CLUBS),"A{}".format(CLUBS)],
        ["J{}".format(DIAMONDS),"Q{}".format(DIAMONDS),"K{}".format(DIAMONDS),"A{}".format(DIAMONDS)],
        ["J{}".format(HEARTS),"Q{}".format(HEARTS),"K{}".format(HEARTS),"A{}".format(HEARTS)],
        ["J{}".format(SPADES),"Q{}".format(SPADES),"K{}".format(SPADES),"A{}".format(SPADES)],
        ["other"]
    ])
Esempio n. 3
0
def create_bidding_keyboard(min_bids):
    line1 = []
    for i in range(5):
        if i==0: suit = CLUBS
        elif i==1: suit = DIAMONDS
        elif i==2: suit = HEARTS
        elif i==3: suit = SPADES
        elif i==4: suit = NOTRUMP
        if min_bids[i]<8:
            line1.append("{}{}".format(min_bids[i],suit))
    line2 = ["pass"]
    if min_bids[4]<7:
        line2.append("other")
    keyboard = bot.create_inline_keyboard([line1,line2])
    return keyboard
Esempio n. 4
0
def show_all_partner_options(chat_id,message_id):
    all_cards = []
    for i in range(4):
        if i==0: suit = CLUBS
        elif i==1: suit = DIAMONDS
        elif i==2: suit = HEARTS
        elif i==3: suit = SPADES
        list = []
        for j in range(2,7):
            list.append("{}{}".format(j,suit))
        all_cards.append(list)
        list = []
        for j in range(7,11):
            list.append("{}{}".format(j,suit))
        all_cards.append(list)
        all_cards.append(["J{}".format(suit),"Q{}".format(suit),"K{}".format(suit),"A{}".format(suit)])
    keyboard = bot.create_inline_keyboard(all_cards)
    bot.edit_message_markup(chat_id, message_id, keyboard)
Esempio n. 5
0
def main():
    bot.clear_past_updates()

    while True:
        data = bot.listen()
        if(data["text"]=="/start"):
            print("game starting...")
            break
        else:
            print("invalid command, trying again")

    # lock in the group chat / cannot take indiv chat
    # updates from other chats will mess with the program
    chat_id = data["chat_id"]
    group_name = data["group_name"]

    # welcome + registration
    bot.send_message(
        "hi {}, welcome to bridge! those who are playing, please /register".format(group_name),
        chat_id
    )
    use_same_players = False
    while True:
        if not use_same_players:
            players = register_players(chat_id)
            # players = register_players_test(chat_id) # for testing purposes

        # start game: create deck and deal
        bot.send_message(
            "shuffling and dealing cards...",
            chat_id,
            bot.create_remove_keyboard()
        )
        deck = cards.Deck()
        while True:
            deck.rebuild()
            print("shuffling cards...")
            deck.shuffle()

            for i in range(4):
                players[i].clear_hand()
                players[i].tricksWon = 0

            # deal cards
            print("dealing cards...")
            for i in range(0,13):
                for j in range(0,4):
                    players[j].draw(deck)

            # sort cards and check if need to wash
            print("checking for wash...")
            need_wash = False
            for i in range(4):
                points = players[i].getPoints() # conveniently sorts the cards too
                if(points<4):
                    print("{} wash. reshuffling...".format(players[i].name))
                    bot.send_message(
                        "{} needs to wash! reshuffling and dealing ...".format(players[i].name),
                        chat_id
                    )
                    need_wash = True
                    break
            if need_wash:
                continue
            print("no need to wash")
            break

        send_cards_to_all(players,chat_id)

        # bidding using inline keyboard
        # so players can still see their cards in the reply keyboard
        skip_to_end = False
        currBidder = random.randint(0,3)
        print("player {} starts bid".format(currBidder+1))
        log = "bid log:\n"
        min_bids = [1,1,1,1,1]
                 # [C,D,H,S,NT]
        pass_count = 0
        bot.send_message(
            "{} (randomly chosen) starts bid\ncurrent bid: no bid".format(players[currBidder].name),
            chat_id,
            create_bidding_keyboard(min_bids)
        )

        while True:
            bid, message_id = listen_for_user(players[currBidder])

            if bid == "pass":
                pass_count += 1
            elif bid == "other":
                show_all_bid_options(chat_id,message_id,min_bids)
                print("showing all bid options")
                continue
            else:
                print("updating minimum bids")
                update_min_bids(min_bids,bid)
                pass_count = 0

            # update log
            log += "{}: {}\n".format(players[currBidder].name,bid)
            print("player {}'s turn ends".format(currBidder+1))
            currBidder = (currBidder+1)%4

            if min_bids[4] == 8: # bidder bid 7NT (no more possible bids)
                bid_winner = (currBidder+3)%4
                print("7NT - player {} wins bid".format(bid_winner+1))
                break
            elif pass_count > 2:
                if min_bids[0]==1: # >=3 passes with no bid
                    if pass_count > 3: # 4 passes with no bid
                        print("everybody passed without bidding (game ends)")
                        bot.edit_message(
                            "{}\neveryone passed; game ends.\n\nplay /again? or /exit".format(log),
                            chat_id,
                            message_id
                        )
                        skip_to_end = True
                        break
                else: # 3 passes with existing bid
                    bid_winner = currBidder
                    print("player {} wins bid".format(bid_winner+1))
                    break

            # continue with these if didnt terminate from above
            main_line = "{}, your turn to bid".format(players[currBidder].name)
            update_bid_message(main_line, log, chat_id, message_id, min_bids)
            # 7nt scenario

        if not skip_to_end:
            winning_bid = get_current_bid(min_bids)
            trump_suit = winning_bid[1:]
            bot.edit_message(
                "{}\n{} wins the bid ({})!".format(log,players[bid_winner].name,winning_bid),
                chat_id,
                message_id
            )
            bot.send_message(
                "{}, choose your partner".format(players[bid_winner].name),
                chat_id,
                create_partner_keyboard()
            )

            while True:
                partner_card_str, message_id = listen_for_user(players[bid_winner]) # new message_id
                if partner_card_str == "other":
                    show_all_partner_options(chat_id,message_id)
                    print("showing all possible cards")
                    continue
                else:
                    break
            bot.edit_message(
                "{} chose {} as his/her partner".format(players[bid_winner].name,partner_card_str),
                chat_id,
                message_id
            )

            partner_card = get_card_from_str(partner_card_str)
            print("partner:"+str(partner_card))
            partner = findPartner(partner_card,players)
            print("partner is player {}\n".format(partner+1))
            teams = createTeams(bid_winner,partner,int(winning_bid[0]))
            allocateTeams(players,teams)

            # to check
            for i in range(2):
                print("team {} needs:".format(i+1))
                print(str(teams[i].tricksNeeded))
                print("members:")
                for m in teams[i].members:
                    print(m+1)

            # determine who starts
            if(trump_suit==NOTRUMP):
                start = bid_winner
            else:
                start = (bid_winner+1)%4

            # for testing, to see all cards in terminal
            for i in range(4):
                print("player {}".format(i+1))
                bridge.printCardsFromList(players[i].hand)

            # actual gameplay
            info_string = "****** GAME INFO ******\nwinning bid: {} ({}-{})\n{}'s partner: {}".format(
                winning_bid,
                teams[players[bid_winner].team].tricksNeeded,
                14-teams[players[bid_winner].team].tricksNeeded,
                players[bid_winner].name,
                partner_card_str
            )
            trump_broken = False
            for i in range(13):
                log = "trick {}'s log:\n".format(i+1)
                suit = None
                print("player {} starts".format(start+1))
                bot.send_message(
                    "{}, play a card".format(players[start].name),
                    chat_id
                )
                for j in range(4):
                    curr_player_idx = (start+j)%4
                    next_player_idx = (start+j+1)%4
                    curr_player = players[curr_player_idx]
                    next_player = players[next_player_idx]
                    playable = curr_player.getPlayable(suit,trump_suit,trump_broken)
                    while True:
                        while True:
                            data = bot.listen()
                            if data["sender_id"]==curr_player.user_id:
                                played_card_str = data["text"]
                                break
                            else:
                                print("not that player's turn, listening again")
                        played_card = get_card_from_str(played_card_str)
                        if played_card in playable:
                            break
                        else:
                            bot.send_message(
                                "you cant play that card (wrong suit / trump not broken / dont have that card)",
                                chat_id
                            )
                    curr_player.play(played_card)
                    if(played_card.suit==trump_suit):
                        trump_broken = True

                    if(j==0):
                        suit = played_card.suit
                        winningCard = played_card
                        trick_winner = curr_player
                        trick_winner_idx = curr_player_idx
                    else:
                        if(isStrongerCard(played_card,winningCard,trump_suit)):
                            winningCard = played_card
                            trick_winner = curr_player
                            trick_winner_idx = curr_player_idx

                    log += "{}: {}\n".format(curr_player.name,played_card_str)

                    if j<3:
                        bot.send_message(
                            "updated @{}'s cards\n\n{}\n{}, your turn".format(curr_player.username,log,next_player.name),
                            chat_id,
                            hand_to_json_keyboard(curr_player)
                        )
                    else: # last player of the trick
                        bot.send_message(
                            "updated @{}'s cards\n\n{}".format(curr_player.username,log),
                            chat_id,
                            hand_to_json_keyboard(curr_player)
                        )
                trick_winner.tricksWon+=1
                teams[trick_winner.team].tricksWon+=1
                scores_str = "total tricks won:\n{}: {}\n{}: {}\n{}: {}\n{}: {}".format(
                    players[0].name, players[0].tricksWon,
                    players[1].name, players[1].tricksWon,
                    players[2].name, players[2].tricksWon,
                    players[3].name, players[3].tricksWon,
                )
                bot.send_message(
                    "{} wins this trick\n\n{}\n\n{}".format(trick_winner.name,scores_str,info_string),
                    chat_id
                )

                start = trick_winner_idx

                # check for winner
                if(teams[0].tricksWon==teams[0].tricksNeeded):
                    winningTeam = 0
                    losingTeam = 1
                    break
                elif(teams[1].tricksWon==teams[1].tricksNeeded):
                    winningTeam = 1
                    losingTeam = 0
                    break
                # else: pass

            if bid_winner == partner:
                partner_message = "{} called himself/herself. he/she ".format(players[bid_winner].name)
            else:
                partner_message = "{} was {}'s partner. they ".format(players[partner].name,players[bid_winner].name)

            other_team_message = get_team_members_str(teams[1],players)
            if winningTeam==0:
                team_message = "won!\n"
                other_team_message += " lost :/"
            else:
                team_message = "lost :/\n"
                other_team_message += " won!"

            print("{} won!".format(teams[winningTeam].name))
            print("winning members: {}".format(get_team_members_str(teams[winningTeam],players)))

            replay_message = "play /again? or /exit"

            bot.send_message(
                "{}{}{}\n\nplay /again? or /exit".format(partner_message,team_message,other_team_message),
                chat_id
            )

        while True:
            data = bot.listen()
            if data["text"]=="/again":
                print("game starting...")
                break
            elif data["text"]=="/exit":
                print("exiting ...")
                bot.send_message(
                    "alright, thanks for playing. goodbye!",
                    chat_id,
                    bot.create_remove_keyboard()
                )
                return
            else:
                print("invalid command, trying again")

        bot.send_message("great! are the same players playing again?",
            chat_id,
            bot.create_inline_keyboard([["same","diff"]])
        )
        while True:
            data = bot.listen_for_callback()
            if data["callback_data"] == "same":
                use_same_players = True
                bot.edit_message("same players are playing again; no need to re-register",chat_id,data["message_id"])
                break
            elif data["callback_data"] == "diff":
                use_same_players = False
                bot.edit_message("different players are playing; new players, please /register",chat_id,data["message_id"])
                break
            else:
                print("unrecognised input, listening again")