Added comments to Solitaire.py and main.py
This commit is contained in:
@@ -1,35 +1,40 @@
|
|||||||
from Deck import Deck
|
from Deck import Deck
|
||||||
|
"""
|
||||||
|
Defines the Solitaire game logic. Contains methods to play the game by dealing cards, removing cards based on game rules, and checking for a win condition.
|
||||||
|
|
||||||
|
Important Note: The prints in this file only run when this file is ececuted directly. This file should not be executed directly normally.
|
||||||
|
"""
|
||||||
|
|
||||||
class Solitaire:
|
class Solitaire:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.deck = Deck()
|
self.deck = Deck()
|
||||||
self.face_up = []
|
self.face_up = [] #List to hold face-up cards.
|
||||||
|
|
||||||
def deal_until_four(self):
|
def deal_until_four(self):
|
||||||
while len(self.face_up) < 4 and self.deck.number_of_cards() > 0:
|
while len(self.face_up) < 4 and self.deck.number_of_cards() > 0:
|
||||||
self.face_up.append(self.deck.deal())
|
self.face_up.append(self.deck.deal()) #Deals cards until there are four face-up cards.
|
||||||
|
|
||||||
def remove_four_same_suit(self) -> bool:
|
def remove_four_same_suit(self) -> bool:
|
||||||
if len(self.face_up) < 4:
|
if len(self.face_up) < 4:
|
||||||
return False
|
return False #Not enough cards to remove four of the same suit.
|
||||||
|
|
||||||
last_four = self.face_up[-4:]
|
last_four = self.face_up[-4:]
|
||||||
suit = last_four[0].get_suit()
|
suit = last_four[0].get_suit()
|
||||||
|
|
||||||
if all(card.get_suit() == suit for card in last_four):
|
if all(card.get_suit() == suit for card in last_four):
|
||||||
del self.face_up[-4:]
|
del self.face_up[-4:]
|
||||||
return True
|
return True #Removed four cards of the same suit.
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def remove_inner_two(self) -> bool:
|
def remove_inner_two(self) -> bool:
|
||||||
if len(self.face_up) < 4:
|
if len(self.face_up) < 4:
|
||||||
return False
|
return False #Not enough cards to remove inner two cards.
|
||||||
|
|
||||||
last_four = self.face_up[-4:]
|
last_four = self.face_up[-4:]
|
||||||
if last_four[0].get_suit() == last_four[3].get_suit():
|
if last_four[0].get_suit() == last_four[3].get_suit():
|
||||||
del self.face_up[-3:-1]
|
del self.face_up[-3:-1]
|
||||||
return True
|
return True #Removed inner two cards.
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -38,21 +43,21 @@ class Solitaire:
|
|||||||
while removed and len(self.face_up) >= 4:
|
while removed and len(self.face_up) >= 4:
|
||||||
removed = self.remove_four_same_suit()
|
removed = self.remove_four_same_suit()
|
||||||
if not removed:
|
if not removed:
|
||||||
removed = self.remove_inner_two()
|
removed = self.remove_inner_two() # Attempts to remove cards based on game rules.
|
||||||
|
|
||||||
def playGame(self) -> bool:
|
def playGame(self) -> bool:
|
||||||
self.deck.shuffle()
|
self.deck.shuffle()
|
||||||
self.face_up = []
|
self.face_up = []
|
||||||
|
|
||||||
self.deal_until_four()
|
self.deal_until_four() #Deals cards until there are four face-up cards.
|
||||||
|
|
||||||
while self.deck.number_of_cards() > 0:
|
while self.deck.number_of_cards() > 0:
|
||||||
self.remove_all_possible()
|
self.remove_all_possible()
|
||||||
self.face_up.append(self.deck.deal())
|
self.face_up.append(self.deck.deal())
|
||||||
|
|
||||||
self.remove_all_possible()
|
self.remove_all_possible()
|
||||||
return len(self.face_up) == 0
|
return len(self.face_up) == 0 # Checks for a win condition.
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
game = Solitaire()
|
game = Solitaire()
|
||||||
print("win?", game.playGame())
|
print("win?", game.playGame()) #Prints whether the game was won or not. Not normally executed.
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
"""
|
||||||
|
This is the main file that should be called to run the simulation.
|
||||||
|
"""
|
||||||
from Solitaire import Solitaire
|
from Solitaire import Solitaire
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user