Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 1. Matching a number card Several cards are flipped over so that the face with numbers is not visible. You want to create a

Problem 1. Matching a number card

Several cards are flipped over so that the face with numbers is not visible. You want to create a game that picks out two cards with the same number. m rows and n columns, the total number of cards is an even number, the number cards with one or more natural numbers are placed face upside down, and each card has two cards with the same number.image text in transcribed

The above picture shows the case where two of the cards listed in the 3 rows and 4 columns (total 12 cards) are found. Unlike the blurred picture, the numbers actually written on the card are not visible when turned upside down.

The game procedure is as follows:

1. Arrange a total of m * n cards, each having two cards of the same number, in m rows and n columns. However, let the numbered face go to the bottom so that you cannot see it.

2. The player chooses two cards.

3. Select two cards

A. If you have the same number, find the correct card and keep the numbers visible.

B. If there are other numbers, turn the two cards upside down so that the numbers are not visible.

4. Repeat steps 2 and 3 until all cards are found.

The success of the game depends on whether the player keeps track of the number of cards he or she sees when flipping the card, and whether a correct pair of cards can be picked up in close proximity.

I want to implement this game in C language. In this assignment, we will proceed to creating a number card and implementing a "memory" space for the "computer" player. The actual game progression will be covered in the next assignment. Implementation is free, but dynamic memory allocation should be used.

(a) Design a "Figure" structure to represent a single number card

Each piece of cards you use in a game requires the following information:

The row and column number on which the card is placed (row and column numbers are assumed to start from 0 for convenience)

0 ? row number ? row number - 1,

0 ? column number ? number of columns 1

Number on card: 1 ? number ? (number of cards / 2)

Whether the number of cards is reversed; 1 if the number is visible, 0 if not

Define a figure structure to hold this information in func1.h file and write the following function to access this struct and return the specified information.

void print_fig_info (struct Figure * fig);

The above function takes a pointer to the figure structure and prints the variable value of the structure pointed to by the pointer to the screen using the printf () function. The output format is as follows.

(, ) = ()

e.g. Row, and column numbers are 1 and 3, the number is 7, and the output of the card,

(1, 3) = 7 (0)

(b) Creation and arbitrary placement of game cards

Now, when the game board size is given in m rows and n columns, try writing a function that generates as many cards as you need and arbitrarily generates numbers to be written on the card.

m rows, n columns -> row index: [0, m-1], column index: [0, n-1]

Card numbers are in the range [1, (m * n) / 2], so that exactly two cards of the same number are displayed.

Assuming a two-dimensional array (returning double pointers), any data structure may be used. However, please use dynamic memory allocation for free game board size support.

struct Figure ** create_figure_table (int row, int col);

Also, create the following function to access any card in the game board.

struct Figure * get_figure (struct Figure ** table, int row, int col);

Also write the following function to return the allocated memory after the game is over.

(c) Memory space for "computer" players

Game Procedure to create a "computer" player for simulation, you need space to remember the location and number of the card you just opened. If you open two cards in the game procedure, but not the correct pair (3.B in the procedure), record them in the storage space. You can then pair your card as soon as it is in memory with the same number of cards as the first opened card in the next round. Paired cards are deleted from memory space.image text in transcribed

However, the size of the memory space is limited to k cards. If you try to save new card information and it is full, you should delete one of the existing saved items. In this case, we will delete First In, First Out, the oldest item.

Write the following function to implement this storage space. Implementation of storage space is free, but dynamic memory allocation should be used. For example, you can use a linked list on a class or a circular queue with an array.

Allocate memory for storage space. For the sake of freedom of implementation, row and column sizes are placed in the parameter list.

void alloc_memory (int row, int col, int k);

Keep the card in storage space. If the space is full, discard the oldest stored card and save the new card instead.

void mem_insert_fig (struct Figure * fig);

Checks for a card with the same value as the requested number value and returns it. If there is no corresponding card, NULL is returned.

struct Figure * find_pairable_fig (int num);

Delete the target card from the storage space. If there is no card, nothing happens.

void mem_delete_fig (struct Figure * fig);

At the end of the game, you should return the memory corresponding to the memory space.

void free_memory (int row, int col, int k);

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

More Books

Students also viewed these Databases questions

Question

-Competitive/Pricing Strategies (Porter)

Answered: 1 week ago

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago