From 685981814f4bd32ebca5f124b45801b67b4058cc Mon Sep 17 00:00:00 2001 From: Benjamin Adovasio Date: Thu, 29 Jan 2026 12:27:26 -0500 Subject: [PATCH] Added comments to Deck.py --- Project 1/Deck.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/Project 1/Deck.py b/Project 1/Deck.py index ac7cb28..df0c047 100644 --- a/Project 1/Deck.py +++ b/Project 1/Deck.py @@ -1,6 +1,11 @@ import random 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: def __init__(self): self._cards= [] @@ -11,27 +16,27 @@ class Deck: for suit in suits: 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): - for i in range(len(self._cards)): - j = random.randrange(i, len(self._cards)) - self._cards[i], self._cards[j] = self._cards[j], self._cards[i] - self._next_card = 0 + for i in range(len(self._cards)): #iterates through each card in the deck + 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] #Shuffles the deck + self._next_card = 0 #Resets the next card index after shuffling. def deal(self): - if self._next_card >= len(self._cards): - return None + if self._next_card >= len(self._cards): + return None #Returns None if there are no cards left to deal. card = self._cards[self._next_card] - self._next_card += 1 + self._next_card += 1 #Advances the next card index. return card 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__": deck = Deck() - deck.shuffle() + deck.shuffle() #Calls the shuffle method to randomize the deck. for _ in range(5): print(deck.deal()) - print("Cards left:", deck.number_of_cards()) \ No newline at end of file + print("Cards left:", deck.number_of_cards()) #Prints the number of cards left in the deck after dealing five cards. \ No newline at end of file