Program in Java - create a program under the following guidelines to play the me
ID: 3700300 • Letter: P
Question
Program in Java - create a program under the following guidelines to play the memory matching game.
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 Java program that plays the memory matching game. Use 16 cards that are laid out in a 4 x 4 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 234 38 * all of the face down cards are indicated by *. The pairs of 8 that are face up are at coordinates (1,1) and (3,2). 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. Notes Your program must use two arrays: e one array to store the board values, and e a second array to determine whether or not to display each value on the board .Your program must implement and use calls to the following methods e a method that gets the value at a particular spot on the board a method that compares two spots on the board a method that shows a particular spot on the board a method that hides a particular spot on the boardExplanation / Answer
// All methods written in single class for better understanding of logic.
// Application.java
import java.util.Random;
import java.util.Scanner;
public class Application {
private static int[][] DECK = new int[4][4];
private static boolean[][] SHOW = new boolean[4][4];
public static void main(String[] args) {
initDeck();
shuffleDeck();
showDeck();
Scanner scanner = new Scanner(System.in);
int i, j, k, l;
int opt = 0;
do {
System.out.println("Choose option");
System.out.println("1. Continue game");
System.out.println("2. Exit");
opt = scanner.nextInt();
switch (opt) {
case 1:
System.out.println("Enter coordinates for 1st card");
System.out.print("i:");
i = scanner.nextInt() -1;
if(!isValidCoordinate(i)) {
System.out.println("Please enter valid coordinate.");
break;
}
System.out.print("j:");
j = scanner.nextInt() -1;
if(!isValidCoordinate(j)) {
System.out.println("Please enter valid coordinate.");
break;
}
System.out.println("Enter coordinates for 2nd card");
System.out.print("i:");
k = scanner.nextInt() -1;
if(!isValidCoordinate(k)) {
System.out.println("Please enter valid coordinate.");
break;
}
System.out.print("j:");
l = scanner.nextInt() -1;
if(!isValidCoordinate(l)) {
System.out.println("Please enter valid coordinate.");
break;
}
System.out.println("Cards on board");
showCardOnBoard(i, j);
showCardOnBoard(k, l);
showDeck();
if (compareCards(i, j, k, l)) {
System.out.println("Congratulation..! Cards are same.");
} else {
System.out.println("Cards does not match. Hiding Cards in deck. Try again..!");
hideCardOnBoard(i, j);
hideCardOnBoard(k, l);
showDeck();
}
break;
case 2:
scanner.close();
System.exit(1);
break;
default:
System.out.println("Choose appropriate option");
}
} while (opt != 2);
scanner.close();
}
private static boolean isValidCoordinate(int input){
if(input < DECK.length) {
return true;
}
return false;
}
// method to get card at particular spot on board
private static int getCardOnBoard(int x, int y) {
return DECK[x][y];
}
// method to compare two cards on board
private static boolean compareCards(int i, int j, int k, int l) {
if (getCardOnBoard(i, j) == getCardOnBoard(k, l)) {
return true;
}
return false;
}
private static void showCardOnBoard(int i, int j) {
SHOW[i][j] = true;
}
private static void hideCardOnBoard(int i, int j) {
SHOW[i][j] = false;
}
// Initial Deck
private static void initDeck() {
int card = 0;
for (int i = 0; i < DECK.length; i++) {
for (int j = 0; j < DECK.length; j++) {
if (card == DECK.length * 2) {
card = 1;
}
DECK[i][j] = ++card;
hideCardOnBoard(i,j);
}
}
}
// Shuffle Deck
private static void shuffleDeck() {
System.out.println("Deck length:" + DECK.length);
for (int i = 0; i < DECK.length * 2; i++) {
swapCards(getRandom(), getRandom(), getRandom(), getRandom());
}
}
private static int getRandom() {
Random random = new Random();
return random.nextInt(3);
}
private static void swapCards(int i, int j, int k, int l) {
int tmp = DECK[i][j];
DECK[i][j] = DECK[k][l];
DECK[k][l] = tmp;
}
// Show Board
private static void showDeck() {
for (int i = 0; i < DECK.length; i++) {
for (int j = 0; j < DECK.length; j++) {
System.out.print(" ");
if (SHOW[i][j]) {
System.out.print(DECK[i][j]);
} else {
System.out.print("*");
}
System.out.print(" ");
}
System.out.println();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.