Question: Do this in python. You will be creating the memory game. You probably played this as a small child. There are a number of cards

Do this in python. You will be creating the memory game. You probably played this as a small child. There are a number of cards face-down, and each player flips two over on each turn. If there is a match, they get a point and go again. If they do not get a match, the cards are flipped back over and the next player goes.

Starter code has been provided. The test.py code will enable you to build and test your GameBoard and MemoryCard classes without having to worry about the actual game play. Once those are working, you can move to the memory_starter.py file. Rename this file task1.py. This file provides a close implementation of the game play, but has bugs in it. You will need to identify and fix the bugs. You will also document what the bugs were in a file called memory_bugs.txt. You will only create Software Designs for the GameBoard and MemoryCard classes. Include UML and designs for each public method, including __init__(). Save these in a file called plan1.txt.

Requirements:

Create a class called MemoryCard in a file called memorycard.py. Place the file in a folder called modules.

The constructor will have a parameter that is an id number. Each card will have a unique id number

Define the following public methods:

getValue() - returns a value associated with a card. Note, this is not the same as the id as matching cards will have the same value

toggleFlipped() - change the card from a face-up to a face-down, and vice versa, state

isFlipped() - returns true if the card is currently face-up

displayCard() - returns a string. If the card is face-up it returns a symbol associated with the value. If it is face-down, it returns a space. If you don't return a space then your game board printout will be kinda messed up.

Symbols are ascii characters starting with "!". Check out https://www.asciitable.com/ (Links to an external site.) for a refresher. Don't forget functions we've learned to encode/decode ascii values.

You may include any private data and methods that you see fit.

Hint: I would build this first, then test it with the code in test.py.

Create a class called GameBoard in a file called gameboard.py. Place the file in a folder called modules.

The constructor will have two parameters indicating the number of columns and number of rows in the game board.

Define the following public methods:

getBoard() - returns a single string representation of the game board. There is an example below, and it can be seen in the demo video. Note that the GameBoard class does not call print(), it just returns a string that can be printed.

flipCard(xPos, yPos) - takes an x, y coordinate from the game board and flips the card at that location.

isCardFlipped(xPos, yPos) - returns true if the card at that position is face-up, otherwise returns false

isMatch(pos1, pos2) - pos1 and pos2 are the x, y locations of two different cards given as a list. For example [5, 3] could be the actual value associated with pos1.

The game board should place the cards in a random order. We have done this multiple times in class.

You may include any private data and methods that you see fit.

