SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Arrays
Exercise:
Programming Projects
Question:13 | ISBN:9780132846813 | Edition: 5

Question

A common memory matching game played by young children is to start with a deck of cards that contains identical pairs. For example, given six cards in the deck, two might be labeled “1”, two might be labeled “2”, and two might be labeled “3”. The cards are shuffled and placed facedown on the table. The player then selects two cards that are facedown, turns them faceup, and if they match they are left faceup. If the two cards do not match, they are returned to their original position facedown. The game continues in this fashion until all cards are faceup.

Write a program that plays the memory matching game. Use 16 cards that are laid out in a square and are labeled with pairs of numbers from 1 to 8. Your program should allow the player to specify the cards through a coordinate system. For example, in the following layout

1 2 3 4

1 8 * * *

2 * * * *

3 * 8 * *

4 * * * *

all of the cards are facedown except for the pair of 8’s, which has been located at coordinates (1,1) and (2,3). To hide the cards that have been temporarily placed faceup, output a large number of newlines that force the old board off the screen.

Hint: Use a 2D array for the arrangement of cards and another 2D array that indicates whether a card is faceup or facedown. Write a function that shuffles” the cards in the array by repeatedly selecting two cards at random and swapping them.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.