Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CSE 240 ASSIGNMENT5 Instructions: In this assignment you will code a simple card-game from scratch. The card-game is traditionally called Memory or Concentration You are

CSE 240 ASSIGNMENT5

Instructions:

In this assignment you will code a simple card-game from scratch. The card-game is traditionally called Memory or Concentration

You are to create a C/C++ file named __hw5.c (or .cpp)

Specification:

You will create a version of Memory that will use a single, standard deck of cards: 52 Cards: 2-10, J, K, Q, A; 4 suits: Hearts, Diamonds, Spades, Clubs.

The cards will be represented in a struct with two members representing the card value and suit.

You should use an enum for the suits ... you might consider using it for the values as well.

Part 1 The setup:

You will populate a compile time array with your cards.

From that deck you will create the play-space. The play-space will be a 2D array that will hold cards. This 2D array will always be 4 by ?

You will prompt the player for how many columns they want to play with (minimum 3, maximum 13). So your Dynamic 2D array will always be somewhere between 4x3 and 4x13.

Create a dynamic single dimensional array of cards to work as a shuffler and dealer.

1 - Overall flow of logic for setting up the game

Create your struct

Fill an array of size 52

Don't shuffle these, create them in order

Prompt the user for how many columns they

want to play with

Create a "dealer" array of size 4 * columns

Fill the "dealer" array with cards taken

from the Deck

Required Functions for part 1:

buildDeck there are two options, you can either make this void and pass the array in, or have it return a pointer and generate the deck from within.

shuffleDeck(cardStruct* dealerDeck, int size) this should shuffle the dealer array

cardStruct draw(cardStruct* deck) removes the first item in the array and returns it. This should also clean up the array. You might consider passing in the size here and maybe pass it by reference so you can change the size.

You should create any other functions that you need. Remember a function should do one thing well.

Part 2 The game

Once you have built the play area, then its just a matter of playing the game. Show the player a grid of blank cards with coordinates on the outer edge:

Example (using a 4 x 10):

### 0 1 2 3 4 5 6 7 8 9

0 ** ** ** ** ** ** ** ** ** **

1 ** ** ** ** ** ** ** ** ** **

2 ** ** ** ** ** ** ** ** ** **

3 ** ** ** ** ** ** ** ** ** **

Prompt the user for two cards to choose using those coordinates:

Choose card 1: 0 0 Choose card 2: 1 1

Make sure you check the inputs!

Dont allow: duplicate card choices, cards that have already been revealed, choices that are out of bounds.

If you are using C++ also make sure you use cin.fail() to check for bad inputs.

After two cards have been chosen, check to see if they match. Reveal the two card choices to the user:

### 0 1 2 3 4 5 6 7 8 9

0 2? ** ** ** ** ** ** ** ** **

1 ** 2? ** ** ** ** ** ** ** **

2 ** ** ** ** ** ** ** ** ** **

3 ** ** ** ** ** ** ** ** ** **

Press any key to continue ...

If they match, then congratulate the player and leave the cards visible in the grid and continue the game.

### 0 1 2 3 4 5 6 7 8 9

0 2? ** ** ** ** ** ** ** ** **

1 ** 2? ** ** ** ** ** ** ** **

2 ** ** ** ** ** ** ** ** ** **

3 ** ** ** ** ** ** ** ** ** **

Choose card 1:

Consider how you would determine if the card is revealed... there are a few options... one option would be a parallel array

If they dont match:

### 0 1 2 3 4 5 6 7 8 9

0 2? ** ** ** ** ** ** ** ** **

1 ** 6? ** ** ** ** ** ** ** **

2 ** ** ** ** ** ** ** ** ** **

3 ** ** ** ** ** ** ** ** ** **

Press any key to continue ...

After the user Presses a key to continue you should clear the screen (output a bunch of newlines to scroll the console window).

After clearing the screen, show the puzzle without the two cards revealed and prompt for new choices.

### 0 1 2 3 4 5 6 7 8 9

0 ** ** ** ** ** ** ** ** ** **

1 ** ** ** ** ** ** ** ** ** **

2 ** ** ** ** ** ** ** ** ** **

3 ** ** ** ** ** ** ** ** ** **

Choose card 1:

Note: if they player had previous matches they should remain visible!!

Play should continue until the player wants to quit or the game is won (all cards matched). The player can quit by entering negative numbers for their first card selection.

Required Functions for Part 2:

bool cardMatch(cardStruct card1, cardStruct card2) this function should return true or false that two cards match. If youre using C this can return a 1 or 0 instead

void printGrid(cardStruct** playArea, int rows, int cols) output the grid as shown above

