import java.awt.*; import java.util.*; import java.util.concurrent.TimeUnit; import javax.swing.JOptionPane; public class Memory extends java.applet.Applet { private int xMouse; private int yMouse; private boolean mouseClicked = false; private boolean firstRun = true; private boolean gameOver = false; private static final int WIDTH = 100; private static int numClick = 0; private long startTime; private long elapsedTime = -1; /***** add the rest of your instance variables here *****/ /*Sets the background of your memory board to black*/ public void init() { setSize(400,400); setBackground(Color.BLACK); } /*This is main in java applets You don't need to change this method, but you need to fill the methods called inside the paint method, i.e., buildBoard and displayHit */ public void paint(Graphics canvas) { displayBoard(canvas); if(firstRun) //for the first run we need to build our random board { buildBoard(4); firstRun = false; } else if(!gameOver) // once our board is built we will display the game { if (mouseClicked) // if the mouse has been clicked { displayHit(canvas); //find which box the user clicked mouseClicked = false; numClick++; if(numClick == 1) startTime = System.nanoTime(); // count elapsed time } } } /* DO NOT change this method determines if the mouse has been pressed sets x and y Mouse to the location of the mouse arrow redraws the image */ public boolean mouseDown(Event e, int x, int y ) { mouseClicked = true; xMouse = x; yMouse = y; repaint(); return true; } /*DO NOT change this method redraws the scene */ public void update ( Graphics canvas ) { paint(canvas); if(gameOver = isGameOver()) { if(elapsedTime < 0) elapsedTime = TimeUnit.SECONDS.convert(System.nanoTime()-startTime, TimeUnit.NANOSECONDS); JOptionPane.showMessageDialog(null, "Game Over! \nYou found all pairs in " + elapsedTime + " seconds!"); } } /*DO NOT change this method draw the board */ public void displayBoard(Graphics canvas) { canvas.setColor(Color.WHITE); for(int i = 0; i < 400; i += WIDTH) for(int j = 0; j < 400; j += WIDTH) canvas.drawRect(i, j, WIDTH, WIDTH); } /***** fill this method *****/ /* pre: pass in the size of the board post: build an array that holds the memory values for a board of size * size the board will hold two of each int from 0 to size*size/2-1 randomly placed in the array */ public void buildBoard(int size) { } /***** fill this method *****/ /* Pre: xMouse and yMouse have been initialized (stored in the associated instance variables) Post: A circle is displayed in the correct box on the screen */ public void displayHit(Graphics canvas) { int i, j; // add your code to implement the game functionality // when you locate the clicked region, call displayCircle method below to draw the circle } /***** fill this method *****/ /* Pre: i and j are the indices of the clicked region, value is the integer at (i, j) on the board Post: A circle is displayed in the correct box on the screen finish the part to set the color of canvas according to value */ private void displayCircle(Graphics canvas, int i, int j, int value) { // use canvas.setColor(...) to set the color of the circle canvas.fillOval(j*100+30, i*100+30, 40, 40); } /***** fill this method *****/ /* Pre: none Post: return true if the game is over */ private boolean isGameOver() { return false; } }