Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Construction and Layout: One standard 52-card deck is used. There are four open cells and four open foundations. The entire deck is dealt out left

Construction and Layout: One standard 52-card deck is used. There are four open cells and four open foundations. The entire deck is dealt out left to right into eight tableaus, four of which comprise seven cards and four of which comprise six. Your program should deal one card to each tableau from left to right and deal this way until all 52 cards are dealt. If you dont setup the tableaus this way, your project will fail all automatic test cases and you will get penalized. Building During Play: Tableaus are dealt randomly from a shuffled deck, but during play tableaus must be built down by the same suit. Foundations are built up by suit. Moves: Any cell card or top card of any tableau may be moved to build on a tableau, or moved to an empty cell, an empty tableau, or its suits foundation. Complete or partial tableaus may be moved to build on existing tableaus, or moved to empty cascades, by recursively placing and removing cards through intermediate locations. Victory: The game is won after all cards are moved in ascending rank by suit to their foundation piles. Detailed Program Specifications: You will develop a program that allows the user to play Bakers Game according to the rules outlined above. The program will use the instructor supplied cards.py module to model the cards and deck of cards. To help clarify the specifications, we provide sample output of a correctly implemented Bakers Game Python program below in the Sample Output section. We also provide a project10.py file that contains stubs of the required functions. The program will run as is, but will not do anything useful. You are encouraged to create additional functions as you see fit. Your program must use the import statement for cards.py; you are not allowed to copy the contents of cards.py into your proj10.py implementation. Also, you are not allowed to modify cards.py Program commands: The program will recognize the following commands regardless of case (i.e. TF, tf, Tf, and tF are all valid) - TC x y Move card from tableau x to cell y TF x y Move card from tableau x to foundation y TT x y Move card from tableau x to tableau y CF x y Move card from cell x to foundation y CT x y Move card from cell x to tableau y R Restart the game with a re-shuffle H Display this menu of commands Q Quit the game Here, x and y denote column numbers. Tableau columns are numbered from 1 to 8, whereas Cells and Foundation piles are numbered from 1 to 4. The program will repeatedly display the current state of the game and prompt the user to enter a command until the user wins the game or enters q, whichever comes first. The program will detect, report, and recover from invalid commands. None of the data structures representing the cells, tableaus, or foundations will be altered by an invalid command. Function Descriptions: We have already supplied stubs for functions that we require, but your implementation can include more functions if desired. 1. setup_game() -> (cells, foundations, tableaus) setup_game takes no parameters. It creates and initializes the cells, foundations, and tableaus, then returns them as a tuple, in that order. The cells and foundations will be a lists of 4 empty lists, the Tableau will be a list of 8 lists, which will contain all of the cards dealt into 8 vertical columns from left to right as described before. 2. display_game(cells, foundations, tableaus) -> None display_game takes four parameters, which should be the lists representing the cells, foundations, and tableaus. The cells and foundations should be displayed above the tableaus. A non-empty cell should be displayed as the card within it, whereas an empty cell should be displayed as [ ]. A non-empty foundation will be displayed as the top card in the pile (i.e. the last card moved to it), while an empty foundation will also be displayed as [ ]. The tableau is displayed below the cells and foundations, and each column of the tableau will be displayed downwards as shown in the sample below. An empty column will be displayed by whitespace. (Suggestions: (1) at first simply print each data structure, e.g. print(tableaus) in this function until you get the rest of the program working. Then come back and do the formatting last. (2) when you get to formatting, convert the card to a string using str(), e.g. print("{:4s}".format(str(tableau[2][0])) ) 3. valid_fnd_move(src_card, dest_card) -> None This function is used to decide whether a movement from a tableau or a cell to a foundation is valid or not. It takes two parameters: src_card and dest_card. The card that you are trying to place into the foundation is the src_card and the card already in the foundation that you are trying to place onto is the dest_card. The function will raise a RuntimeError exception if the move is not valid. Conditions for a valid foundation move: If there is no dest_card and the src_card is an Ace. If the src_card and dest_card have the same suit and the rank of the src_card is 1 greater than that of the dest_card. 4. valid_tab_move(src_card, dest_card) -> None This function is used to decide whether a movement from a cell, foundation, or another tableau to a tableau is valid or not: It takes two parameters, src_card and dest_card. The card that you are trying to place into the tableau is the src_card and the card already in the tableau that you are trying to place onto is the dest_card. The function will raise a RuntimeError exception if the move is not valid. Conditions for a valid tabeau move: If there is no dest_card then any src_card is valid. If the src_card and dest_card have the same suit and the rank of the src_card is 1 less than that of the dest_card. 5. tableau_to_cell(tab, cell) -> None This function will implement a movement of a card from a tableau to a cell. It takes two parameters, both are lists. The first one is the source tableau column (i.e. from which you are moving a card) and the second parameter is the destination cell. This function has no return value. This function moves a single card and has to decide if a movement is valid or not. If there is an invalid move, it will raise a RuntimeError exception. When a user enters tc x y, this function will be used. 6. tableau_to_foundation(tab, fnd) -> None This function will implement a movement of a card from a tableau to a foundation. It takes two parameters, both are lists. The first one is the source tableau column (i.e. from which you are moving a card) and the second parameter is the destination foundation pile. This function has no return value. This function moves a single card and calls valid_fnd_move() to decide if a movement is valid or not. If there is an invalid move, it will raise a RuntimeError exception. When a user enters tf x y, this function will be used. 7. tableau_to_tableau(tab1, tab2) -> None This function will implement a movement of a card from one tableau column to another tableau column. It takes two lists as parameters. The first one is the tableau column from which you are moving a card and the second is the destination tableau column that you are moving a card to. This function has no return value. This function calls valid_tab_move() to decide if such a movement is valid or not. If there is an invalid move, it will raise a RuntimeError exception. When a user enters tt x y, this function will be used. 8. cell_to_foundation(cell, fnd) -> None This function will implement a movement of a card from a cell to a foundation. It takes two parameters, both are lists. The first one is the source cell (i.e. from which you are moving a card) and the second parameter is the destination foundation pile. This function has no return value. This function moves a single card and calls valid_fnd_move() to decide if a movement is valid or not. If there is an invalid move, it will raise a RuntimeError exception. When a user enters cf x y, this function will be used. 9. cell_to_tableau(cell, tab) -> None This function will implement a movement of a card from a cell to a tableau column. It takes two lists as parameters. The first one is the cell from which you are moving a card and the second is the destination tableau column that you are moving a card to. This function has no return value. This function calls valid_tab_move() to decide if such a movement is valid or not. If there is an invalid move, it will raise a RuntimeError exception. When a user enters ct x y, this function will be used. 10. is_winner(foundation) -> True/False This function will be used to decide if a game was won. A game has been won if all the foundation piles have 13 cards and the top card on each of the pile is a King. This function returns either True or False. Some Mandatory Stuff: 1. The display_game() function output does not have to look exactly like the example, but it must generate legible output (it should look like you are playing the game). 2. You must use the try-except block to handle errors (the only try-except block needed is included in the sample), each error message must explain exactly what type of errors a user is making. There are many errors to consider and we will not list them for you. You should be able to think of them when analyzing gameplay. Please see the example.txt example for some example of errors. 3. You should use the RuntimeErrors custom message option to throw and handle errors for all errors. The error message is placed in quotes and when the exception is caught in the main your message in quotes is passed as error_message in the except block and is then printed. Example:

if everything looks fine:

do whatever necessary

else:

raise RuntimeError(Error: invalid command because of )

proj10.py:

import cards #This is necessary for the project

MENU = """ Game commands: TC x y Move card from tableau x to cell y TF x y Move card from tableau x to foundation y TT x y Move card from tableau x to tableau y CF x y Move card from cell x to foundation y CT x y Move card from cell x to tableau y R Restart the game with a re-shuffle H Display this menu of commands Q Quit the game """ def valid_fnd_move(src_card, dest_card): """ Add your function header here. """ pass #Replace this pass statement with your own code def valid_tab_move(src_card, dest_card): """ Add your function header here. """ pass #Replace this pass statement with your own code def tableau_to_cell(tab, cell): """ Add your function header here. """ pass #Replace this pass statement with your own code def tableau_to_foundation(tab, fnd): """ Add your function header here. """ pass #Replace this pass statement with your own code def tableau_to_tableau(tab1, tab2): """ Add your function header here. """ pass #Replace this pass statement with your own code def cell_to_foundation(cell, fnd): """ Add your function header here. """ pass #Replace this pass statement with your own code def cell_to_tableau(cell, tab): """ Add your function header here. """ pass #Replace this pass statement with your own code def is_winner(foundations): """ Add your function header here. """ pass #Replace this pass statement with your own code def setup_game(): """ The game setup function. It has 4 cells, 4 foundations, and 8 tableaus. All of these are currently empty. This function populates the tableaus from a standard card deck. Tableaus: All cards are dealt out from left to right (meaning from tableau 1 to 8). Thus we will end up with 7 cards in tableaus 1 through 4, and 6 cards in tableaus 5 through 8 (52/8 = 6 with remainder 4). This function will return a tuple: (cells, foundations, tableaus) """ #You must use this deck for the entire game. #We are using our cards.py file, so use the Deck class from it. stock = cards.Deck() #The game piles are here, you must use these. cells = [[], [], [], []] #list of 4 lists foundations = [[], [], [], []] #list of 4 lists tableaus = [[], [], [], [], [], [], [], []] #list of 8 lists """ YOUR SETUP CODE GOES HERE """ pass #Replace this pass statement with your own code return cells, foundations, tableaus def display_game(cells, foundations, tableaus): """ Add your function header here. """ #Labels for cells and foundations print(" =======Cells======== ====Foundations=====") print(" --1----2----3----4-- --1----2----3----4--") print(" ", end="") # to print a card using formatting, convert it to string: # print("{}".format(str(card))) print() #Labels for tableaus print(" =================Tableaus=================") print(" ---1----2----3----4----5----6----7----8---") #HERE IS THE MAIN BODY OF OUR CODE print(RULES) cells, fnds, tabs = setup_game() display_game(cells, fnds, tabs) print(MENU) command = input("prompt :> ").strip().lower() while command != 'q': try: pass #Replace this pass statement with your own code #Any RuntimeError you raise lands here except RuntimeError as error_message: print("{:s} Try again.".format(str(error_message))) display_game(cells, fnds, tabs) command = input("prompt :> ").strip().lower() 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Theory Icdt 99 7th International Conference Jerusalem Israel January 10 12 1999 Proceedings Lncs 1540

Authors: Catriel Beeri ,Peter Buneman

1st Edition

3540654526, 978-3540654520

More Books

Students also viewed these Databases questions