Home > OS >  Python Card game with Class and loop function
Python Card game with Class and loop function

Time:04-18

I have a problem creating a card game, since when I run the code the result is both 2 player get the same card this is the code I have, and the result of it means that they draw the same card. I can't find why the reason why two players got the same card. The requirement of this coding question is 1. It must have a function called create_deck() that creates (and returns) a deck of 52 cards ) – the deck must be created using iterative structures (ie for loop). 2.It must have a function called deal_cards(the_deck) that takes in the created deck as a parameter and deals the cards to each player as 26 random cards for each player (NOTE: the random value “simulates” shuffling the deck). Every card dealt must of course be uniqueBoth hands (for player 1 and player 2) must be returned by the function. 3. It must have a function called play_game(p1_cards, p2_cards) that accepts the hands for both player 1 and player 2 and must then “Play” the game by drawing the top card from each player, comparing them, and determining the result. Once all hands have been played, the function must return the results as the number of hands won by player 1, player 2, and the number of hands “drawn” (3 values in total).

import random

class Card():
    def __init__(self, rankvalue, rankname, suit):
        self.rankvalue = rankvalue
        self.rankname = rankname
        self.suit = suit
        
    def __lt__(self, other):
        return self.rankvalue < other.rankvalue
        
    def __gt__(self, other):
        return self.rankvalue > other.rankvalue
        
    def __eq__(self, other):
        return self.rankvalue == other.rankvalue
        
    def __str__(self):
        return self.rankname   " of "   self.suit

suits = ("Clubs","Spades","Hearts","Diamonds")
ranks = ("2","3","4","5","6","7","8","9","10","Jack","Queen","King","Ace")

def create_deck():
    
    deck = []  
    
    for s in suits:
        for i,r in enumerate(ranks):
            deck.append(Card(i 2,r,s))
    return deck

deck = create_deck()

def deal_card(the_deck):
    random.shuffle(the_deck)
    player1 = []
    player2 = []

    for s in the_deck[:26]:
        player1.append(s)
    for r in the_deck[:26]:
        player2.append(r)
    return player1, player2
    
py1 , py2 = deal_card(deck)

def play_game(p1_cards, p2_cards):

    p1_score = 0
    p2_score = 0
    draw = 0
    hand = 1

    while hand != 27:
        card_p1 = p1_cards[hand-1]
        card_p2 = p2_cards[hand-1]

        print(f"\nHand Number: {hand}")
        print(f"Player 1 has: {card_p1}")
        print(f"PLayer 2 has: {card_p2}")

        if card_p1 > card_p2: 
            print("Player 1 wins the hand")
            p1_score  = 1
        elif card_p2 > card_p1: 
            print("Player 2 wins the hand")
            p2_score  = 1
        elif card_p1 == card_p2: 
            print("DRAW!!!!!")
            draw  = 1
        hand  = 1
    print("")
    print("-"*20)
    print(f"FINAL GAME RESULT\nPlayer 1 won {p1_score}\nPlayer 2 won {p2_score}\n{draw} were draw\n")
    if p1_score > p2_score:
        return "Player 1 is the winner"
    elif p1_score < p2_score:
        return "Player 2 is the winner"
    else:
        return "It was a tie game"
result = play_game(py1, py2)
print(result)

CodePudding user response:

You only need to return the_deck in two halves once it has been shuffled:

def deal_card(the_deck):
    random.shuffle(the_deck)
    return the_deck[:26], the_deck[26:]

CodePudding user response:

Deal_card() gives the same first 26 cards to both players. Try:

def deal_card(the_deck):
    random.shuffle(the_deck)
    player1 = []
    player2 = []

    for s in the_deck[:26]:
        player1.append(s)
        the_deck.remove(s)  #new
    for r in the_deck[:26]:
        player2.append(r)
    return player1, player2
  • Related