Join 300,481 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,768 people online right now. Registration is fast and FREE... Join Now!
ok i did that wrong n e way i made this memory game and its suppose to use an array of integers and place them in the cells of the game board randomly and you have to click to find a match only right now every square in my board has the same number and i can't figure out what i did wrong I just need to know how to correctly randomize my board
This post has been edited by needhelpbad: 28 Nov, 2008 - 09:51 PM
If you post the code it would really help. It sounds like however you're iterating through the Array is pulling the same value or possibly the contents of the Array is being loaded with the same value. I'd start my debugging by focusing on how the array is populated then how information is pulled from it.
public class MemoryCharm extends java.applet.Applet { private int xMouse; private int yMouse; private boolean mouseClicked = false; private boolean firstRun = true; private static final int WIDTH = 100;
private int [][]array = new int [4][4]; private Color[] color = {Color.WHITE, Color.BLUE, Color.RED, Color.YELLOW, Color.DARK_GRAY, Color.GREEN, Color.ORANGE, Color.PINK}; private int firstX, firstY, nextX, nextY;
/*Sets the background of your memory board to black*/
public void init() { setBackground(Color.BLACK); }
public void paint(Graphics canvas) { if(firstRun) //for the first run we need to build our random board { buildBoard(4); displayBoard(canvas); firstRun = false; } else // 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; } } }
public boolean mouseDown(Event e, int x, int y ) { mouseClicked = true; xMouse = x; yMouse = y; repaint(); return true; }
public void update ( Graphics g ) { paint(g); }
public void buildBoard(int s) { int [] values = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7}; //array w/ values 1-7*2
for (int i = 0; i<values.length-1; i++) { int chosenNumber = (int)((values.length-i)*Math.random()); int temp = values[chosenNumber+i]; values[chosenNumber+i] = values[i]; values[i] = temp; }
int counter = 0;
for(int i = 0; i<4; i++) { for(int k = 0; k <4; k++) { array[i][k] = values[counter++]; } }
}
public void displayBoard(Graphics canvas) { canvas.setColor(Color.WHITE);
sorry guess i should have been a little more detailed im trying to use the numbers 1-7 so i created a 2d array that repeats all of the numbers (which will make the matches) i want the random number to be chosen from that array but each number should only be chosen once. I know that probably sound confusing so sorry i just been working on this for a long while and its kind of complicated to me too
i did that or i thought i did the original array i created was int array then i created the array to hold the values (below) and i attempted to randomize it
CODE
int [] values = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7}; //array w/ values 1-7*2
for (int i = 0; i<values.length-1; i++) { int chosenNumber = (int)((values.length-i)*Math.random()); int temp = values[chosenNumber+i]; values[chosenNumber+i] = values[i]; values[i] = temp; }
If there is anyone out there that could help me with this i would be greatly appreciative. I'm creating a memory game in which you click the squares and try to find the matching circles. The colored circles are assigned to each block using a random 2D array. My problem is for some reason my array isn't working so instead of getting different colors i get the same color circle each time i click . Could someone please help me!!!!!! here's what i have.
public class MemoryCharm extends java.applet.Applet { private int xMouse; private int yMouse; private boolean mouseClicked = false; private boolean firstRun = true; private static final int WIDTH = 100;
private int [][]array = new int [4][4]; private Color[] color = {Color.WHITE, Color.BLUE, Color.RED, Color.YELLOW, Color.DARK_GRAY, Color.GREEN, Color.ORANGE, Color.PINK}; private int firstX, firstY, nextX, nextY;
/*Sets the background of your memory board to black*/
public void init() { setBackground(Color.BLACK); }
public void paint(Graphics canvas) { if(firstRun) //for the first run we need to build our random board { buildBoard(4); displayBoard(canvas); firstRun = false; } else // 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; } } }
public boolean mouseDown(Event e, int x, int y ) { mouseClicked = true; xMouse = x; yMouse = y; repaint(); return true; }
public void update ( Graphics g ) { paint(g); }
public void buildBoard(int s) { int [] values = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7}; //array w/ values 1-7*2
for (int i = 0; i<values.length-1; i++) { int chosenNumber = (int)((values.length-i)*Math.random()); int temp = values[chosenNumber+i]; values[chosenNumber+i] = values[i]; values[i] = temp; }
int counter = 0;
for(int i = 0; i<4; i++) { for(int k = 0; k <4; k++) { array[i][k] = values[counter++]; } }
}
public void displayBoard(Graphics canvas) { canvas.setColor(Color.WHITE);
i did that or i thought i did the original array i created was int array then i created the array to hold the values (below) and i attempted to randomize it
CODE
int [] values = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7}; //array w/ values 1-7*2
for (int i = 0; i<values.length-1; i++) { int chosenNumber = (int)((values.length-i)*Math.random()); int temp = values[chosenNumber+i]; values[chosenNumber+i] = values[i]; values[i] = temp; }
This is the "real" way of doing that type of stuff
CODE
ArrayList<Integer> al = new ArrayList<Integer>(); for(int i = 0; i <= 7; i++) { al.add(i); al.add(i); }
int[] value = new int[al.size()]; Random ran = new Random(); for(int i = 0; i < value.length; i++) { value[i] = al.remove(ran.nextInt(al.size())); }
okay someone please help me i added the repaint method and my code still doesn't work the circles aren't matching instead im gettin a grid filled with all the same colored circles can anyone help me!!!
well what i was trying to do was randomize it so that each time u click a different square it would show a different color using each one twice so i tried to randomize my array
CODE
public void buildBoard(int s) { int [] values = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7}; //array w/ values 1-7*2
for (int i = 0; i<values.length-1; i++) { int chosenNumber = (int)((values.length-i)*Math.random()); int temp = values[chosenNumber+i]; values[chosenNumber+i] = values[i]; values[i] = temp; }
int counter = 0;
for(int i = 0; i<4; i++) { for(int k = 0; k <4; k++) { array[i][k] = values[counter++]; } }
}
and i know i havent' used those variables yet ive changed the code a few times since then and haven't got back to that pact
well what i was trying to do was randomize it so that each time u click a different square it would show a different color using each one twice so i tried to randomize my array
CODE
public void buildBoard(int s) { int [] values = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7}; //array w/ values 1-7*2
for (int i = 0; i<values.length-1; i++) { int chosenNumber = (int)((values.length-i)*Math.random()); int temp = values[chosenNumber+i]; values[chosenNumber+i] = values[i]; values[i] = temp; }
int counter = 0;
for(int i = 0; i<4; i++) { for(int k = 0; k <4; k++) { array[i][k] = values[counter++]; } }
}
and i know i havent' used those variables yet ive changed the code a few times since then and haven't got back to that pact
So that was my first post: "how to randomize an array" but your problem is not there... You can have randomize the array as many times as you want you are drawing the same oval 16 times at the same place
sorry i still kind of learning how to work the site. But anyway so my problem is with the location my circles how do i match the location of the click to correspond with my random array
ok well now i've completely gotten lost. so i changed the location but now i can't click in each square anymore i can only click one square can you tell me where i went wrong
CODE
public void displayHit(Graphics g) { for(int i = 0; i<4; i++) for(int j = 0; j<4; j++) { g.setColor(color[array[i][j]]);
int x = xMouse/WIDTH; x = WIDTH; int y = yMouse/WIDTH; y = WIDTH; g.fillOval(x+30, y+30, 40, 40); } }