473,378 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Memory Game Program

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
2 4669
sukatoa
539 512MB
Better to use codetags when posting codes...

Just reminding, :)
sukatoa
Apr 19 '08 #2
Laharl
849 Expert 512MB
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

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

Similar topics

23
by: BlackHawke | last post by:
Hello! This is my second post. Ppl really helped me with the first. I hope there are answers for this one as well I own a game company (www.aepoxgames.net) releasing the beta for our first...
3
by: Marcelo A. Camelo | last post by:
Hi! I will be presenting Python to an audience of game developers, mostly C/C++ programmers. In my presentation I will talk about using python and C/C++ extension instead of pure C/C++ to write...
4
by: Moosebumps | last post by:
When I have time, I am planning to evaluate Python for console game development (on Playstation 2, GameCube, and Xbox). Does anyone have any experience with this? Pretty much the only resource...
18
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a...
19
by: tweak | last post by:
I have been messing around with buffers, and I found it peculiar that the code below will run without a segmentation fault. As far as I know, overwriting the allocated space from a call to...
13
by: hurry | last post by:
In order to avoid declaring static variables in a function I was asked to write a scratch memory. Reserve a block of memory outside the function and assigning pointers to the memory locations as...
16
by: KG | last post by:
Hi, I do have a question. int main() { char *p = (char *)malloc(9); strcpy(p,"TajMahal"); p++;
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
8
Spippo
by: Spippo | last post by:
Hi all, I'm trying to create a trainer for a flash game. I have an AxShockwaveFlash component to play the flash game in a windows form. With an other program (cheat engine) I found the memory...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.