By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

Memory Game Program

P: 1
I'm trying to make a memory game where you try to link up the two corresponding colored circles, but if they are the wrong colors, they go back to being a blank canvas. The graphics methods do not link up, and I'm not sure why. Anyone mind taking a look at my code and seeing what the problem is?

[CODE import javax.swing.*;
import java.awt.*;
import java.lang.Math;

public class Memory extends java.applet.Applet
{
private int xMouse;
private int yMouse;
private boolean mouseClicked = false;
private boolean firstRun = true;
private static final int WIDTH = 100;
//add the rest of your instance variables here
int[][] board = new int[4][4];


/*Sets the background of your memory board to black*/
public void init()
{
setBackground(Color.BLACK);
}
/*This is main in java applets
You may need to add (not change) a couple things in this method
*/
public void paint(Graphics canvas)
{
if(firstRun) //for the first run we need to build our random board
{
buildBoard(4);
firstRun = false;
}
else // once our board is built we will display the game
{
displayGame(canvas);
if (mouseClicked) // if the mouse has been clicked
{
displayHit(canvas, x, y);//find which box the user clicked
mouseClicked = false;
}
}
}

/*
DO NOT change this method
determins 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 g )
{
paint(g);
}

/*
pre: none
post: build an array that holds the memory vales for a board of size x size
the board will hold two of each int from 0 to size randomly placed in the array
*/
public void buildBoard(int s)
{
int[] board1D = new int[16];
for(int i=0; i<16; i++)
{
board1D[i] = i % 8;
shuffle(board1D);

}
int index=0;
for(int i=0; i < board.length; i++)
{
for(int j=0; j < board[i].length; j++)
{

board[i][j] = board1D[index];
index = index + 1;
}
}
}

public void shuffle(int[] index)
{
int j;
int k = index.length;
for (int i=0; i < 15; i++)
{
j = ((int)(((double)(k - index.length))*Math.random())) + i;
int temp = index[i];
index[i] = index[j];
index[j] = temp;
}
printArray(index);
}

public static void printArray(int[] board)
{
System.out.print("Random Order.");
for(int i=0; i < board.length; i++)
System.out.print(" " + board[i]);
System.out.println();
}


public static void displayGame(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);

canvas.drawNumbers(canvas, WIDTH);
}


/*
Pre: xMouse and yMouse have been initialized
Post: A circle is displayed in the correct box on the screen
Currenlty the circle is displayed at the mouse location
*/
public void displayHit(Graphics g, int x, int y)
{
canvas.ballColor(g, board[x][y]);
g.setColor(Color.WHITE);
g.fillOval(xMouse, yMouse, 40, 40);
}

public void drawNumbers(Graphics canvas, int w)
{
int w2 = w / 3; // where to display number
for(int i = 0; i < board.length; i++)
for(int j = 0; j < board[i].length; j++)
{
canvas.setNumberColor(canvas, i, j);
canvas.drawString(Integer.toString(board[i][j]), w*i + w2, w*j + (w/2));
}
}

public void setNumberColor(Graphics canvas, int i, int j)
{
int r = 255;
int g = 255;
int b = 255;
Color c = new Color(r-20*i, g-20*j, b-i*j);
canvas.setColor(c);
}


public void setBallColor(Graphics g, int i)
{
switch(i)
{
case 0:
g.setColor(Color.GREEN); break;
case 1:
g.setColor(Color.YELLOW); break;
case 2:
g.setColor(Color.LIGHT_GRAY); break;
case 3:
g.setColor(Color.MAGENTA); break;
case 4:
g.setColor(Color.BLUE); break;
case 5:
g.setColor(Color.RED); break;
case 6:
g.setColor(Color.ORANGE); break;
case 7:
g.setColor(Color.CYAN); break;
default:
g.setColor(Color.WHITE); break;
}
}
}[/code]
Apr 18 '08 #1
Share this Question
Share on Google+
2 Replies


100+
P: 539
Better to use codetags when posting codes...

Just reminding, :)
sukatoa
Apr 19 '08 #2

Expert 100+
P: 849
The OP did, he just missed a ] at the top.

As to the actual question, the only thing I see after a quick scan (Graphics isn't really my forte) is that anything in the java.lang package is always imported, so you don't need to import java.lang.Math.
Apr 19 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.