Simple Card Game
- Matthew Joseph R. Banaag

- Jul 20, 2020
- 2 min read
The whole code can be found here: https://github.com/mbanaag/Programming-Projects.git
In this project I tried to practice my object oriented programming (OOP) by making a deck of cards and playing a simple game of war with it.
This will be done using Python, and the only package needed is random
import random as rWe first need to define what a "Card" is. Well, the simplest definition I can think of is that each card as a unique combination of card-faces (Ace to King) and suites (Hearts, Diamonds, Clubs, Spades).
class Card:
def __init__(self):
self.full_deck = []
self.suites = ["Hearts","Diamonds","Spades","Clubs"]
self.faces = ["A",2,3,4,5,6,7,8,9,10,"J","Q","K"]
for i in self.suites:
for j in self.faces:
self.full_deck.append([i,j])After defining what a card is, now we have to define what a whole deck of them is. We can start with:
class Deck:
def __init__(self,cards):
self.cards = cards
self.current_deck = cards.full_deckHere we see that a deck is a list of the different possible combinations of card faces and suites. But what else can we do with a deck of cards? What if we need to shuffle them? Well, we can define shuffling as:
def shuffle(self):
return r.shuffle(self.current_deck)Luckily, the package random takes care of that for us.
Now, if we want to play War with these cards, we'd need to be able to deal the cards. To do so, we first have to tell our program how many cards we want to deal.
def deal(self,name,Ncards = 0):
self.ncards = Ncards
i = 0
total_value = []
if Ncards > len(self.current_deck):
print("You're out of cards!")
if Ncards == 0:
print("You didn't draw anything!")This snippet here tells us that we have to draw at least 1 card, and we can't exceed the limit of the number of cards in the deck. But okay, now that we can select how many cards to draw, what happens when we get a card? Well, each card would have it's own face and suite.
else:
while i < Ncards:
y = self.current_deck[r.randint(0,len(self.current_deck) - 1)]
print(y)
if y[1] == 'A' or y[1] == 'J' or y[1] == 'Q' or y[1] == 'K':
total_value.append(10)
else:
total_value.append(y[1])
self.current_deck.pop(self.current_deck.index(y))
i += 1
if Ncards > len(self.current_deck):
print("You're out of cards!")Assigning the Ace, King, Queen, and Jack a value of 10, this snippet also allows the program to total the faces of the cards. Lastly, we want to make sure that cards don't repeat, so we pop them out of the list and store the number of remaining cards somewhere else.
self.cards_left = len(self.current_deck)
self.score = sum(total_value)
print('{0}, your score is'.format(name), self.score)I also created a sample match where two players draw two cards per turn. Whoever has the higher total in that round wins, and each win (and ties) are recorded and shown in the end.
Player1 = Deck(Card())
Player2 = Deck(Card())
xwins = []
ywins = []
ties = []
#You can set the range according to how many rounds you want
#The max is 25 (since there are 52 cards in a deck and Python starts counting at 0)
for i in range(25):
Player1.deal("Player 1",2)
Player2.deal("Player 2",2)
if Player1.score > Player2.score:
xwins.append(1)
elif Player2.score > Player1.score:
ywins.append(1)
else:
print('Tie!')
ties.append(1)
Player1.shuffle()
Player2.shuffle()
print('Player 1 won',sum(xwins),'times, while Player 2 won',sum(ywins),'times. There was/were',sum(ties),'ties.')

Comments