Skip to content

Commit

Permalink
Formatting fixes and comment additions
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredRabie committed Apr 17, 2022
1 parent defbb3b commit 6e5d389
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions main_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ def __init__(self, value, suit):
self.__value = value
self.__suit = suit

#Creates string representation of Card instance to write to output.
def __str__(self):
return str(self.__value) + str(self.__suit)

#Rewrite the = operator to check if both value and suit match for two instances of Card.
def __eq__(self, other):
if self.__value == other.__value and self.__suit == other.__suit:
return True
else:
return False

#Rewrite the + operator so that for instances of Card, Card + 1 gives the next sequential card, preserving suit, so that the game can check that the cards are in order.
def __add__(self, other):
if type(other) == int:
return Card(self.__value + other, self.__suit)
Expand All @@ -34,6 +37,7 @@ def add_top(self, item):
def add_bottom(self, item):
self.__items = self.__items + [item]

#Remove and return bottom/top item from stack.
def remove_top(self):
top_item = self.__items[0]
self.__items = self.__items[1:]
Expand All @@ -45,19 +49,22 @@ def remove_bottom(self):

def size(self):
return len(self.__items)

#Remove without returning bottom/top item from stack.
def peek_top(self):
return self.__items[0]
def peek_bottom(self):
return self.__items[-1]

#String representation of stack for display during gameplay.
def print_all(self, index):
if index == 0:
string = "" #defining string here avoids throwing an error when the pile is empty
if len(self.__items) > 0: #the error would be thrown here, an empty pile does not pass this condition the string is never assigned.
if len(self.__items) > 0: #the error would be thrown here, an empty pile does not pass this condition if the string is never assigned.
string = str(self.__items[0]) + " *"*(len(self.__items)-1)
print(string)
else:
string = "" #defining string here avoids throwing an error when the pile is empty
string = ""
for item in self.__items:
string += str(item) + " "
string.strip()
Expand All @@ -68,18 +75,21 @@ def shuffle(self):

class Solitaire:
def __init__(self, cards=[]):
#Settings() creates instance of Settings class which handles read/write of settings.txt
self.__settings = Settings()
self.__full_settings_list = self.__settings.get_full_list()
self.__full_settings_list = self.__settings.get_full_list() #imports list of settings for assignment

#Assigns settings tthat werte imported above by Settings class.
self.select_difficulty()
self.__range = (1, self.__settings[1])
self.__max_num_moves = self.__settings[2]
suits_string = self.__settings[4]
self.__num_suits = self.__settings[3]
self.__suits = suits_string[:self.__settings[3]]

self.__num_piles = (self.__range[1]//8) + 3 + len(self.__suits)
self.__num_piles = (self.__range[1]//8) + 3 + len(self.__suits) #Pile number generator outlined in assignment specifications.

#Generator for piles, which are instances of CardPile, as well as generator for Card instances in each pile.
self.__piles = []
for i in range(self.__num_piles):
self.__piles.append(CardPile())
Expand All @@ -96,14 +106,17 @@ def display(self):
for index in range(self.__num_piles):
print("{}:".format(index), end=" ")
self.__piles[index].print_all(index)


#defining the move method which determines if a move is legal, then pops/pushes between stacks to move the Card instances.
def move(self, p1, p2):
#Player can spend one move to move top card to bottom of stack (also hides the moved card).
if p1 == p2 == 0:
draw_pile = self.get_pile(0)
if draw_pile.size() > 0:
top = draw_pile.remove_top()
draw_pile.add_bottom(top)

#Player can move from draw pile which reveals the next card.
elif p1 == 0 and p2 > 0:
draw_pile = self.get_pile(0)
p2_pile = self.get_pile(p2)
Expand All @@ -115,7 +128,8 @@ def move(self, p1, p2):
else:
top_p1 = draw_pile.remove_top()
p2_pile.add_bottom(top_p1)


#Player can move between two non-draw piles.
elif p1 > 0 and p2 > 0:
start_pile = self.get_pile(p1)
final_pile = self.get_pile(p2)
Expand All @@ -124,15 +138,17 @@ def move(self, p1, p2):
while start_pile.size() > 0:
top_p1 = start_pile.remove_top()
final_pile.add_bottom(top_p1)
else:
print("Illegal move! Try again.")

def is_complete(self):
#condition 1: empty starting pile
#Win condition 1: empty starting pile
draw_pile_is_empty = False
draw_pile = self.get_pile(0)
if draw_pile.size() == 0:
draw_pile_is_empty = True

#condition 2: all cards on a number of piles equal to the number of suits
#Win condition 2: all cards on a number of piles equal to the number of suits
full_piles = False
pile_sizes = [self.get_pile(i).size() for i in range(self.__num_piles)]
num_nonzeroes = 0
Expand All @@ -144,14 +160,15 @@ def is_complete(self):
if num_nonzeroes == self.__num_suits:
full_piles = True

#condition 3 (descending order) is guaranteed by there being full stacks, as the cards can't be added to the stack out of order.
#Win condition 3 (descending order) is guaranteed by there being full stacks, as the cards can't be added to the stack out of order.

#final check
#Final check
if full_piles == True and draw_pile_is_empty == True:
return True
else:
return False

#Method to fetch and validate move input from user. Uses while loops to fetch data from user until valid input is given.
def get_move_input(self, move_number):
print("Round", move_number, "out of", self.__max_num_moves, end = ": ")
row1 = None
Expand Down Expand Up @@ -182,6 +199,7 @@ def get_move_input(self, move_number):

return row1, row2


def select_difficulty(self):
full_settings_list = self.__settings.get_full_list()
change_settings_bool = input("Your current difficulty is: {}. Would you like to change difficulties? (y/n)".format(self.__settings[0]))
Expand Down Expand Up @@ -226,7 +244,7 @@ def change_custom_settings(self):
text += "2: Change Moves" + "\n"
text += "3: Change Number of Suits" + "\n"
text += "4: Change Default Suits" + "\n"
text += "5: Exit" + "\n"
text += "5: Play" + "\n"
change_custom_settings_input = input(text)
accepted_inputs = [str(x) for x in range(1, 6)]
while change_custom_settings_input not in accepted_inputs:
Expand Down Expand Up @@ -383,9 +401,8 @@ def write_settings_file(self):

def interpret_settings_file():
filename = "settings.txt"
file = open(filename, "r")
text = file.read()
file.close()
with open(filename, "r") as file:
text = file.read()

upper_list = text.split("#####")

Expand Down

0 comments on commit 6e5d389

Please sign in to comment.