void flipCard([whatever is needed], int x, int y) however you choose to make a card visible or invisible in the printGrid function should be done here. There are a few ways to do this... depending on what you choose that will change the parameters for this function.

bool checkForWin([whatever is needed]) this function should return true or false (or 1 or 0 in C) if the game is won!

Of course, once again, you should create as many functions as needed. I once again encourage you to modularize your code!

Extra Credit Opportunity +3: Create an advanced mode option that the player can opt into at the beginning of the game. The advanced mode makes matches on matching number and color. Part of this would be to create a bool cardMatchAdvanced(...) that can be called instead. Yes these should be two separate functions.

Show game area

Check for a

match

Prompt for

card choice 1

(Take in an x

& y)

If they do

match

If they don't

match

Prompt for

card choice 2

Leave both

cards flipped

in the game

area

Hide the cards

again

Flip both

cards

Show the game

area with both

cards flipped

Back to the

beginning

of gameplay

Sample Output

Welcome to CSE240 Memory!

The rules are simple, pick two cards and see if they match. Continue

until youve matched all of the cards!

You may enter -1 for your cards at any time during the game to quit.

How many columns of cards do you want to play with?

Minimum of 3, maximum of 13: 10

### 0 1 2 3 4 5 6 7 8 9

0 ** ** ** ** ** ** ** ** ** **

1 ** ** ** ** ** ** ** ** ** **

2 ** ** ** ** ** ** ** ** ** **

3 ** ** ** ** ** ** ** ** ** **

Choose card 1: 0 0 Choose card 2: 1 1

### 0 1 2 3 4 5 6 7 8 9

0 2? ** ** ** ** ** ** ** ** **

1 ** 6? ** ** ** ** ** ** ** **

2 ** ** ** ** ** ** ** ** ** **

3 ** ** ** ** ** ** ** ** ** **

Too bad! Not a match!

Press any key to continue...

[clear the screen here]

### 0 1 2 3 4 5 6 7 8 9

0 ** ** ** ** ** ** ** ** ** **

1 ** ** ** ** ** ** ** ** ** **

2 ** ** ** ** ** ** ** ** ** **

3 ** ** ** ** ** ** ** ** ** **

Choose card 1: 0 0 Choose card 2: 3 2

### 0 1 2 3 4 5 6 7 8 9

0 2? ** ** ** ** ** ** ** ** **

1 ** ** ** ** ** ** ** ** ** **

2 ** ** ** 2? ** ** ** ** ** **

3 ** ** ** ** ** ** ** ** ** **

Congratulations! Its a match!

Press any key to continue ...

### 0 1 2 3 4 5 6 7 8 9

0 2? ** ** ** ** ** ** ** ** **

1 ** ** ** ** ** ** ** ** ** **

2 ** ** ** 2? ** ** ** ** ** **

3 ** ** ** ** ** ** ** ** ** **

Choose card 1: 0 0 Choose card 2: 3 2

Im sorry those cards have already been chosen!

Press any key to continue ...

### 0 1 2 3 4 5 6 7 8 9

0 2? ** ** ** ** ** ** ** ** **

1 ** ** ** ** ** ** ** ** ** **

2 ** ** ** 2? ** ** ** ** ** **

3 ** ** ** ** ** ** ** ** ** **

Choose card 1: 0 10 Choose card 2: 2 2

Im sorry, you entered an invalid card. Please try again!

Press any key to continue...

### 0 1 2 3 4 5 6 7 8 9

0 2? ** ** ** 5? ** ** ** ** **

1 ** ** ** ** ** ** ** ** ** **

2 ** ** 4? 2? ** ** ** ** ** **

3 ** ** ** ** ** ** ** ** ** **

Choose card 1: 0 4 Choose card 2: 2 2

Too bad! Not a match!

Press any key to continue...

[clear the screen here]

### 0 1 2 3 4 5 6 7 8 9

0 2? ** ** ** ** ** ** ** ** **

1 ** ** ** ** ** ** ** ** ** **

2 ** ** ** 2? ** ** ** ** ** **

3 ** ** ** ** ** ** ** ** ** **

Choose card 1: -1 -1 Choose card 2: -1 -1

Goodbye!

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_2

Step: 3

blur-text-image_3

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 Modeling And Design

Authors: Toby J. Teorey, Sam S. Lightstone, Tom Nadeau, H.V. Jagadish

5th Edition

0123820200, 978-0123820204

More Books

Students also viewed these Databases questions

Question

On the same day, Kyoto Fund converted $500,000 into JPY at

Answered: 1 week ago