Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A common memory matching game played by young children is to start with a deck o

ID: 3700466 • Letter: A

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 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 board

Explanation / Answer

CODE IN JAVA:

import java.awt.Color;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Random;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

import javax.swing.Timer;

import org.wintrisstech.cards.Card;

import org.wintrisstech.cards.Deck;

public class MemoryGame extends JPanel implements Runnable, ActionListener

{

private static final int NUM_ROWS = 4;

private static final int NUM_COLUMNS = 6;

private static final int NUM_CARDS = NUM_ROWS * NUM_COLUMNS;

private static final int AUTOFLIP_TIME = 1000;

private int state = 0;

private CardButton first;

private CardButton second;

private int misses = 0;

private int matches = 0;

private final Deck deck;

private final Random random = new Random();

private final CardButton[] buttons;

private final Timer autoFlipTimer;

private MemoryGame()

{

buttons = new CardButton[NUM_CARDS];

deck = new Deck(Color.RED);

autoFlipTimer = new Timer(AUTOFLIP_TIME, this);

autoFlipTimer.setRepeats(false);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new MemoryGame());

}

public void run()

{

JFrame frame = new JFrame("Memory");

setLayout(new GridLayout(NUM_ROWS, NUM_COLUMNS));

Card[] selectedCards = selectCards();

for (int i = 0; i < NUM_CARDS; i++)

{

CardButton cardButton = new CardButton(selectedCards[i]);

cardButton.addActionListener(this);

add(cardButton);

buttons[i] = cardButton;

}

frame.add(this);

frame.pack();

frame.setResizable(false);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

private Card[] selectCards()

{

if (deck.getCount() < NUM_CARDS / 2)

{

deck.shuffle();

}

Card[] selection = new Card[NUM_CARDS];

Card card = deck.getCard();

selection[1] = selection[0] = card;

for (int index = 2; index < NUM_CARDS; index++)

{

if ((index & 1) == 0)

{

card = deck.getCard();

}

int randomIndex = random.nextInt(index + 1);

selection[index] = selection[randomIndex];

selection[randomIndex] = card;

}

return selection;

}

public void actionPerformed(ActionEvent evt)

{

if (evt.getSource() == autoFlipTimer)

{

autoFlip();

}

else

{

CardButton button = (CardButton) evt.getSource();

handleClick(button);

}

}

private void handleClick(CardButton button)

{

switch (state)

{

case 0:

if (button.isFaceUp())

{

}

else

{

first = button;

first.flip();

state = 1;

}

break;

case 1:

if (button.isFaceUp())

{

}

else

{

second = button;

second.flip();

if (first.getCard().equals(second.getCard()))

{

matches++;

endOfGameCheck();

state = 0;

}

else

{

misses++;

autoFlipTimer.start();

state = 2;

}

}

break;

case 2:

autoFlipTimer.stop();

first.flip();

second.flip();

if (button.isFaceUp())

{

state = 0;

} else {

first = button;

first.flip();

state = 1;

}

break;

default:

}

}

private void endOfGameCheck()

{

if (endOfGame())

{

if (playAgain())

{

resetGame();

} else {

System.exit(0);

}

}

}

private void autoFlip()

{

if (state == 2)

{

first.flip();

second.flip();

state = 0;

}

}

private boolean endOfGame()

{

return matches * 2 == NUM_CARDS;

}

private boolean playAgain()

{

return JOptionPane.showConfirmDialog(this, "You had " + misses+ (misses == 1 ? " miss" : " misses") + ". Play again?","End of Game", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;

}

private void resetGame()

{

final Card[] selectedCards = selectCards();

for (int i = 0; i < selectedCards.length; i++)

{

buttons[i].setCard(selectedCards[i]);

}

misses = matches = 0;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote