Last active
July 18, 2021 05:23
-
-
Save christophergandrud/79c54c49d76b8879508ef9b1ecc29f24 to your computer and use it in GitHub Desktop.
Create the card game "War" where the player with the most cards at the end of 1 round wins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Create the card game "War" | |
where the player with the most cards at the end of 1 round wins | |
(written using GitHub Copilot) | |
""" | |
# Create a range with max from len(x) | |
def range_len(x) : | |
""" | |
Return the range of the length of x | |
""" | |
return range(1, len(x)) | |
# Function to create deck of cards | |
def create_deck() : | |
""" | |
Create a deck of cards as an array of tuples | |
""" | |
deck = [] | |
for suit in range(4) : | |
for rank in range(1, 14) : | |
deck.append((suit, rank)) | |
return deck | |
# Function to shuffle the deck of cards | |
def shuffle_deck(deck) : | |
""" | |
Shuffle the deck of cards | |
""" | |
import random | |
random.shuffle(deck) | |
return deck | |
# Deal our_deck to the player and computer | |
def deal_cards(deck) : | |
""" | |
Deal the cards to the player and computer | |
""" | |
our_deck = [] | |
for i in range(26) : | |
our_deck.append(deck.pop()) | |
return our_deck | |
# Play the game | |
def play_game(player_deck, computer_deck) : | |
""" | |
For each turn, compare the card in the player's deck and the computer's deck | |
The higher ranked card wins and is added to the winner's deck out_deck | |
""" | |
player_out_deck, computer_out_deck = [], [] | |
for i in range_len(player_deck) : | |
if player_deck[i] > computer_deck[i] : | |
player_out_deck.append(computer_deck[i]) | |
else : | |
computer_out_deck.append(player_deck[i]) | |
return player_out_deck, computer_out_deck | |
# Find game winner | |
def find_winner(player_deck, computer_deck) : | |
""" | |
Find the winner of the game from a two player game | |
""" | |
if len(player_deck) > len(computer_deck) : | |
print("Player wins!") | |
elif len(player_deck) < len(computer_deck) : | |
print("Computer wins!") | |
else : | |
print("It's a tie!") | |
# -------------------------- | |
# Play the game | |
# Create the deck of cards | |
our_deck = create_deck() | |
# Shuffle our_deck of cards | |
shuffled_deck = shuffle_deck(our_deck) | |
# Deal our_deck to the player and computer | |
player_deck = deal_cards(shuffled_deck) | |
computer_deck = deal_cards(shuffled_deck) | |
# Assert that player_deck and computer_deck have no duplicate cards | |
assert len(set(player_deck) & set(computer_deck)) == 0 | |
# Assert that player_dec and computer_deck are the same length | |
assert len(player_deck) == len(computer_deck) | |
# Play the game | |
player_deck, computer_deck = play_game(player_deck, computer_deck) | |
# Determine the winner of the game | |
find_winner(player_deck, computer_deck) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment