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:2 | ISBN:9780132830317 | Edition: 5

Question

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

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 that he or she would like to select through a coordinate system.

For example, in the following layout,

1 2 3 4

1 8 * * *

2 * * * *

3 * 8 * *

4 * * * *

all of the face down cards are indicated by *. The pairs of 8 that are face up are at coordinates (1,1) and (2,3). To hide the cards that have been temporarily placed face up, output a large number of newlines to force the old board off the screen.

Hint: Use a 2D array for the arrangement of cards and another 2D array that indicates if a card is face up or face down. Or, a more elegant solution is to create a single 2D array where each element is an object that stores both the card’s value and face. Write a function that “shuffles” the cards in the array by repeatedly selecting two cards at random and swapping them.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

import java.util.Scanner;
public class MatchingGame {
	private int val;
	private boolean revVal;

	public MatchingGame(int value) 
	{
		val = value;
		revVal = false;
	}

	public int getValue() 
	{
		return val;
	}

	public boolean isrevVal() 
	{
		return revVal;
	}

	public void setrevVal(boolean bool) 
	{
		revVal = bool;
	}

	public static void displayBoard(Card[][] cards)
	{


		System.out.println("\t\t1\t2\t3\t4");
		System.out.println("\t-----------------------------------");
		for (int i = 0; i < cards.length; i++) {

			System.out.print((i + 1) + "\t|\t");
			for (int j = 0; j < cards[0].length; j++) {

				if (cards[i][j].isrevVal())
					System.out.print(cards[i][j].getValue() + "\t");
				else
					System.out.print("*\t");
			}
			System.out.println();
		}
	}

	public static Card[][] createBoard()
	{
		Card[][] b = new Card[4][4];
		for (int i = 1; i <= 8; i++) 
		{

			for (int j = 1; j <= 2; j++)
			{
				boolean selectedLocation = false;
				while (!selectedLocation)
				{

					int row = (int) (Math.random() * 4);
					int col = (int) (Math.random() * 4);
					if (b[row][col] == null) {

						selectedLocation = true;
						b[row][col] = new Card(i);

					}
				}
			}
		}


		return b;
	}

	public static boolean wonGame(Card[][] b1) 
	{
	for (int i = 0; i < b1.length; i++) {
			for (int j = 0; j < b1[0].length; j++) {

				if (!b1[i][j].isrevVal())
					return false;
			}
		}

		return true;
	}

	public static void main(String[] args)
	{

		Card[][] board = createBoard();
		Scanner input = new Scanner(System.in);
		while (!wonGame(board)) {
			displayBoard(board);


			System.out.print("Enter first x-coordinate value: ");
			int col1 = input.nextInt();
			System.out.print("Enter first y-coordinate value: ");
			int row1 = input.nextInt();

			System.out.print("Enter second x-coordinate value: ");
			int col2 = input.nextInt();
			System.out.print("Enter second y-coordinate value: ");
			int row2 = input.nextInt();

			Card card1 = board[row1 - 1][col1 - 1];
			Card card2 = board[row2 - 1][col2 - 1];

	if (card1.getValue() == card2.getValue() && !(row1 == row2 && col1 == col2)) {
				card1.setrevVal(true);
				card2.setrevVal(true);
			} else if (row1 == row2 && col1 == col2) {
				System.out.println("Select the same point is not acceptable.");
			}

			else {
System.out.println("Sorry, " + card1.getValue() + " and " + card2.getValue() 
+ " do not match.");
			}

			System.out.println("-----");

		}
		displayBoard(board);
	}
}

Output:

		1	2	3	4
	-----------------------------------
1	|	*	*	*	*	
2	|	*	*	*	*	
3	|	*	*	*	*	
4	|	*	*	*	*	
Enter first x-coordinate value: 2
Enter first y-coordinate value: 2
Enter second x-coordinate value: 2
Enter second y-coordinate value: 2
Select the same point is not acceptable.
-----
		1	2	3	4
	-----------------------------------
1	|	*	*	*	*	
2	|	*	*	*	*	
3	|	*	*	*	*	
4	|	*	*	*	*	
Enter first x-coordinate value: 2
Enter first y-coordinate value: 2
Enter second x-coordinate value: 3
Enter second y-coordinate value: 2
Sorry, 3 and 8 do not match.
-----
		1	2	3	4
	-----------------------------------
1	|	*	*	*	*	
2	|	*	*	*	*	
3	|	*	*	*	*	
4	|	*	*	*	*	
Enter first x-coordinate value: 1
Enter first y-coordinate value: 3
Enter second x-coordinate value: 4
Enter second y-coordinate value: 3
-----
		1	2	3	4
	-----------------------------------
1	|	*	*	*	*	
2	|	*	*	*	*	
3	|	7	*	*	7	
4	|	*	*	*	*	
   ....
   ....
   ....

Enter first x-coordinate value: 1
Enter first y-coordinate value: 2
Enter second x-coordinate value: 3
Enter second y-coordinate value: 4
		1	2	3	4
	-----------------------------------
1	|	5	1	8	2	
2	|	3	6	6	1	
3	|	7	2	5	7	
4	|	4	4	8	3	

 

0 0

Discussions

Post the discussion to improve the above solution.