The deliverable for this assignment is the following file:proj10.py– the source code for your Python program Be sure to use the specified file name and to submit it for grading via theMimir...

1 answer below »

The deliverable for this assignment is the following file:proj10.py– the source code for your Python program


Be sure to use the specified file name and to submit it for grading via theMimir systembefore the project deadline. Donotsubmit thecards.pyfile. Do not copy the content of thecards.pyinto your file. You have to import the module instead.


Assignment Background


Seahaven is a solitaire card game which is played by one person with a standard 52-card deck of cards. The rules and a tutorial video are available at:


http://worldofsolitaire.com/


Click on “Choose Game” at the top of the screen and click on “Seahaven”.


Your program will allow the user to play a simplified version of Seahaven, with the program managing the game. The game rules are given below, but you should play the game online to understand it before you try to code it.


Game Rules


1. Start: The game is played with one standard deck of 52 cards. The deck is shuffled and becomes the initialstock(pile of cards).


Fifty cards are then dealt from the stock, left to right, one to each column of a ten-columntableau, continuing dealing an additional card to each column, until each column has five cards. The remaining two cards are dealt to the middle two of thecells, again left to right. The cells are above the tableau and centered. Each cell can contain at most one card. The order of the initial deal is important to match tests. All cards are visible, a.k.a. face up.


The game also has afoundation, which has four piles of cards with cards face up. The foundation starts out empty and is filled during play. Each foundation pile is in order and


contains only one suit starting with the ace at the bottom and ending with the king on top. Only the top card of each foundation pile is visible. The foundation is in the same row as the cells with two foundation piles to the left of the cells and two foundation piles to the right of the cells. Note the initial layout on the worldofsolitaire.com site.


2. Goal: The game is won when the foundation is full.


3. Moves: A player can move one card at a time (Note that the actual game, e.g. on worldofsolitaire, allows sequences of cards to be moved, but to simplify this project we are only allowing one card at a time to be moved.) A card can be moved to one of three places:




  • To the tableau:
    oA card can be moved to a card at the bottom of a tableau column, if they have the


    same suit and the destination card has a rank one higher than the source card. If a


    tableau column is empty, only a king can be placed there.
    oA card can be moved within the tableau, i.e. from one tableau column to another,


    or moved from a cell to the tableau. (Cards do not move from the foundation to


    the tableau.)




  • To a cell:


    oAny tableau card can be moved to any empty cell. (Cards do not move from the foundation to the cells.)


    oA cell can contain at most one card.




  • To the foundation:


    oA card can be moved to a foundation, if the destination (top) foundation card is the same suit and has a rank one lower than the source card. An ace can only move to an empty foundation pile.


    oA card can be moved from the tableau or from the cells. No other moves are permitted.


    Assignment Specifications


    You will develop a program that allows the user to play Seahaven according to the rules given above. The program will use the instructor-suppliedcards.pymodule to model the cards and deck of cards. To help clarify the specifications, we provide a sample interaction with a program satisfying the specifications at the end of this document.


    1. The program will recognize the following commands (upper or lower case):




MTT s d: Move card from end of Tableau column s to end of column d.MTC s d: Move card from end of Tableau column s to Cells d.
MCT s d: Move card from Cells s to end of Tableau column d.
MTF s d: Move card from end of Tableau column s to Foundation d.MCF s d: Move card from end of Cell s to Foundation d.


R: Restart the game (after shuffling)H: Display this menu of choices
Q: Quit the game


wheresandddenote column numbers (standing forsource anddestination), and the columns are numbered from 1 to 10. All indices in the functions starts from 0. When the user enters indices, you have to subtract 1 before calling the functions.


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 stock, tableau, or foundation will be altered by an invalid command.


2.initialize()(tableau, foundation, cells)
The program will use this function to initialize a game.
The function has no parameters. It creates and initializes the tableau, foundation and cells, and then returns them as a tuple, in that order. This corresponds to rule 1 in the game rules:




  • foundationis an empty list of four lists, i.e. [[],[],[],[]]




  • tableauisalistoftenlists,eachcontainingfiveofthedealtcards.Thefirstelementof


    tableauis the leftmost column when displayed.




  • cellsisalistoffouritemsthatwillbeNoneoracard. UseNoneasaplaceholderfor


    an empty cell.




  • stockisashuffleddeck(rememberthecards.pyfile)


    The deck is shuffled and becomes the initialstock(pile of cards). All fifty-two cards are then dealt from the stock as specified above into thetableau, with the final two cards dealt to thecells. The order of dealing is important to match tests.


    3.(Provided)display( tableau, foundation, cells )NoneThe program will use this function to display the current state of the game:


    4.(Provided)get_option()list
    The program will use this function to prompt the user to enter an option and return a representation of the option designed to facilitate subsequent processing.
    The function takes no parameters. It prompts the user for an option and checks that the input supplied by the user is of the form requested in the menu. Valid inputs for options are described in item (bullet) 1 of the specifications. If the input is not of the required form, the function prints an error message and returnsNone.
    Note that input with the incorrect number of arguments, e.g. M 2, or incorrect types, e.g. F D, will returnNone.


    The function returns a list as follows:






  • - None, if the input is not of the required form




  • - ['Mxx', s, d], wheresanddareint’s, for moving a card from a source tableau


    column, cell or foundation to a destination tableau, cell or foundation. Where Mxx is in['MTT','MTC','MCT','MTF','MCF']






  • - ['R'], for restart




  • - ['H'], for displaying the menu




  • - ['Q'], for quit


    5. The program will use the following functions to determine if a requested move is valid:


    validate_move_within_tableau(tableau,src_col,dst_col)bool validate_move_cell_to_tableau(tableau,cells,cell_no,dst_col)bool validate_move_tableau_to_cell(tableau,cells,src_col,cell_no)bool validate_move_tableau_to_foundation(tableau,foundation,src_col,dst_col)bool validate_move_cell_to_foundation(tableau,foundation,cell_no,found_no)bool


    These similar functions have three or four parameters: the data structure representing the tableau, foundation, cells column, or twointsindicating the source column, and theintfor the column where the source card should be moved. The rules are stated above in the Game




