Skip to content

Instantly share code, notes, and snippets.

@cgbeutler
Created January 5, 2019 19:39
Show Gist options
  • Save cgbeutler/95b8a16f3d5441c1eea3acd2734d9be5 to your computer and use it in GitHub Desktop.
Save cgbeutler/95b8a16f3d5441c1eea3acd2734d9be5 to your computer and use it in GitHub Desktop.
Card Game
from random import randint
club = u'\u2663'
diamond = u'\u2666'
heart = u'\u2665'
spade = u'\u2660'
card_suits = [club, diamond, heart, spade]
card_values = [
(" A", 1), (" 2", 2), (" 3", 3), (" 4", 4), (" 5", 5), (" 6", 6),
(" 7", 7), (" 8", 8), (" 9", 9), ("10", 10), (" J", 10), (" Q", 10),
(" K", 0)
]
def inputChoice(prompt, options):
while True:
choice = input(prompt)
if choice.isdigit():
choice = int(choice)
else:
print("Please enter a number")
continue
if choice not in options:
print("Invalid choice")
continue
return choice
class Card():
def __init__(self, suit, points):
self.suit = suit
self.points = points
def __str__(self):
return self.suit
class Deck():
def __init__(self):
self.cards = []
for (card_type, card_value) in card_values:
for card_suit in card_suits:
self.cards += [Card(card_type + card_suit, card_value)]
def draw(self):
drawn_card = self.cards[randint(0, len(self.cards)-1)]
self.cards.remove(drawn_card)
return drawn_card
class DiscardPile():
def __init__(self):
self.cards = []
def peek(self):
if len(self.cards) > 0:
return self.cards[-1]
else:
return None
def draw(self):
drawn_card = self.cards[-1]
self.cards.remove(drawn_card)
return drawn_card
def discard(self, trash_card):
self.cards += [ trash_card ]
class Player():
def __init__(self, initialCards):
self.board = [
initialCards[0:2],
initialCards[2:4]
]
def peekBoard(self):
print("Your board:")
print(" ".join([str(card) for card in self.board[0]]))
for row in self.board[1:]:
print(" ".join(["???" for r in range(len(row))]))
def showBoard(self):
print("Your board:")
print(" ".join([str(card) for card in self.board[0]]))
print(" ".join([str(card) for card in self.board[1]]))
def play(self, deck, discard):
self.peekBoard()
print("Discard Pile: " + str(discard.peek()))
choice = inputChoice("\n" +
"What do you want to do?\n" +
"1-Draw Deck 2-Draw Discard 3-End Game\n" +
"> ",
[1, 2, 3])
print("")
hand = None
fromDeck = None
if choice == 1:
hand = deck.draw()
fromDeck = True
print("You picked up a " + str(hand))
elif choice == 2:
hand = discard.draw()
fromDeck = False
print("You picked up a " + str(hand))
elif choice == 3:
# Knock to make this the last round
return True
choice = None
if fromDeck:
choice = inputChoice("\n" +
"Where do you want to play your " + str(hand) + "?\n" +
"1-Discard 2-My Grid\n" +
"> ",
[1, 2])
if choice == 1:
discard.discard(hand)
return False
self.peekBoard()
choice = inputChoice("\n" +
"Where do you want to play your card?\n" +
" 1 2\n" +
" 3 4\n" +
"> ",
[1, 2, 3, 4])
if choice == 1:
oldCard = self.board[0][0]
self.board[0][0] = hand
elif choice == 2:
oldCard = self.board[0][1]
self.board[0][1] = hand
elif choice == 3:
oldCard = self.board[1][0]
self.board[1][0] = hand
elif choice == 4:
oldCard = self.board[1][1]
self.board[1][1] = hand
hand = None
discard.discard(oldCard)
return False
def getScore(self):
score = 0
for row in self.board:
for card in row:
score += card.points
return score
class Game():
def __init__(self):
self.deck = Deck()
self.discard = DiscardPile()
self.player1 = Player([self.deck.draw() for r in range(4)])
self.player2 = Player([self.deck.draw() for r in range(4)])
self.discard.discard(self.deck.draw())
self.lastRound = False
def play(self):
while(not self.lastRound):
print("\n== Player 1's Turn ==")
self.lastRound = self.player1.play(self.deck, self.discard)
print("\n== Player 2's Turn ==")
self.lastRound = self.player2.play(self.deck, self.discard) or self.lastRound
print("")
print("Player 1: " + str(self.player1.getScore()))
self.player1.showBoard()
print("Player 2: " + str(self.player2.getScore()))
self.player2.showBoard()
if self.player1.getScore() == self.player2.getScore():
print("\n== The game has ended with a tie! ==\n")
elif self.player1.getScore() < self.player2.getScore():
print("\n== Player 1 has won the game! ==\n")
else:
print("\n== Player 2 has won the game! ==\n")
while (True):
print("Let's play Golf!")
choice = inputChoice(
"1 - Play 2 - Quit\n" +
"> ",
[1, 2])
if choice == 1:
Game().play()
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment