Started making OOP blackjack

This commit is contained in:
Evelyne 2023-04-25 11:23:25 +01:00
parent 3011db7053
commit 26341b390f
2 changed files with 81 additions and 18 deletions

54
OOP/Card Game/main.py Normal file
View File

@ -0,0 +1,54 @@
# Python OOP implementation of blackjack.
from random import choice
class Card:
def __init__(self):
# Set colour as random between red and black
self.colour = choice(["red", "black"])
# Set suit depending on colour since a suit has an allocated colour
self.suit = choice(["heart", "diamond"]) if self.colour == "red" else choice(["club", "spade"])
# Set value
self.value = choice(["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"])
# Set whether the value is a face card or not
self.face = True if self.value in ["J", "Q", "K"] else False
class Player:
def __init__(self, number=None):
self.name = f"player{number}"
self.cards = []
def show_cards(self):
pass
class Dealer(Player):
def __init__(self):
super().__init__(self)
self.name = "dealer"
def show_cards(self):
...
class Game:
def __init__(self):
self.players = [Dealer()]
for player_num in range(1):
self.players.append(Player(player_num-1))
self.deal_cards()
for player in self.players:
print(player.cards)
def deal_cards(self):
for player in self.players:
for _ in range(2):
player.cards.append(Card())
if __name__ == "__main__":
game = Game()

View File

@ -15,19 +15,15 @@ class Color:
class Knight:
FIRST = ["Lancelot", "Charles", "Henry", "William"]
NICK = ["Brave", "Horrific", "Courageous", "Terrific"]
ACTION = ["ATTACK", "DEFEND", "REST"]
def __init__(self):
for i in range(3):
self.ACTION.append("ATTACK")
self.actions = ["ATTACK", "ATTACK", "ATTACK", "DEFEND", "REST"]
self.name = f"{choice(Knight.FIRST)} The {choice(Knight.NICK)}"
self.state = ""
self.state = None
self.health = 100
def action(self, player):
action = choice(Knight.ACTION)
self.state = action
self.state = choice(self.actions)
def update_health(self, player):
if player == Game.PLAYERS[0]:
@ -40,6 +36,7 @@ class Knight:
class Game:
# Do not like this being outside of init, figure out a way to have it inside
PLAYERS = []
def __init__(self):
for i in range(2):
@ -48,7 +45,7 @@ class Game:
def gameloop(self):
while self.PLAYERS[0].health > 0 and self.PLAYERS[1].health > 0:
system("clear")
system("cls" if name == "nt" else "clear")
print(f"{Color.cyan}-----------------------------------------------{Color.norm}")
for player in self.PLAYERS:
@ -61,21 +58,33 @@ class Game:
player.action(player)
match player.state:
case "ATTACK":
print(f"{player.name} attacks!")
print(f"{Color.green}{player.name} {Color.red}attacks{Color.norm} the enemy!")
case "DEFEND":
print(f"{player.name} defends from any attacks.")
print(f"{Color.green}{player.name} {Color.red}defends{Color.norm} from any attacks.")
case "REST":
print(f"{player.name} is tired and will not do anything this round")
sleep(2)
print(f"{Color.green}{player.name}{Color.norm} is tired and will {Color.red}rest{Color.norm} for this round")
sleep(1)
for player in self.PLAYERS:
player.update_health(player)
sleep(2)
self.end_game()
sleep(1)
def end_game(self):
print("Game End")
system("cls" if name == "nt" else "clear")
self.game_end()
def game_end(self):
print(f"{Color.cyan}-----------------------------------------------{Color.norm}")
for player in self.PLAYERS:
print(f"{Color.green}{player.name}{Color.norm}: {player.health}")
print(f"{Color.cyan}-----------------------------------------------{Color.norm}")
if self.PLAYERS[0].health == self.PLAYERS[1].health:
print(f"Both brave knights sadly die from their battle wounds!")
elif self.PLAYERS[0].health == 0:
print(f"{self.PLAYERS[1].name} won this mighty battle!")
else:
print(f"{self.PLAYERS[0].name} has conquered his enemy!")
if __name__ == "__main__":
game = Game()
game.gameloop()
Game().gameloop()