Skip to content

Commit

Permalink
code formatted with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
kpranke committed Oct 6, 2021
1 parent 541577c commit b728da9
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 106 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ For the moment, only the basic functionalities have been implemented:
- Cards are shuffled and distributed among the two players. Each player plays a card until there is no cards left.

## Objectives

- Make a good usage of classes.
- Use Object-Oriented-Programming (OOP).
- Use imports in a clean way.
- Use a clean architecture.
- Structure a project.
- Go deeper in object inheritance.

## Repo architecture
```
Expand Down
13 changes: 7 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
from utils.card import Card



player_1_name = input('Add name of player 1: ')
player_2_name = input('Add name of player 2: ')
player_1_name = input("Add name of player 1: ")
player_2_name = input("Add name of player 2: ")

player_1 = Player(player_1_name)
player_2 = Player(player_2_name)

players = [player_1, player_2]
players = [player_1, player_2]

for element in players:
print(f"Welcome, {element}!")
print(f"Welcome, {element}!")

board = Board(players)
board.start_game()

print("This is it. Game over. If you want to make this game more fancy, consider contributing to the project! Ciao!")
print(
"This is it. Game over. If you want to make this game more fancy, consider contributing to the project! Ciao!"
)
Binary file modified utils/__pycache__/player.cpython-39.pyc
Binary file not shown.
43 changes: 20 additions & 23 deletions utils/card.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
class Symbol:
'''A class to store the card symbols: icons and colors'''

def __init__(self, color, icon):
'''Initialize a symbol and set its icon and color.'''

self.icon = icon
self.color = color

def __str__(self):
'''Return a symbol which consists of an icon and a color.'''
return f"{self.color}, {self.icon}"
"""A class to store the card symbols: icons and colors"""

class Card(Symbol):
'''A class to create a card. This class inherits icon and color from the Symbol class and adds a value.'''
def __init__(self, color, icon):
"""Initialize a symbol and set its icon and color."""

self.icon = icon
self.color = color

def __init__(self, color, icon, value):
super().__init__(color, icon)
'''Initialize a card that inherits an icon and a color from Symbol and set its value.'''
self.value = value

def __str__(self):
"""Return a symbol which consists of an icon and a color."""
return f"{self.color}, {self.icon}"

def __str__(self):
'''Return a card which consists of an icon, a color and a value.'''
return f"{self.color}, {self.icon}, {self.value}"

def create_list_of_cards(self):
'''Creates a list with 4 cards'''
class Card(Symbol):
"""A class to create a card. This class inherits icon and color from the Symbol class and adds a value."""

def __init__(self, color, icon, value):
super().__init__(color, icon)
"""Initialize a card that inherits an icon and a color from Symbol and set its value."""
self.value = value

def __str__(self):
"""Return a card which consists of an icon, a color and a value."""
return f"{self.color}, {self.icon}, {self.value}"

def create_list_of_cards(self):
"""Creates a list with 4 cards"""
50 changes: 26 additions & 24 deletions utils/game.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
from utils.card import Card
from utils.player import Player, Deck
#from card import Card
#from player import Player, Deck

# from card import Card
# from player import Player, Deck


class Board:
'''A class to stores a board'''
def __init__(self, players : list):
self.players = players
self.turn_count = 0
self.active_cards = []
self.history_cards = []
"""A class to stores a board"""

def __init__(self, players: list):
self.players = players
self.turn_count = 0
self.active_cards = []
self.history_cards = []

def start_game(self):
'''
Starts the game, fills a Deck, distributes cards of the Deck to the players.
'''
print('The game has started.')
deck = Deck()
deck.fill_deck()
deck.shuffle()
deck.distribute(self.players)
def start_game(self):
"""
Starts the game, fills a Deck, distributes cards of the Deck to the players.
"""
print("The game has started.")
deck = Deck()
deck.fill_deck()
deck.shuffle()
deck.distribute(self.players)

while self.turn_count < 26:
self.turn_count += 1
print(f"Starting turn {self.turn_count}")
for player in self.players:
card = player.play(self.turn_count)
self.history_cards.append(card)
self.active_cards.append(card)
while self.turn_count < 26:
self.turn_count += 1
print(f"Starting turn {self.turn_count}")
for player in self.players:
card = player.play(self.turn_count)
self.history_cards.append(card)
self.active_cards.append(card)
106 changes: 54 additions & 52 deletions utils/player.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,69 @@
from random import shuffle

from utils.card import Card
#from card import Card

# from card import Card


class Player:
'''A class to manage a player'''
def __init__(self, name: str):
self.name = name
self.cards = [] #cards assigned
self.number_of_cards = 0
self.history = [] #what cards were assigned to the player
"""A class to manage a player"""

def __str__(self):
return self.name
def __init__(self, name: str):
self.name = name
self.cards = [] # cards assigned
self.number_of_cards = 0
self.history = [] # what cards were assigned to the player

def __str__(self):
return self.name

def play(self, turn_count : int):
'''Player picks a card in cards.'''
shuffle(self.cards)
card = self.cards.pop()
self.history.append(card)
print(f"{self.name} in turn {turn_count} played: {card}")
return card
def play(self, turn_count: int):
"""Player picks a card in cards."""
shuffle(self.cards)
card = self.cards.pop()
self.history.append(card)
print(f"{self.name} in turn {turn_count} played: {card}")
return card

class Deck:
'''A class to store a deck of cards.'''
def __init__(self):
''' Initialize a deck of cards'''
self.cards = []
self.players = []

def fill_deck(self):
icons = ["Club", "Heart", "Diamond", "Spade"]
values = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
for icon in icons:
if icon == "Heart" or icon =="Diamond":
color = "red"
else:
color = "black"
for value in values:
self.cards.append(Card(color, icon, value))
class Deck:
"""A class to store a deck of cards."""

def __init__(self):
"""Initialize a deck of cards"""
self.cards = []
self.players = []

def __str__(self):
'''Shows a deck of cards'''
string_of_cards = ''
for element in self.cards:
string_of_cards += element.__str__() + " "
return string_of_cards
def fill_deck(self):
icons = ["Club", "Heart", "Diamond", "Spade"]
values = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
for icon in icons:
if icon == "Heart" or icon == "Diamond":
color = "red"
else:
color = "black"
for value in values:
self.cards.append(Card(color, icon, value))

def shuffle(self):
''' Shuffles all cards in the list of cards. '''
shuffle(self.cards)
print("The deck has been shuffled.")
def __str__(self):
"""Shows a deck of cards"""
string_of_cards = ""
for element in self.cards:
string_of_cards += element.__str__() + " "
return string_of_cards

def distribute(self, players: list):
'''
Takes a list of players as a parameter and distributes the cards evenly between all the players.
'''
while len(self.cards) > 0:
for player in players:
if len(self.cards) > 0:
card = self.cards.pop()
player.cards.append(card)
print('Cards have been distributed.') # move to game
def shuffle(self):
"""Shuffles all cards in the list of cards."""
shuffle(self.cards)
print("The deck has been shuffled.")

def distribute(self, players: list):
"""
Takes a list of players as a parameter and distributes the cards evenly between all the players.
"""
while len(self.cards) > 0:
for player in players:
if len(self.cards) > 0:
card = self.cards.pop()
player.cards.append(card)
print("Cards have been distributed.") # move to game

0 comments on commit b728da9

Please sign in to comment.