made project 2
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
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.
|
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.
|
||||||
|
|
||||||
|
|||||||
22
Project 2/Card.py
Normal file
22
Project 2/Card.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
class Card:
|
||||||
|
"""
|
||||||
|
Represents a playing card with a suit and value.
|
||||||
|
"""
|
||||||
|
def __init__(self, suit: str, value: str):
|
||||||
|
self._suit = suit
|
||||||
|
self._value = value
|
||||||
|
#Creates a Card object with the specified suit and value.
|
||||||
|
|
||||||
|
def get_suit(self) -> str:
|
||||||
|
return self._suit
|
||||||
|
|
||||||
|
#Returns the suit of the card.
|
||||||
|
|
||||||
|
def get_value(self) -> str:
|
||||||
|
return self._value
|
||||||
|
#Returns the value of the card.
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return f"{self._value} of {self._suit}"
|
||||||
|
|
||||||
|
#Returns a string representation of the card in the format "Value of Suit".
|
||||||
41
Project 2/Deck.py
Normal file
41
Project 2/Deck.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
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= []
|
||||||
|
self._next_card = 0
|
||||||
|
|
||||||
|
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades'] #the possible suits
|
||||||
|
values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace'] #the possible values
|
||||||
|
|
||||||
|
for suit in suits:
|
||||||
|
for value in values:
|
||||||
|
self._cards.append(Card(suit, value)) #creates a standard 52-card deck
|
||||||
|
|
||||||
|
def shuffle(self):
|
||||||
|
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 #Returns None if there are no cards left to deal.
|
||||||
|
card = self._cards[self._next_card]
|
||||||
|
self._next_card += 1 #Advances the next card index.
|
||||||
|
return card
|
||||||
|
|
||||||
|
def number_of_cards(self) -> int:
|
||||||
|
return len(self._cards) - self._next_card #Returns the number of remaining cards in the deck.
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
deck = Deck()
|
||||||
|
deck.shuffle() #Calls the shuffle method to randomize the deck.
|
||||||
|
for _ in range(5):
|
||||||
|
print(deck.deal())
|
||||||
|
print("Cards left:", deck.number_of_cards()) #Prints the number of cards left in the deck after dealing five cards.
|
||||||
BIN
Project 2/Printed Version.pdf
Normal file
BIN
Project 2/Printed Version.pdf
Normal file
Binary file not shown.
63
Project 2/Solitaire.py
Normal file
63
Project 2/Solitaire.py
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
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:
|
||||||
|
def __init__(self):
|
||||||
|
self.deck = Deck()
|
||||||
|
self.face_up = [] #List to hold face-up cards.
|
||||||
|
|
||||||
|
def deal_until_four(self):
|
||||||
|
while len(self.face_up) < 4 and self.deck.number_of_cards() > 0:
|
||||||
|
self.face_up.append(self.deck.deal()) #Deals cards until there are four face-up cards.
|
||||||
|
|
||||||
|
def remove_four_same_suit(self) -> bool:
|
||||||
|
if len(self.face_up) < 4:
|
||||||
|
return False #Not enough cards to remove four of the same suit.
|
||||||
|
|
||||||
|
last_four = self.face_up[-4:]
|
||||||
|
suit = last_four[0].get_suit()
|
||||||
|
|
||||||
|
if all(card.get_suit() == suit for card in last_four):
|
||||||
|
del self.face_up[-4:]
|
||||||
|
return True #Removed four cards of the same suit.
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def remove_inner_two(self) -> bool:
|
||||||
|
if len(self.face_up) < 4:
|
||||||
|
return False #Not enough cards to remove inner two cards.
|
||||||
|
|
||||||
|
last_four = self.face_up[-4:]
|
||||||
|
if last_four[0].get_suit() == last_four[3].get_suit():
|
||||||
|
del self.face_up[-3:-1]
|
||||||
|
return True #Removed inner two cards.
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def remove_all_possible(self):
|
||||||
|
removed = True
|
||||||
|
while removed and len(self.face_up) >= 4:
|
||||||
|
removed = self.remove_four_same_suit()
|
||||||
|
if not removed:
|
||||||
|
removed = self.remove_inner_two() # Attempts to remove cards based on game rules.
|
||||||
|
|
||||||
|
def playGame(self) -> bool:
|
||||||
|
self.deck.shuffle()
|
||||||
|
self.face_up = []
|
||||||
|
|
||||||
|
self.deal_until_four() #Deals cards until there are four face-up cards.
|
||||||
|
|
||||||
|
while self.deck.number_of_cards() > 0:
|
||||||
|
self.remove_all_possible()
|
||||||
|
self.face_up.append(self.deck.deal())
|
||||||
|
|
||||||
|
self.remove_all_possible()
|
||||||
|
return len(self.face_up) == 0 # Checks for a win condition.
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
game = Solitaire()
|
||||||
|
print("win?", game.playGame()) #Prints whether the game was won or not. Not normally executed.
|
||||||
BIN
Project 2/__pycache__/Card.cpython-314.pyc
Normal file
BIN
Project 2/__pycache__/Card.cpython-314.pyc
Normal file
Binary file not shown.
BIN
Project 2/__pycache__/Deck.cpython-314.pyc
Normal file
BIN
Project 2/__pycache__/Deck.cpython-314.pyc
Normal file
Binary file not shown.
BIN
Project 2/__pycache__/Solitaire.cpython-314.pyc
Normal file
BIN
Project 2/__pycache__/Solitaire.cpython-314.pyc
Normal file
Binary file not shown.
21
Project 2/main.py
Normal file
21
Project 2/main.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
"""
|
||||||
|
This is the main file that should be called to run the simulation.
|
||||||
|
"""
|
||||||
|
from Solitaire import Solitaire
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
wins = 0
|
||||||
|
|
||||||
|
for games in range(1, 10001):
|
||||||
|
game = Solitaire()
|
||||||
|
if game.playGame():
|
||||||
|
wins += 1
|
||||||
|
|
||||||
|
if games % 1000 == 0:
|
||||||
|
percent = (wins / games) * 100
|
||||||
|
print(f"{wins}/{games} games won = {percent:.2f}%")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user