Started making OOP blackjack
This commit is contained in:
parent
3011db7053
commit
26341b390f
54
OOP/Card Game/main.py
Normal file
54
OOP/Card Game/main.py
Normal 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()
|
45
OOP/main.py
45
OOP/main.py
@ -15,19 +15,15 @@ class Color:
|
|||||||
class Knight:
|
class Knight:
|
||||||
FIRST = ["Lancelot", "Charles", "Henry", "William"]
|
FIRST = ["Lancelot", "Charles", "Henry", "William"]
|
||||||
NICK = ["Brave", "Horrific", "Courageous", "Terrific"]
|
NICK = ["Brave", "Horrific", "Courageous", "Terrific"]
|
||||||
ACTION = ["ATTACK", "DEFEND", "REST"]
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
for i in range(3):
|
self.actions = ["ATTACK", "ATTACK", "ATTACK", "DEFEND", "REST"]
|
||||||
self.ACTION.append("ATTACK")
|
|
||||||
self.name = f"{choice(Knight.FIRST)} The {choice(Knight.NICK)}"
|
self.name = f"{choice(Knight.FIRST)} The {choice(Knight.NICK)}"
|
||||||
self.state = ""
|
self.state = None
|
||||||
self.health = 100
|
self.health = 100
|
||||||
|
|
||||||
def action(self, player):
|
def action(self, player):
|
||||||
action = choice(Knight.ACTION)
|
self.state = choice(self.actions)
|
||||||
self.state = action
|
|
||||||
|
|
||||||
|
|
||||||
def update_health(self, player):
|
def update_health(self, player):
|
||||||
if player == Game.PLAYERS[0]:
|
if player == Game.PLAYERS[0]:
|
||||||
@ -40,6 +36,7 @@ class Knight:
|
|||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
|
# Do not like this being outside of init, figure out a way to have it inside
|
||||||
PLAYERS = []
|
PLAYERS = []
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
for i in range(2):
|
for i in range(2):
|
||||||
@ -48,7 +45,7 @@ class Game:
|
|||||||
def gameloop(self):
|
def gameloop(self):
|
||||||
while self.PLAYERS[0].health > 0 and self.PLAYERS[1].health > 0:
|
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}")
|
print(f"{Color.cyan}-----------------------------------------------{Color.norm}")
|
||||||
for player in self.PLAYERS:
|
for player in self.PLAYERS:
|
||||||
@ -61,21 +58,33 @@ class Game:
|
|||||||
player.action(player)
|
player.action(player)
|
||||||
match player.state:
|
match player.state:
|
||||||
case "ATTACK":
|
case "ATTACK":
|
||||||
print(f"{player.name} attacks!")
|
print(f"{Color.green}{player.name} {Color.red}attacks{Color.norm} the enemy!")
|
||||||
case "DEFEND":
|
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":
|
case "REST":
|
||||||
print(f"{player.name} is tired and will not do anything this round")
|
print(f"{Color.green}{player.name}{Color.norm} is tired and will {Color.red}rest{Color.norm} for this round")
|
||||||
sleep(2)
|
sleep(1)
|
||||||
|
|
||||||
for player in self.PLAYERS:
|
for player in self.PLAYERS:
|
||||||
player.update_health(player)
|
player.update_health(player)
|
||||||
sleep(2)
|
sleep(1)
|
||||||
self.end_game()
|
|
||||||
|
|
||||||
def end_game(self):
|
system("cls" if name == "nt" else "clear")
|
||||||
print("Game End")
|
|
||||||
|
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__":
|
if __name__ == "__main__":
|
||||||
game = Game()
|
Game().gameloop()
|
||||||
game.gameloop()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user