
As an end-of-semester project of my introduction to computer science class in high school, we all gets to design and write a text-based game, simulating the popular casino poker card game Blackjack, which is also known as Twenty-One. I implemented this game with game rules simplified.
About a year later, in 2024, I recreated this game myself with a major improvements on graphics. The game draws a gambling table and poker cards on console or terminal with text.
The game’s rule is simple:
Here is a copy of the console during a game play:
_____________________________
| .-------------------------. |
| | | |
| | [A] [6] [#] | |
| | | |
| |_________________________| |
| | | |
| | | |
| | [2] [Q] [A] | |
| | | |
| |_________________________| |
|_____________________________|
\ \\ // /
HOUSE HAS 7 + ?
YOU HAVE 13
Hit or stand? Enter Y to hit and any other keys to stand.
The implementation of this game includes a large amount of object oriented programming. For example, a poker card is exist as an instance of class PokerCard, with attribute name and value. Another example would be the PokerDeck class, whose instance has an attribute that is a list of PokerCard objects.
After an initialization with a constructor, an instance of PokerDeck contains a sorted list of 52 PokerCard objects, which consist of four poker cards from each face value. If a player modified the code to include card e and E, more cards will appear in the PokerDeck instance. The list is shuffled before the game starts with the following method:
# Shuffles a deck with a specified time of repeat.
def shuffle(self, repeat):
for r in range(repeat):
split1 = random.randrange(0, int(self.quantity >> 1)) # Choose two random points in the deck. One from the first
split2 = random.randrange(split1, self.quantity) # half of the deck and the other from the range after it.
portion1 = self.content[:split1] # With these two points, split the entire deck into 3
portion2 = self.content[split1:split2] # parts.
portion3 = self.content[split2:]
self.content = portion1 + portion3 + portion2 # Switch the order of these parts and combine them.
The shuffle method literally simulates how I would shuffle a deck of poker card!
While writing code for this game, I found that object oriented programming simplifies the task, because I can think in the way that I think about stuffs I see in real life. For example, the attribute of a poker card can be any information related to it, such as its name, face value, color, shape, just to name a few. The methods are like things one can do to a stuff, such as shuffling a deck of poker card. It was nice that an object groups all related information and method together, making reference or use of any of them often be as simple as a variable name plus a period plus the attribute or method needed. I am free of finding variables and functions that may have defined all around the project files for one operation regarding one thing. This is why I like object oriented programming. By doing this project, I learned and experienced tremendously about object oriented programming.