Exemplo n.º 1
0
def newHand():
    global dealerHand, dealerHandElements
    global playerHand, playerHandElements
    dealerHand = cards.draw(dealerDeck, 2)

    dealerHandElements = genCardElements(dealerHand, startPos=(10, 10))
    dealerHandElements[0].flip()
    playerHand = cards.draw(dealerDeck, 2)
    playerHandElements = genCardElements(playerHand,
                                         startPos=(10, 720 -
                                                   DEFAULT_CARD_SIZE[1] - 10))
Exemplo n.º 2
0
def command_cards(intext, *args):
    if len(intext) == 0:
        return "A dry wind blows in from the west."
    intext = intext.split(" ")
    subargs = len(intext)
    subcommand = intext[0]
    try:
        # fetch deck from manager. TODO: lock here?
        data = args[0]
        deck = data.get_card_deck()
        user = args[1]
    except Exception as err:
        raise RuntimeError("Can't find cards (data manager failed?)") from err

    ret = None
    if subcommand == "draw":
        count = 1
        if subargs > 1:
            count = int(intext[1])
        drawn = cards.draw(deck, count)
        ret = f"{drawn}"
    elif subcommand == "reset":
        deck = cards.shuffle(cards.build_deck_52())
        ret = "Deck reset and shuffled."
    elif subcommand == "shuffle":
        deck = cards.shuffle(deck)
        ret = f"Deck shuffled."
    elif subcommand == "inspect":
        top = deck[len(deck) - 1] if len(deck) > 0 else None
        bot = deck[0] if len(deck) > 0 else None
        ret = f"{len(deck)} cards in deck. Top card is {top}. Bottom card is {bot}."
    elif subcommand == "history":
        count = 1
        if subargs > 1:
            count = int(intext[1])
        history = data.get_card_logs()
        numbered = [f"{i+1}: {history[i]}"
                    for i in range(len(history))][-count:]
        ret = '\n'.join(numbered)
        if len(numbered) < count:
            ret = "> start of history.\n" + ret
    else:
        return f"Unknown subcommand {codeblock(subcommand)}. {sub_help_notice('cards')}"

    # apply deck changes
    data.set_card_deck(deck)
    # update card log
    data.add_card_log(
        f"{user}: {ret if subcommand != 'history' else 'viewed history.'}")
    return ret
Exemplo n.º 3
0
    def __init__(self, num_players=2, num_decks=2, goal_size=13, hand_size=4):
        '''
            Create a new game, ready to play with the specified parameters
        '''
        if num_players < 2:
            raise RuntimeError("Too few players")
        # TODO explore how many cards are needed to ensure a game finishes - this is a rough guess
        min_cards_needed = num_players * (goal_size + hand_size) + (
            MAX_CARDS_PER_PLAY_PILE + 1) * NUM_PLAY_PILES
        if num_decks * NUM_CARDS_PER_DECK < min_cards_needed:
            raise RuntimeError("Too few cards")

        # constants
        self.num_players = num_players
        self.num_decks = num_decks
        self.hand_size = hand_size
        self.goal_size = goal_size
        self.winner = None

        # Make a deck
        decks = make_decks(num_decks)

        #Deal!
        # list of lists of Cards
        self.goal_piles = [
            draw_N(decks, goal_size) for _ in range(num_players)
        ]
        # list of lists of Cards
        self.player_hands = [
            draw_N(decks, hand_size) for _ in range(num_players)
        ]
        # list of Cards
        self.goal_cards = [draw(pile) for pile in self.goal_piles]
        # list of lists of lists of Cards - top card is self.discard_piles[player_index][pile_index][-1]
        self.discard_piles = [[[] for _ in range(NUM_DISCARD_PILES)]
                              for _ in range(num_players)]
        # list of lists of Cards
        self.play_piles = [[] for _ in range(NUM_PLAY_PILES)]
        # list of Cards
        self.draw_pile = decks
        self.current_player = 0
