Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Created on Thu Mar 12 12:36:46 2020
- @author: mcaggiano2022
- """
- import random
- import math
- from cards import Deck
- from cards import Card
- def setup():
- """
- paramaters: None (deck can be created within this function)
- returns:
- - a foundation (list of 4 empty lists)
- - cell (list of 4 empty lists)
- - a tableau (a list of 8 lists, the dealt cards)
- """
- global gametime
- gametime = 0
- d = Deck()
- d.shuffle()
- foundation = [[], [], [], []]#placeholder, needs replaced!!
- #placeholder, needs replaced!!
- tableau1 = [] #placeholder, needs replaced!!
- tableau2 = []
- tableau3 = []
- tableau4 = []
- tableau5 = []
- tableau6 = []
- tableau7 = []
- tableau8 = []
- global cell
- cell = [(),(),(),()]
- for i in range(0,7):
- tableau1.append(d.deal())
- for i in range(7,14):
- tableau2.append(d.deal())
- for i in range(14,21):
- tableau3.append(d.deal())
- for i in range(21,28):
- tableau4.append(d.deal())
- for i in range(28,34):
- tableau5.append(d.deal())
- for i in range(34,40):
- tableau6.append(d.deal())
- for i in range(40,46):
- tableau7.append(d.deal())
- for i in range(46,52):
- tableau8.append(d.deal())
- tableau = [tableau1, tableau2, tableau3, tableau4, tableau5, tableau6, tableau7, tableau8]
- return foundation, tableau, cell
- def move_to_foundation(tableau, foundation, t_col, f_col):
- #Get rank and get suit graphs the rank and suit of the card and this method checks the rank and suit
- one = tableau[t_col][-1]
- two = foundation[f_col]
- '''
- Finds the rank and suit ^^^^^^^^^
- '''
- if foundation[f_col] == []:
- if one.get_rank() == 1:
- foundation[f_col].append(one) #adds the card
- del tableau[t_col][-1] #del function removes card from tableau
- elif one.get_suit() == two.get_suit():
- if one.get_rank()== two.get_rank()+1:
- foundation[f_col].append(one)
- del tableau[t_col][-1]
- '''
- parameters: a tableau, a foundation, column of tableau, column of foundation
- returns: Boolean (True if the move is valid, False otherwise)
- moves a card at the end of a column of tableau to a column of foundation
- This function can also be used to move a card from cell to foundation
- '''
- def move_to_cell(tableau, cell, t_col, c_col):
- one = tableau[t_col][-1]
- two = cell[c_col]
- if two == []:
- cell[c_col].append(one) #Appends tableau column card
- del tableau[t_col][-1] #del function removes card from tableau
- '''
- parameters: a tableau, a cell, column of tableau, column of cell
- returns: Boolean (True if the move is valid, False otherwise)
- moves a card at the end of a column of tableau to a cell
- '''
- def move_to_tableau(tableau, foundation, t_col, f_col, c_col):
- #s,c,d,h all refer to card colors and the code checks if colors are allowed
- one = tableau[t_col][-1]
- if cell[c_col] != []: #Does not equal empty slot for a card
- two = cell[c_col][0]
- if one == two+1:
- if "d" in one or "h" in one: #runs the red cards first
- if "s" in two or "c" in two:
- tableau[t_col].append(two)
- del cell[c_col][-1]
- elif "d" in two or "h" in two: #runs the red cards first
- if "s" in one or "c" in one:
- tableau[t_col].append(two)
- del cell[c_col][-1]
- '''
- parameters: a tableau, a cell, column of tableau, a cell
- returns: Boolean (True if the move is valid, False otherwise)
- moves a card in the cell to a column of tableau
- remember to check validity of move
- '''
- def is_winner(foundation):
- gametime = 0
- while gametime == 0:
- if foundation[0] != []:
- if foundation[1] != []:
- if foundation[2] != []:
- if foundation[3] != []:
- print("You won!")
- gametime = 1
- '''
- parameters: a foundation
- return: Boolean
- '''
- def move_in_tableau(tableau, t_col_source, t_col_dest):
- #s,c,d,h all refer to card colors and the code checks if colors are allowed
- one = tableau[t_col_source][-1]
- two = tableau[t_col_dest][-1]
- if one == two+1:
- if "d" in one or "h" in one: #runs the red cards first
- if "s" in two or "c" in two:
- tableau[t_col_dest].append(one)
- del cell[t_col_source][-1]
- elif "d" in two or "h" in two: #runs the red cards first
- if "s" in one or "c" in one:
- tableau[t_col_dest].append(one)
- del cell[t_col_source][-1]
- '''
- parameters: a tableau, the source tableau column and the destination tableau column
- returns: Boolean
- move card from one tableau column to another
- remember to check validity of move
- '''
- pass
- def print_game(foundation, tableau, cell):
- """
- parameters: a tableau, a foundation and a cell
- returns: Nothing
- prints the game, i.e, print all the info user can see.
- Includes:
- a) print tableau
- b) print foundation ( can print the top card only)
- c) print cells
- """
- print()
- print(" Cells: Foundation:")
- # print cell and foundation labels in one line
- for i in range(4):
- print('{:8d}'.format(i + 1), end='')
- print(' ', end='')
- for i in range(4):
- print('{:8d}'.format(i + 1), end='')
- print() # carriage return at the end of the line
- # print cell and foundation cards in one line; foundation is only top card
- for c in cell:
- # print if there is a card there; if not, exception prints spaces.
- try:
- print('{:8s}'.format(c[0].rank_and_suit()), end='')
- except IndexError:
- print('{:8s}'.format(''), end='')
- print(' ', end='')
- for stack in foundation:
- # print if there is a card there; if not, exception prints spaces.
- try:
- print('{:8s}'.format(stack[-1].rank_and_suit()), end='')
- except IndexError:
- print('{:8s}'.format(''), end='')
- print() # carriage return at the end of the line
- print('----------')
- print("Tableau")
- for i in range(len(tableau)): # print tableau headers
- print('{:8d}'.format(i + 1), end='')
- print() # carriage return at the end of the line
- # Find the length of the longest stack
- max_length = max([len(stack) for stack in tableau])
- # print tableau stacks row by row
- for i in range(max_length): # for each row
- print(' ' * 7, end='') # indent each row
- for stack in tableau:
- # print if there is a card there; if not, exception prints spaces.
- try:
- print('{:8s}'.format(stack[i].rank_and_suit()), end='')
- except IndexError:
- print('{:8s}'.format(''), end='')
- print() # carriage return at the end of the line
- print('----------')
- def print_rules():
- '''
- parameters: none
- returns: nothing
- prints the rules
- '''
- print("Rules of FreeCell")
- print("Goal")
- print("\tMove all the cards to the Foundations")
- print("Foundation")
- print("\tBuilt up by rank and by suit from Ace to King")
- print("Tableau")
- print("\tBuilt down by rank and by alternating color")
- print("\tThe bottom card of any column may be moved")
- print("\tAn empty spot may be filled with any card ")
- print("Cell")
- print("\tCan only contain 1 card")
- print("\tThe card may be moved")
- def show_help():
- '''
- parameters: none
- returns: nothing
- prints the supported commands
- '''
- print("Responses are: ")
- print("\t t2f #T #F - move from Tableau to Foundation")
- print("\t t2t #T1 #T2 - move card from one Tableau column to another")
- print("\t t2c #T #C - move from Tableau to Cell")
- print("\t c2t #C #T - move from Cell to Tableau")
- print("\t c2f #C #F - move from Cell to Foundation")
- print("\t 'h' for help")
- print("\t 'q' to quit")
- def play():
- '''
- Main program. Does error checking on the user input.
- '''
- print_rules()
- foundation, tableau, cell = setup()
- show_help()
- while True:
- print_game(foundation, tableau, cell)
- response = input("Command (type 'h' for help): ")
- response = response.strip()
- response_list = response.split()
- if len(response_list) > 0:
- r = response_list[0]
- f_col = response_list[2] #Defines f_col
- t_col = response_list[1] #Defines t_col
- c_col = response_list[2] #Defines c_col
- '''
- t_col and f_col here ^^^^^^^^^^^^^^^^
- '''
- if r == 't2f':
- move_to_foundation(tableau, foundation, t_col, f_col)
- elif r == 't2t':
- move_in_tableau(tableau, t_col_source, t_col_dest)
- elif r == 't2c':
- move_to_cell(tableau, cell, t_col, c_col)
- elif r == 'c2t':
- move_to_tableau(tableau, cell, t_col, c_col)
- elif r == 'c2f':
- move_to_foundation(tableau, foundation, t_col, f_col)
- elif r == 'q':
- break #Break quits the whole game
- elif r == 'h':
- show_help() #Calls for help method
- else:
- print('Unknown command:', r)
- else:
- print("Unknown Command:", response)
- print('Thanks for playing')
- play()
Add Comment
Please, Sign In to add comment