Home > Mobile >  Making card game, how do I give the face cards value
Making card game, how do I give the face cards value

Time:01-12

Im making a card game for a project for my high school class. I'm making black jack but with changed rules and new features. I am using the random plugin to randomize the numbers. I am not using suits but am using face cards. How do I make it so if a user gets a face card like 'King' the program knows the value of the face card and able to calculate the total.

For example if one user gets a 3 card and a King card their total will be 13 as King = 10. How will I make the face cards have values and put it into a list with the normal cards

Here is my code so far, I know how to deal more cards later, just want to know how to add face cards and their value to shuffle. Thanks

    while len(dealer_cards) != 2:
    dealer_cards.append(random.randint(2, 10))
    if len(dealer_cards) == 2:
        print("The Dealer has:", dealer_cards)
# Players cards
while len(player_cards) != 2:
    player_cards.append(random.randint(2, 10))
    if len(player_cards) == 2:
        print("You have:", player_cards)

CodePudding user response:

One way is to define the card as an object (dictionary)

Note that this is just a way to build your logic: This doesn't take into effect other rules (like how many total cards and if the same card gets dealt out twice. You need more rules and house-keeping for that

card = {
   "face": "K",   # This indicates what is printed on the face
   "value": 10,  # This indicates the value (used internally)
   "suite": "spade",  # will be one of ["spade", "jack", "clubs", "hearts"]
}

define a function to get a random card:

def getRandomCard():
    # list of possible cards with their values
    cards = [("1", 1), ("2", 2).. ("J", <value>), ("Q", <value>), ("K", <value>), ("A", <value>)]

    randIdx = <Get random number>
    randomCard, randomCardValue = cards[randIdx % len(cards)]  # 

    suits = ["spade", "jack", "clubs", "hearts"]
   
    randomSuite = <similarly pick a random suite>
    return {
       "face": randomCard,
       "value": randomCardValue,
       "suite": randomSuite
    }

Now rest of your code deals with the card object. you get the idea..

CodePudding user response:

I hate doing your homework but I quickly wrote this bare bones blackjack game. It doesn't have betting, splitting, or even the logic to say if who wins but it does the basic game play. I hope you can look at this example of Object Oriented programming and maybe learn something. If you have any questions I will be glad to respond.

import random

class Card:
    suits = {0:"Spades",
             1:"Heaarts",
             2:"Clubs",
             3:"Dimods"}
    names = {1:"Ace",
             2:"Duce",
             3:"Three",
             4:"Four",
             5:"Five",
             6:"Six",
             7:"Seven",
             8:"Eight",
             9:"Nine",
             10:"Ten",
             11:"Jack",
             12:"Queen",
             0:"King"}
    def __init__(self,id):
        self.id = id
        self.name = id
        self.suit = int((id-1)/13)
    def announce(self):
        return Card.names[self.name] " of " Card.suits[self.suit]
    def value(self):
        if self.name > 10:
            return 10
        if self.name == 1:
            return 0
        else:
            return self.name


class Deck:
    def __init__(self):
        self.cards=[int(k) for k in range(1,53)]
    def restock(self):
        self.cards = range(1,53)
    def deal(self):
        cardIndex = random.choice(range(len(self.cards)))
        cardId = self.cards[cardIndex]
        del self.cards[cardIndex]
        return Card(cardId)

def evaluate(hand):
    total = 0
    aces = 0
    for c in hand:
        v = c.value()
        if v:
            total = v
        else:
            aces =1
    while aces > 0:
        aces-=1
        if total<11-aces:
            total =11
        else:
            total =1
    return 0 if total>21 else total

class Player:
    deck = Deck()
    def __init__(self):
        self.hand = []
    def getCard(self):
        self.hand.append(self.deck.deal())
    def discard(self):
        self.hand = []
    def play(self):
        print("Player has:")
        print(self.hand[0].announce())
        print(self.hand[1].announce())
        hitOrStand="hit"
        v = evaluate(self.hand)
        while hitOrStand != "stand" and v:
            hitOrStand = input("hit or stand:")
            if hitOrStand not in ["hit","stand"]:
                print("please enter `hit` or `stand`.")
                continue
            if hitOrStand == "hit":
                self.getCard()
                print(self.hand[-1].announce())
            v = evaluate(self.hand)
            print(v if v else "bust")

class Dealer(Player):
    def hits(self):
        v = evaluate(self.hand)
        return True if v<17 and v else False
    def play(self):
        print("Dealer has:")
        print(self.hand[0].announce())
        print(self.hand[1].announce())
        while self.hits():
            print("And gets:")
            self.getCard()
            print(self.hand[-1].announce())
            v = evaluate(self.hand)
        v = evaluate(self.hand)
        print(v if v else "bust")
    def restock(self):
        Player.deck.restock()

class Game:
    def __init__(self):
        self.player = Player()
        self.dealer = Dealer()
    def playRound(self):
        self.player.getCard()
        self.dealer.getCard()
        self.player.getCard()
        self.dealer.getCard()
        self.player.play()
        self.dealer.play()
        self.player.discard()
        self.dealer.discard()
        self.dealer.restock()

g = Game()
g.playRound() 
  •  Tags:  
  • Related