Changes from CAT session
This commit is contained in:
@@ -27,12 +27,18 @@ class Deck:
|
|||||||
self._cards.append(Card(suit, value)) #creates a standard 52-card deck
|
self._cards.append(Card(suit, value)) #creates a standard 52-card deck
|
||||||
|
|
||||||
def shuffle(self):
|
def shuffle(self):
|
||||||
|
"""
|
||||||
|
Shuffles the deck of cards using the Fisher-Yates algorithm. This algorithm iterates through the deck and swaps each card with a randomly selected card from the remaining unshuffled portion of the deck. After shuffling, it resets the next card index to 0.
|
||||||
|
"""
|
||||||
for i in range(len(self._cards)): #iterates through each card in the deck
|
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
|
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._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.
|
self._next_card = 0 #Resets the next card index after shuffling.
|
||||||
|
|
||||||
def deal(self):
|
def deal(self):
|
||||||
|
"""
|
||||||
|
Deals the next card from the deck. Returns None if there are no cards left to deal.
|
||||||
|
"""
|
||||||
if self._next_card >= len(self._cards):
|
if self._next_card >= len(self._cards):
|
||||||
return None #Returns None if there are no cards left to deal.
|
return None #Returns None if there are no cards left to deal.
|
||||||
card = self._cards[self._next_card]
|
card = self._cards[self._next_card]
|
||||||
|
|||||||
2
Project 1/test.py
Normal file
2
Project 1/test.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
for i in range (0, 10):
|
||||||
|
print(i)
|
||||||
@@ -26,12 +26,18 @@ class Deck:
|
|||||||
self._cards.append(Card(suit, value)) #creates a standard 52-card deck
|
self._cards.append(Card(suit, value)) #creates a standard 52-card deck
|
||||||
|
|
||||||
def shuffle(self):
|
def shuffle(self):
|
||||||
|
"""
|
||||||
|
Shuffles the deck of cards using the Fisher-Yates algorithm. This algorithm iterates through the deck and swaps each card with a randomly selected card from the remaining unshuffled portion of the deck. After shuffling, it resets the next card index to 0.
|
||||||
|
"""
|
||||||
for i in range(len(self._cards)): #iterates through each card in the deck
|
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
|
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._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.
|
self._next_card = 0 #Resets the next card index after shuffling.
|
||||||
|
|
||||||
def deal(self):
|
def deal(self):
|
||||||
|
"""
|
||||||
|
Deals the next card from the deck. Returns None if there are no cards left to deal.
|
||||||
|
"""
|
||||||
if self._next_card >= len(self._cards):
|
if self._next_card >= len(self._cards):
|
||||||
return None #Returns None if there are no cards left to deal.
|
return None #Returns None if there are no cards left to deal.
|
||||||
card = self._cards[self._next_card]
|
card = self._cards[self._next_card]
|
||||||
@@ -41,6 +47,13 @@ class Deck:
|
|||||||
def number_of_cards(self) -> int:
|
def number_of_cards(self) -> int:
|
||||||
return len(self._cards) - self._next_card #Returns the number of remaining cards in the deck.
|
return len(self._cards) - self._next_card #Returns the number of remaining cards in the deck.
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
"""
|
||||||
|
Returns a string representation of the entire deck,
|
||||||
|
with one card per line.
|
||||||
|
"""
|
||||||
|
return "\n".join(str(card) for card in self._cards)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
deck = Deck()
|
deck = Deck()
|
||||||
deck.shuffle() #Calls the shuffle method to randomize the deck.
|
deck.shuffle() #Calls the shuffle method to randomize the deck.
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Project 2/__pycache__/main.cpython-314.pyc
Normal file
BIN
Project 2/__pycache__/main.cpython-314.pyc
Normal file
Binary file not shown.
BIN
Project 2/__pycache__/utilities.cpython-314.pyc
Normal file
BIN
Project 2/__pycache__/utilities.cpython-314.pyc
Normal file
Binary file not shown.
@@ -41,3 +41,5 @@ def test_sum_cards():
|
|||||||
]
|
]
|
||||||
assert sum_cards_iter(cards) == 21
|
assert sum_cards_iter(cards) == 21
|
||||||
assert sum_cards_recursive(cards) == 21
|
assert sum_cards_recursive(cards) == 21
|
||||||
|
|
||||||
|
test_sum_cards()
|
||||||
Reference in New Issue
Block a user