Hint: Make sure your class works for different combinations of column and row counts (the board doesn't have to be a square).

Bugs in memory_starter.pyYou must identify and fix bugs you find in the file. Save these in a file called memory_bugs.txt. It should look something like this. Note that these may or may not be actual bugs in the program.

Memory Bugs 1. No prompt for the number of columns 2. Score for players is not updating properly 3. And so on...

The number of bugs is unknown (to you).

A bug is anything that makes the gameplay experience not match expectations.

Expectations are based on the following Requirements Specification

The game starts with a welcome message

The user is prompted to enter the number of columns on the game board

The user is prompted to enter the number of rows on the game board

The user is prompted to enter the number of players, which will be named Player 1, Player 2, etc.

Game loop

Prior to each prompting during game play, the score for each player is printed followed by the game board (see below)

The current player is prompted to input an x, y coordinate

If the user ever enters the coordinates of a card that is face-up already, they are notified of the error and need to try again

If the spot is valid the card is flipped face-up.

Process repeats for a second card

If a match is detected the player scores a point and goes again

If a match is not detected the two cards are flipped face-down and the next player goes

This continues until all possible matches are made

After all matches are made, one of two possible messages are displayed

If it is a tie, then it is so stated along with an indication of the players who tied

If there is a single winner then it is so stated along with a message of who the winner is

You do not need to complete a Software Development plan for this file

Note:

Example game board. Notice the spacing for each cell. Two cards are currently face-up

Player 1: 0 Player 2: 0 Player 3: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | ----------------------------------- 1 | | | ? | | | | | | ----------------------------------- 2 | | | | | | | | | ----------------------------------- 3 | | | | | | 6 | | | ----------------------------------- 4 | | | | | | | | | ----------------------------------- 5 | | | | | | | | | ----------------------------------- 6 | | | | | | | | | ----------------------------------- 7 | | | | | | | | | ----------------------------------- 8 | | | | | | | | | -----------------------------------

Starter Code:

memory_starter.py

from modules.memoryboard import MemoryBoard # This import gives some funcitonality if your run the program from the commandline. See the video in the assignment # for instructions. Running in the terminal makes the game board play more like a game instead of scrolling and # printing a new board every time. # You'll need to comment a line down below in the printGameBoard() function if using PyCharm import os def main(): playerTurn = 0 # Keep track of whose turn it is print("Welcome to Memory ") # Get the Board Size cols = int(input("Enter the number of columns on the board: ")) rows = int(input("Enter the number of rows on the board: ")) # Get the number of players playerCount = int(input("Enter the number of players: ")) scores = [0] * playerCount # Create the MemoryBoard object memoryBoard = MemoryBoard(rows, cols) # Game loop winner = False while not winner: selectedCards = [] # Track the cards the current player selected printGameBoard(scores, memoryBoard.getBoard()) # Each turn is two card flips for i in range(2): xPos, yPos = eval(input("Player " + str(playerTurn + 1) + " choose a card to flip: ")) memoryBoard.flipCard(xPos, yPos) printGameBoard(scores, memoryBoard.getBoard()) # Player goes again if they get a match if memoryBoard.isMatch(selectedCards[0], selectedCards[1]): print("You got a match!") scores[playerTurn + 1] += 1 winner = sum(scores) == cols * rows / 2 # If the game is over show a different message if winner: print("Hit Enter to Continue") else: input("Player " + str(playerTurn) + " hit Enter to go again.") else: memoryBoard.flipCard(selectedCards[0][0], selectedCards[1][0]) memoryBoard.flipCard(selectedCards[0][1], selectedCards[1][1]) print("No Match") playerTurn += 1 input("Hit Enter for Player " + str(playerTurn + 1))

# Find the winning score value highScore = max(scores) # Check for a tie/winner if scores.count(highScore) > 1: print("The following players tied: ", end="") for i in range(len(scores)): if scores[i] == highScore: print(i, end=" ") else: print("The winner is player " + str(scores.index(highScore) + 1)) # Print the scoreboard and gameboard def printGameBoard(scores, board): os.system('cls||clear') # Comment this out if running in PyCharm for i in range(len(scores)): print("Player " + str(i + 1) + ": " + str(scores[i])) print() print(board) main()

memory_test.py

# Use this test code to make sure your memoryboard.py and memorycard.py files work like they should from modules.memoryboard import MemoryBoard from modules.memorycard import MemoryCard def main(): print("Testing the Card") testCard() print(" Testing the Board") testBoard() def testCard(): input("Hit Enter to create a card with an ID of 10") card1 = MemoryCard(10) input("Now hit Enter to display the card") print(card1.displayCard()) input("Nothing displayed. Now hit Enter to flip the card") card1.toggleFlipped() input("Now hit Enter to display the card") print(card1.displayCard()) print("The first card's symbol is a &") input("Hit Enter to create, flip, and display a card with an ID of 50") card2 = MemoryCard(50) card2.toggleFlipped() print(card2.displayCard()) print("The second card's symbol is a :") input("Hit Enter to create, flip, and display a card with an ID of 51") card2 = MemoryCard(51) card2.toggleFlipped() print(card2.displayCard()) print("Card 2 and 3 match event though they have different ID values") input("Hit Enter to test the game board") def testBoard(): COLS = 8 ROWS = 8 memoryBoard = MemoryBoard(COLS, ROWS) print(memoryBoard.getBoard()) input("Hit Enter to flip card at 7, 5") memoryBoard.flipCard(7, 5) print(memoryBoard.getBoard()) input("Hit Enter to flip back card at 7, 5") memoryBoard.flipCard(7, 5)

print(memoryBoard.getBoard()) input("Hit Enter to flip card at 3, 3") memoryBoard.flipCard(3, 3) print(memoryBoard.getBoard()) input("Hit Enter to try to flip a card that is already flipped when we don't want that to happen") if memoryBoard.isCardFlipped(3, 3): input("That card has been flipped already. Hit Enter to continue.") else: input("This better not have happened. If it did, hit Enter.") memoryBoard.flipCard(3, 3) print(memoryBoard.getBoard()) input("Hit Enter to flip card at 4, 3") memoryBoard.flipCard(4, 3) print(memoryBoard.getBoard()) input("Hit Enter to see if the two cards match") if memoryBoard.isMatch([3, 3], [4, 3]): print("They Match!") else: print("They Do Not Match!") print("Remove the shuffling in memoryboard.py to get a match") main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!