Exemplo n.º 4
0
    def do_other_work(self):
        '''
            Do everything that isn't shared between Game and subclasses
        '''
        # shuffle full play piles back into deck
        for i in range(len(self.play_piles)):
            if len(self.play_piles[i]) == MAX_CARDS_PER_PLAY_PILE:
                self.draw_pile += self.play_piles[i]
                self.play_piles[i] = []

        # restock current player's hand
        if len(self.player_hands[self.current_player]) == 0:
            self.fill_hand()

        # check for winner, flip next goal card
        if self.goal_cards[self.current_player] is None \
                and len(self.goal_piles[self.current_player]) > 0:
            self.goal_cards[self.current_player] = draw(
                self.goal_piles[self.current_player])

        if len(self.goal_piles[self.current_player]) == 0:
            self.winner = self.current_player
Exemplo n.º 5
0
def hit(hand, elements):
    hand.append(*cards.draw(dealerDeck, 1))
    elements.append(*genCardElements([hand[-1]],
                                     startPos=(elements[-1].position[0] +
                                               DEFAULT_CARD_SIZE[0] + 10,
                                               elements[-1].position[1])))
Exemplo n.º 6
0
def main():
    print("\n \n \n \n \n \n \n \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n")
    print("Welcome to Jojo's Blackjack Corner!!")
    print("")

    player_number = input("How many players(1-5)? ")

    if (player_number == "quit"):
        print("Thankyou for playing, Goodbye!")
        exit(0)

    PLAYER_NUMBER = int(player_number) + 1

    while (PLAYER_NUMBER < 1 or PLAYER_NUMBER > 5):
        PLAYER_NUMBER = int(input("Invalid input: "))

    #Enter how much money each player will have
    player_money = input("How much money will each player start out with? ")

    if (player_money == "quit"):
        print("Thankyou for playing, Goodbye!")
        exit(0)

    player_money = int(player_money)

    while (player_money < 1 or player_money > 100000000):
        player_money = int(
            input(
                "Invalid input, please choose positive number from 1 to 1000000: "
            ))

    print("Then let's get started!")

    #DONE
    #Player vector
    player_vector = {}
    for i in range(0, PLAYER_NUMBER):
        if (i < PLAYER_NUMBER - 1):
            player_vector[i] = player.Player(player_money, "Player" + str(i),
                                             0)
        else:
            player_vector[i] = player.Player(player_money, "Dealer", 0)

    user_input = ""

    while (True):
        # cards.resetHands(player_vector[PLAYER_NUMBER-1])
        # cards.resetHands(player_vector[i])
        for i in range(0, PLAYER_NUMBER):
            cards.resetHands(player_vector[i])

        # card1 = cards.draw(deck, cards_dealt)
        # card2 = cards.draw(deck, cards_dealt)
        # card3 = cards.draw(deck, cards_dealt)
        # card4 = cards.draw(deck, cards_dealt)
        all_cards = {}
        total_card_num = 2 * (PLAYER_NUMBER)
        player_counter = 0
        for i in range(0, total_card_num):
            all_cards[i] = cards.draw(deck, cards_dealt)
            if (i >= PLAYER_NUMBER):
                player_vector[player_counter].setHand(
                    all_cards[i - PLAYER_NUMBER], all_cards[i])
                player_vector[player_counter].setTotal(
                    all_cards[i - PLAYER_NUMBER].getClassification() +
                    all_cards[i].getClassification())
                player_counter += 1

        #PLAYER_NUMBER-1 because we want to loop over every user, not dealer
        for j in range(0, PLAYER_NUMBER - 1):

            print("\n \n \n \n \n \n \n \n\n \n \n \n \n \n \n \n \n \n \n \n")

            user_input = input("Ready to play?")

            if (user_input == "quit" or user_input == "q"):
                print("Thank you for playing!")
                exit(0)

            print("You have $" + str(player_vector[j].getMoney()))

            amount_bet = int(
                input("How much are you betting (100, 500, or 1000)?"))

            while (amount_bet > player_vector[j].getMoney()):
                amount_bet = int(input("Please enter a valid bet:"))
            actions.bet(player_vector[j], amount_bet)

            aces = 0
            busted = False
            hand_index = 2
            while (True):
                for i in range(0, len(player_vector[j].hand)):
                    if (i == 0):
                        print("Hidden: " + player_vector[j].hand[i].getName())
                        if (player_vector[j].hand[i].getClassification() == 11
                            ):
                            aces += 1
                    else:
                        print("Visible: " + player_vector[j].hand[i].getName())
                        if (player_vector[j].hand[i].getClassification() == 11
                            ):
                            aces += 1
                if (len(player_vector[j].hand) == 2
                        and player_vector[j].getTotal() == 21):
                    print("Blackjack, congradulations!")
                    break
                elif (player_vector[j].getTotal() == 21):
                    print("You got 21, congradulations!")
                    break
                else:
                    print("Your total is now: " +
                          str(player_vector[j].getTotal()))
                user_input = input("Would you like to hit or stay? ")
                if (user_input == "quit" or user_input == "q"):
                    print("Thankyou for playing, goodbye!")
                    exit(0)
                if (user_input == "hit"):
                    player_vector[j].hand[hand_index] = cards.draw(
                        deck, cards_dealt)
                    player_vector[j].adjustTotal(
                        player_vector[j].hand[hand_index].getClassification())
                    if (player_vector[j].hand[hand_index].getClassification()
                            == 11):
                        aces += 1
                    hand_index += 1

                    if (player_vector[j].getTotal() > 21 and aces > 0):
                        cards.aceHighOrLow(player_vector[j])
                    print("Your total is now: " +
                          str(player_vector[j].getTotal()))

                    if (player_vector[j].getTotal() > 21):
                        print("Oops, busted")
                        for i in range(0, len(player_vector[j].hand)):
                            if (i == 0):
                                print("Hidden: " +
                                      player_vector[j].hand[i].getName())
                            else:
                                print("Visible: " +
                                      player_vector[j].hand[i].getName())
                        busted = True
                        actions.endRound(player_vector[j], amount_bet, busted,
                                         False)
                        break
                if (user_input == "stay"):
                    print("Your total is " + str(player_vector[j].getTotal()))
                    break
            if (busted):
                continue

            print(" \n \n \n \n \n \n \n \n \n \n \n" +
                  player_vector[j + 1].getName() + "'s turn!")

        #Loop to model dealer's move
        hand_index = 2
        while (True):
            print("\nDealer's cards")

            print("PLAYER_NUMBER = " + str(PLAYER_NUMBER))
            for i in range(0, len(player_vector[PLAYER_NUMBER - 1].hand)):
                if (i == 0):
                    print("Hidden: " +
                          player_vector[PLAYER_NUMBER - 1].hand[i].getName())
                    aces += 1
                else:
                    print("Visible: " +
                          player_vector[PLAYER_NUMBER - 1].hand[i].getName())
                    aces += 1
            #INCREMENT ACE COUNT PROPERLY
            if (player_vector[PLAYER_NUMBER - 1].getTotal() < 17):
                player_vector[PLAYER_NUMBER - 1].hand[hand_index] = cards.draw(
                    deck, cards_dealt)
                player_vector[PLAYER_NUMBER - 1].adjustTotal(
                    player_vector[PLAYER_NUMBER -
                                  1].hand[hand_index].getClassification())
                hand_index += 1
                print("\nLength of dealer hand = " +
                      str(len(player_vector[PLAYER_NUMBER - 1].hand)))
                print("Hit")
                for k in range(0, len(player_vector[PLAYER_NUMBER - 1].hand)):
                    if (k == 0):
                        print("Hidden: " + player_vector[PLAYER_NUMBER -
                                                         1].hand[k].getName())
                    else:
                        print("Visible: " + player_vector[PLAYER_NUMBER -
                                                          1].hand[k].getName())
                print("Dealer total is now: " +
                      str(player_vector[PLAYER_NUMBER - 1].getTotal()))
                continue

            for i in range(0, PLAYER_NUMBER - 1):
                if (player_vector[PLAYER_NUMBER - 1].getTotal() > 21
                        and aces > 0):
                    cards.aceHighOrLow(player_vector[PLAYER_NUMBER - 1])

                if (player_vector[PLAYER_NUMBER - 1].getTotal() ==
                        player_vector[i].getTotal()):
                    print("PUSH!!!")
                    player_vector[i].setMoney(amount_bet, 1)

                if (player_vector[PLAYER_NUMBER - 1].getTotal() > 16
                        and player_vector[PLAYER_NUMBER - 1].getTotal() < 22):
                    print("Dealer total is now: " +
                          str(player_vector[PLAYER_NUMBER - 1].getTotal()))
                    if (player_vector[i].getTotal() >
                            player_vector[PLAYER_NUMBER - 1].getTotal()):
                        print("You win $" + str(amount_bet) + "!")
                        increment = 2 * amount_bet
                        player_vector[i].setMoney(increment, 1)

                    else:
                        print("You lose $" + str(amount_bet))

                if (player_vector[PLAYER_NUMBER - 1].getTotal() > 21):
                    print("Dealer total is now: " +
                          str(player_vector[PLAYER_NUMBER - 1].getTotal()))
                    print("You win $" + str(amount_bet) + "!")
                    amount_bet = 2 * amount_bet
                    player_vector[i].setMoney(amount_bet, 1)

        continue
Exemplo n.º 7
0
def main():
    print("\n \n \n \n \n \n \n \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n")
    print("Welcome to Jojo's Blackjack Corner!!")
    print("")

    player_number = input("How many players(2-6)? ")

    if (player_number == "quit"):
        print("Thankyou for playing, Goodbye!")
        exit(0)

    PLAYER_NUMBER = int(player_number)

    while (PLAYER_NUMBER < 1 or PLAYER_NUMBER > 6):
        PLAYER_NUMBER = int(input("Invalid input: "))

    #Enter how much money each player will have
    player_money = input("How much money will each player start out with? ")

    if (player_money == "quit"):
        print("Thankyou for playing, Goodbye!")
        exit(0)

    player_money = int(player_money)

    while (player_money < 1 or player_money > 1000000):
        player_number = int(
            input(
                "Invalid input, please choose positive number from 1 to 1000000: "
            ))

    print("Then let's get started!")

    #DONE
    #Player vector
    player_vector = {}
    for i in range(0, PLAYER_NUMBER + 1):
        player_vector[i] = player.Player(player_money, "Player" + str(i), 0)

    user_input = ""

    while (True):
        print("\n \n \n \n \n \n \n \n\n \n \n \n \n \n \n \n \n \n \n \n")

        user_input = input("Ready to play?")

        if (user_input == "quit" or user_input == "q"):
            print("Thank you for playing!")
            exit(0)

        print("You have $" + str(player_vector[1].getMoney()))

        card1 = cards.draw(deck, cards_dealt)
        card2 = cards.draw(deck, cards_dealt)
        card3 = cards.draw(deck, cards_dealt)
        card4 = cards.draw(deck, cards_dealt)

        cards.resetHands(player_vector[0])
        cards.resetHands(player_vector[1])
        player_vector[0].setHand(card2, card4)
        player_vector[1].setHand(card1, card3)

        player_vector[0].setTotal(card2.getClassification() +
                                  card4.getClassification())
        player_vector[1].setTotal(card1.getClassification() +
                                  card3.getClassification())

        amount_bet = int(
            input("How much are you betting (100, 500, or 1000)?"))

        while (amount_bet > player_vector[1].getMoney()):
            amount_bet = int(input("Please enter a valid bet:"))
        actions.bet(player_vector[1], amount_bet)

        aces = 0
        busted = False
        hand_index = 2
        while (True):
            for i in range(0, len(player_vector[1].hand)):
                if (i == 0):
                    print("Hidden: " + player_vector[1].hand[i].getName())
                    if (player_vector[1].hand[i].getClassification() == 11):
                        aces += 1
                else:
                    print("Visible: " + player_vector[1].hand[i].getName())
                    if (player_vector[1].hand[i].getClassification() == 11):
                        aces += 1
            if (len(player_vector[1].hand) == 2
                    and player_vector[1].getTotal() == 21):
                print("Blackjack, congradulations!")
                break
            elif (player_vector[1].getTotal() == 21):
                print("You got 21, congradulations!")
                break
            else:
                print("Your total is now: " + str(player_vector[1].getTotal()))
            user_input = input("Would you like to hit or stay? ")
            if (user_input == "quit" or user_input == "q"):
                print("Thankyou for playing, goodbye!")
                exit(0)
            if (user_input == "hit"):
                player_vector[1].hand[hand_index] = cards.draw(
                    deck, cards_dealt)
                player_vector[1].adjustTotal(
                    player_vector[1].hand[hand_index].getClassification())
                if (player_vector[1].hand[hand_index].getClassification() == 11
                    ):
                    aces += 1
                hand_index += 1

                if (player_vector[1].getTotal() > 21 and aces > 0):
                    cards.aceHighOrLow(player_vector[1])
                print("Your total is now: " + str(player_vector[1].getTotal()))

                if (player_vector[1].getTotal() > 21):
                    print("Oops, busted")
                    for i in range(0, len(player_vector[1].hand)):
                        if (i == 0):
                            print("Hidden: " +
                                  player_vector[1].hand[i].getName())
                        else:
                            print("Visible: " +
                                  player_vector[1].hand[i].getName())
                    busted = True
                    actions.endRound(player_vector[1], amount_bet, busted,
                                     False)
                    break
            if (user_input == "stay"):
                print("Your total is " + str(player_vector[1].getTotal()))
                break
        if (busted):
            continue
        dealer_total = 0
        hand_index = 2
        while (True):
            print("\nDealer's cards")

            for i in range(0, len(player_vector[0].hand)):
                if (i == 0):
                    print("Hidden: " + player_vector[0].hand[i].getName())
                    aces += 1
                else:
                    print("Visible: " + player_vector[0].hand[i].getName())
                    aces += 1
            #INCREMENT ACE COUNT PROPERLY
            if (player_vector[0].getTotal() < 17):
                player_vector[0].hand[hand_index] = cards.draw(
                    deck, cards_dealt)
                player_vector[0].adjustTotal(
                    player_vector[0].hand[hand_index].getClassification())
                hand_index += 1
                print("\nLength of dealer hand = " +
                      str(len(player_vector[0].hand)))
                print("Hit")
                for i in range(0, len(player_vector[0].hand)):
                    if (i == 0):
                        print("Hidden: " + player_vector[0].hand[i].getName())
                    else:
                        print("Visible: " + player_vector[0].hand[i].getName())
                print("Dealer total is now: " +
                      str(player_vector[0].getTotal()))
                continue

            if (player_vector[0].getTotal() > 21 and aces > 0):
                print("print reached if statement")
                cards.aceHighOrLow(player_vector[0])

            if (player_vector[0].getTotal() == player_vector[1].getTotal()):
                print("PUSH!!!")
                player_vector[1].setMoney(amount_bet, 1)
                break

            if (player_vector[0].getTotal() > 16
                    and player_vector[0].getTotal() < 22):
                print("Dealer total is now: " +
                      str(player_vector[0].getTotal()))
                if (player_vector[1].getTotal() > player_vector[0].getTotal()):
                    print("You win $" + str(amount_bet) + "!")
                    increment = 2 * amount_bet
                    player_vector[1].setMoney(increment, 1)
                    break
                else:
                    print("You lose $" + str(amount_bet))
                    break
                break
            if (player_vector[0].getTotal() > 21):
                print("Dealer total is now: " +
                      str(player_vector[0].getTotal()))
                print("You win $" + str(amount_bet) + "!")
                amount_bet = 2 * amount_bet
                player_vector[1].setMoney(amount_bet, 1)
                break
        continue
        print(" \n \n \n \n \n \n \n \n \n \n \n" + Player.getName() +
              "'s turn!")
Exemplo n.º 8
0
def main():
    player_number = input("How many players(2-6)? ")
    if (player_number == "quit"):
        print("Thankyou for playing, Goodbye!")
        exit(0)
    PLAYER_NUMBER = int(player_number)
    while (PLAYER_NUMBER < 2 or PLAYER_NUMBER > 6):
        PLAYER_NUMBER = int(input("Invalid input: "))
    #Enter how much money each player will have
    player_money = input("How much money will each player start out with? ")
    if (player_money == "quit"):
        print("Thankyou for playing, Goodbye!")
        exit(0)
    player_money = int(player_money)
    while (player_money < 1 or player_money > 1000000):
        player_number = int(
            input(
                "Invalid input, please choose positive number from 1 to 1000000: "
            ))
    print("Then let's get started!")

    #DONE
    #Player vector
    player_vector = {}
    #Declare number of players(loop through if you want after finished)
    if (PLAYER_NUMBER == 2):
        Player1 = player.Player(player_money, "Player1", 0)
        player_vector[0] = Player1
        Player2 = player.Player(player_money, "Player2", 0)
        player_vector[1] = Player2
    elif (PLAYER_NUMBER == 3):
        Player1 = player.Player(player_money, "Player1", 0)
        player_vector[0] = Player1
        Player2 = player.Player(player_money, "Player2", 0)
        player_vector[1] = Player2
        Player3 = player.Player(player_money, "Player3", 0)
        player_vector[2] = Player3
    elif (PLAYER_NUMBER == 4):
        Player1 = player.Player(player_money, "Player1", 0)
        player_vector[0] = Player1
        Player2 = player.Player(player_money, "Player2", 0)
        player_vector[1] = Player2
        Player3 = player.Player(player_money, "Player3", 0)
        player_vector[2] = Player3
        Player4 = player.Player(player_money, "Player4", 0)
        player_vector[3] = Player4
    elif (PLAYER_NUMBER == 5):
        Player1 = player.Player(player_money, "Player1", 0)
        player_vector[0] = Player1
        Player2 = player.Player(player_money, "Player2", 0)
        player_vector[1] = Player2
        Player3 = player.Player(player_money, "Player3", 0)
        player_vector[2] = Player3
        Player4 = player.Player(player_money, "Player4", 0)
        player_vector[3] = Player4
        Player5 = player.Player(player_money, "Player5", 0)
        player_vector[4] = Player5
    elif (PLAYER_NUMBER == 6):
        Player1 = player.Player(player_money, "Player1", 0)
        player_vector[0] = Player1
        Player2 = player.Player(player_money, "Player2", 0)
        player_vector[1] = Player2
        Player3 = player.Player(player_money, "Player3", 0)
        player_vector[2] = Player3
        Player4 = player.Player(player_money, "Player4", 0)
        player_vector[3] = Player4
        Player5 = player.Player(player_money, "Player5", 0)
        player_vector[4] = Player5
        Player6 = player.Player(player_money, "Player6", 0)
        player_vector[5] = Player6
    user_input = ""

    #Set game counter
    game_counter = 0
    while (user_input != "quit"):
        pot = 0
        player_vector_index = 0
        if (game_counter == 0):
            #Draws the hands of Players, puts them into a (player_number * 2) matrix
            #Later try to consolidate the 3 loops into 2
            hands = [["", ""], ["", ""], ["", ""], ["", ""], ["", ""],
                     ["", ""]]
            card_index = 0
            i = 0
            while (i < 2):
                card_index = 0
                while (card_index < PLAYER_NUMBER):
                    hands[card_index][i] = cards.draw(deck, cards_dealt)
                    card_index = card_index + 1
                i = i + 1

            #Sets the hands of all players using hands matrix
            #Sets the initial status to 1
            hand_index = 0
            while (hand_index < PLAYER_NUMBER):
                player_vector[player_vector_index].setHand(
                    hands[hand_index][0], hands[hand_index][1])
                player_vector[player_vector_index].setStatus(1)
                hand_index = hand_index + 1
                player_vector_index = player_vector_index + 1

            #Starts game play
            #Initial round of betting before the flop
            players_moved = 0
            previous = -2
            player_vector_index = 0
            player_round_number = PLAYER_NUMBER
            while (players_moved != player_round_number):
                print(player_vector[player_vector_index].getName())
                round_bet = actions.move(player_vector[player_vector_index],
                                         previous)
                if (round_bet > 0):
                    pot = pot + round_bet
                    previous = round_bet
                    players_moved = 1
                elif (round_bet == 0):
                    previous = round_bet
                    players_moved = players_moved + 1
                elif (round_bet == -1):
                    previous = round_bet
                    player_round_number = player_round_number - 1
            if (player_round_number == 1):
                actions.endRound(player_vector, False)
                continue
Exemplo n.º 9
0
def hit(Player, hand_index):
    Player.hand[hand_index] = cards.draw()