Rules. The function will returnTrue,if the move is valid; andFalse, otherwise.


Some things to consider:




  • An empty tableau column can only have a king moved to it.




  • An empty foundation can only have an ace moved to it.




  • A cell can have at most one card




  • A card can only be moved to a card of the same suit and whose rank is one greater.




  • Remember to ensure that a source card exists.




  • Remember to ensure that indices are valid.
    When checking that the source card exists at the specified column and row, e.g. the index may be


    out of range;try-exceptworks well for that.
    6. The program will use the following functions to move a card between data structures


    (between tableau, foundation, and cell):


    move_within_tableau(tableau,src_col,dst_col)bool move_tableau_to_cell(tableau,cells,src_col,cell_no)bool move_cell_to_tableau(tableau,cells,cell_no,src_col)bool move_cell_to_foundation(cells,foundation,cell_no,found_no)bool move_tableau_to_foundation(tableau,foundation,src_col,found_no)bool


    These functions have three or four parameters: the data structure representing the tableau, cells or foundation, the source, and the destination. If the move is valid (determined by calling the correspondingvalidatefunction), the function will update the data structure and returnTrue; otherwise, it will do nothing to it and returnFalse. Because a lot of work is done in the corresponding validate function, these functions are easier to implement.




7.check_for_win( foundation )Bool
The program will use this function to check if the game has been won. The function checks to see if the foundation is full. It returnsTrue, if the foundation is full andFalse, otherwise. (Hint: you can make your life easier by assuming that your move (and validate) functions were correct.)


8. Once you write all your functions, it is time to write yourmainfunction:




  1. a) Your program should start by initializing the board (the cells, the tableau and the


    foundation).




  2. b) Display the menu and the board.




  3. c) Ask to input an option and check the validity of the input.




  4. d) If‘Mxx s d’, move a card


    If a move is successful, check to see if the user won; if so, display the winning board and restart the game (initialize the board, display the new board, and display the menu (see note below). If a move is not successful, print an error message that includes the 3 items of the list returned fromget_option()function (i.e.,‘Mxx s d’). Check to see if the user_won the game if the move was valid. If it is, start a new game like below.




  5. e) If‘R’,restart the game byinitializing the board. Display the board.




  6. f) If‘H’, display the menu of choices




  7. g) If‘Q’, quit the game




  8. h) If none of these options, the program should display an error message. (the error message


    is already included in theget_option()function).




  9. i) The program should repeat until the user quits the game. Display a goodbye message.




If the user won,


print("You won!")
# display the winning game print("\n- - - - New Game. - - - -") # restart the game
# display the new game
# display the menu

Answered 2 days AfterApr 10, 2021

Answer To: The deliverable for this assignment is the following file:proj10.py– the source code for your Python...

Swapnil answered on Apr 12 2021
131 Votes
import cards, random
random.seed(100)
MENU =
def initialize():
tableau = [[], [], [], [], [], [], [], [], [], []]
foundation = [[], [], [], []]
cells = [None, None, None, None]
deck = cards.Deck()
deck.shuffle()
for i in range(50):
tablea
u_column_index = i % 10
tableau[tableau_column_index].append(deck.deal())
cells[1] = deck.deal()
cells[2] = deck.deal()
return tableau, foundation, cells
def display(tableau, foundation, cells):
print("\n{:<11s}{:^16s}{:>10s}".format("foundation", "cell", "foundation"))
print("{:>14s}{:>4s}{:>4s}{:>4s}".format("1", "2", "3", "4"))
for i, f in enumerate(foundation):
if f and (i == 0 or i == 1):
print(f[-1],
end=' ')
elif i == 0 or i == 1:
print("{:4s}".format(" "),
end='')
print("{:3s}".format(' '), end='')
for c in cells:
if c:
print(c, end=' ')
else:
print("[ ]",
end='')
print("{:3s}".format(' '), end='')
for i, f in enumerate(foundation):
if f and (i == 2 or i == 3):
print(f[-1],
end=' ')
elif i == 2 or i == 3:
print("{}{}".format(" ", " "),
end='')
print()
print("\ntableau")
print(" ", end=' ')
for i in range(1, 11):
print("{:>2d} ".format(i), end=' ')
print()
max_col = max([len(i) for i in tableau])
for row in range(max_col):
print("{:>2d}".format(row + 1), end=' ')
for col in range(10):
if row < len(tableau[col]):
print(tableau[col][row], end=' ')
else:
print(" ", end=' ')
print()
print()
def validate_move_within_tableau(tableau, src_col, dst_col):
if not src_col < 10 and not dst_col < 10:
return False
try:
src_col_list = tableau[src_col]
dst_col_list = tableau[dst_col]
src_card = src_col_list[-1]
if len(dst_col_list) == 0 and src_card.rank() == 13:
return True
dst_card = dst_col_list[-1]
if src_card.suit() == dst_card.suit() and dst_card.rank() == \
src_card.rank() + 1:
return True
except IndexError:
return False
return False
def validate_move_cell_to_tableau(tableau, cells, cell_no, dst_col):
if not cell_no < 4 or not dst_col < 10:
return False
try:
src_card = cells[cell_no]
if not src_card:
return False
dst_col_list = tableau[dst_col]
if len(dst_col_list) == 0 and src_card.rank() == 13:
return True
dst_card = dst_col_list[-1]
if...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here