Added comments to Deck.py
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
import random
|
import random
|
||||||
from Card import Card
|
from Card import Card
|
||||||
|
|
||||||
|
"""
|
||||||
|
Defines the deck of cards used in the Solitaire game. Contains methods to shuffle the deck, deal cards, and check the number of remaining cards.
|
||||||
|
|
||||||
|
Important Note: The prints in this file only run when this file is ececuted directly. This file should not be executed directly normally.
|
||||||
|
"""
|
||||||
class Deck:
|
class Deck:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._cards= []
|
self._cards= []
|
||||||
@@ -11,27 +16,27 @@ class Deck:
|
|||||||
|
|
||||||
for suit in suits:
|
for suit in suits:
|
||||||
for value in values:
|
for value in values:
|
||||||
self._cards.append(Card(suit, value))
|
self._cards.append(Card(suit, value)) #creates a standard 52-card deck
|
||||||
|
|
||||||
def shuffle(self):
|
def shuffle(self):
|
||||||
for i in range(len(self._cards)):
|
for i in range(len(self._cards)): #iterates through each card in the deck
|
||||||
j = random.randrange(i, len(self._cards))
|
j = random.randrange(i, len(self._cards)) #selects a random index from i to the end of the deck
|
||||||
self._cards[i], self._cards[j] = self._cards[j], self._cards[i]
|
self._cards[i], self._cards[j] = self._cards[j], self._cards[i] #Shuffles the deck
|
||||||
self._next_card = 0
|
self._next_card = 0 #Resets the next card index after shuffling.
|
||||||
|
|
||||||
def deal(self):
|
def deal(self):
|
||||||
if self._next_card >= len(self._cards):
|
if self._next_card >= len(self._cards):
|
||||||
return None
|
return None #Returns None if there are no cards left to deal.
|
||||||
card = self._cards[self._next_card]
|
card = self._cards[self._next_card]
|
||||||
self._next_card += 1
|
self._next_card += 1 #Advances the next card index.
|
||||||
return card
|
return card
|
||||||
|
|
||||||
def number_of_cards(self) -> int:
|
def number_of_cards(self) -> int:
|
||||||
return len(self._cards) - self._next_card
|
return len(self._cards) - self._next_card #Returns the number of remaining cards in the deck.
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
deck = Deck()
|
deck = Deck()
|
||||||
deck.shuffle()
|
deck.shuffle() #Calls the shuffle method to randomize the deck.
|
||||||
for _ in range(5):
|
for _ in range(5):
|
||||||
print(deck.deal())
|
print(deck.deal())
|
||||||
print("Cards left:", deck.number_of_cards())
|
print("Cards left:", deck.number_of_cards()) #Prints the number of cards left in the deck after dealing five cards.
|
||||||
Reference in New Issue
Block a user