# Lab Assignment 14

.py

School

Weber State University *

*We aren’t endorsed by this school

Course

1400

Subject

Computer Science

Date

Apr 30, 2024

Type

py

Pages

2

Uploaded by CountOkapiPerson1132

Report
# Lab Assignment 14: Card Clash import random # Card classes' info for every card class Card: def __init__(self, faceValue, suit): self.faceValue = faceValue self.suit = suit # Display face value/type of cards in print def display(self): print(f"{self.faceValue} of {self.suit}") # Deck class contains info for card deck class Deck: def __init__(self, acesLow, addJokers): self.acesLow = acesLow self.addJokers = addJokers self.stack = list() self.reset() # Reset returns unshuffles deck def reset(self): self.stack.clear() suits = ["Spades", "Clubs", "Diamonds", "Hearts"] face_values = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"] for suit in suits: for face_value in face_values: self.stack.append(Card(face_value, suit)) if self.addJokers: self.stack.append(Card("Joker", "")) # Shuffle deck/draw cards def shuffle(self): random.shuffle(self.stack) def draw(self): return self.stack.pop(0) # Game classes for card game class CardGame: def __init__(self): self.deck = Deck(acesLow=False, addJokers=False) self.round_number = 0 self.player_wins = 0 self.computer_wins = 0 # Deal cards for new round def deal_cards(self): self.round_number += 1 print(f"\nRound {self.round_number}:") self.deck.reset() self.deck.shuffle() self.player_hand = [self.deck.draw() for _ in range(3)] self.computer_hand = [self.deck.draw() for _ in range(3)] print("\nYour hand:") for i, card in enumerate(self.player_hand): print(f"{i + 1}: {card.faceValue} of {card.suit}")
# Determine round winners def determine_round_winner(self, player_card, computer_card): if player_card.faceValue == computer_card.faceValue: return "Tie" elif player_card.faceValue > computer_card.faceValue: return "Player" else: return "Computer" # Play a round def play_round(self): print("Choose a card to play (1-3): ") chosen_card_index = int(input()) - 1 player_card = self.player_hand.pop(chosen_card_index) print(f"\nYou played: {player_card.faceValue} of {player_card.suit}") # Computer's turn: Choose a random card to play computer_card_index = random.randint(0, 2) computer_card = self.computer_hand.pop(computer_card_index) print(f"Computer played: {computer_card.faceValue} of {computer_card.suit}") # Determine round winners winner = self.determine_round_winner(player_card, computer_card) print(f"\nThe winner of this round is: {winner}") # Update score based on round winner if winner == "Player": self.player_wins += 1 elif winner == "Computer": self.computer_wins += 1 # Start game def play(self): print("Welcome to Card Clash!") print("You'll be dealt 3 cards, and so will the computer.") print("Choose the card in your hand with the highest value to pit against the computer's to win.") print("The player who wins 2 out of 3 rounds wins the game.") print("Let's play!") while True: # Deal cards to play a round self.deal_cards() self.play_round() # Check if either player won 2 rounds if self.player_wins == 2: print("\nCongratulations! You won!") break elif self.computer_wins == 2: print("\nComputer wins. Better luck next time!") break else: print("\nNo one has won 2 rounds yet. Starting the next round...") game = CardGame() game.play()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help