Forum Stats

  • 3,875,873 Users
  • 2,266,977 Discussions
  • 7,912,362 Comments

Discussions

Need Help with a Memory Game Applet

843789
843789 Member Posts: 46,655 Green Ribbon
I can get everything to pop up, and the mouse listeners work, but I cannot for the life of me get the pictures to cover up again if they don't match. Does anyone know how to do this?
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.*;
import java.awt.*;
import javax.swing.JApplet;




public class MemGame extends JApplet implements MouseListener {
	
	
	final int wide = 5;
	final int high = 5;
	static int row, col, move[][], grid[][];
	static int count = 3;
	
	MemoryGame field = new MemoryGame();
	
	private final int APPLET_WIDTH = 200;
	private final int APPLET_HEIGHT = 200;
	
	private Image pic1, pic2, pic3, pic4, pic5, pic6, pic7, pic8, pic9, pic10;
	
    
    
	
	public void init(){
		addMouseListener(this);
		setSize (APPLET_WIDTH, APPLET_HEIGHT);
		
		
	}
	
	
	
	public void start(){
		
		 grid = new int[high][wide];
		move = new int[high][wide];
		for (int r = 0; r < high; r++){
			for (int c = 0; c < wide; c++){
				grid[r][c] = MemoryGame.field[r][c];
				move[r][c] = 0;
				System.out.println(grid[r][c] + "     ");
			}
		}
		
		
	}
	
	public void paint(Graphics g){
		int x = getSize().width/wide;
		int y = getSize().height/high;
		
		pic1 = getImage(getDocumentBase(), "pic1.gif");  //  add pictures here
		pic2 = getImage(getDocumentBase(), "pic2.gif");
		pic3 = getImage(getDocumentBase(), "pic3.gif");
		pic4 = getImage(getDocumentBase(), "pic4.gif");
		pic5 = getImage(getDocumentBase(), "pic5.gif");
		pic6 = getImage(getDocumentBase(), "pic6.gif");
		pic7 = getImage(getDocumentBase(), "pic7.gif");
		pic8 = getImage(getDocumentBase(), "pic8.gif");
		pic9 = getImage(getDocumentBase(), "pic9.gif");
		pic10 = getImage(getDocumentBase(), "pic10.gif");
		
		
		
			g.setColor(Color.GRAY);
			g.fillRect(0,0, getSize().width-1, getSize().height-1);
			
			for (int b = 0; b < high; b++){
				for (int a = 0; a < wide; a++){
					g.setColor(Color.blue);
					g.fillRect(x*a, y*b, x-1, y-1);
					
				g.setColor(Color.cyan);
				if (move[b][a] == 1){
					g.drawImage(pic1, x*a, y*b, x-1, y-1, this); 
					}
				if (move[b][a] == 2){
					g.drawImage(pic2, x*a, y*b, x-1, y-1, this);
					}
				if (move[b][a] == 3){
					g.drawImage(pic3, x*a, y*b, x-1, y-1, this);
					}
				if (move[b][a] == 4){
					g.drawImage(pic4, x*a, y*b, x-1, y-1, this);
					}
				if (move[b][a] == 5){
					g.drawImage(pic5, x*a, y*b, x-1, y-1, this);
					}
				if (move[b][a] == 6){
					g.drawImage(pic6, x*a, y*b, x-1, y-1, this);
					}
				if (move[b][a] == 7){
					g.drawImage(pic7, x*a, y*b, x-1, y-1, this);
					}
				if (move[b][a] == 8){
					g.drawImage(pic8, x*a, y*b, x-1, y-1, this);
					}
				if (move[b][a] == 9){
					g.drawImage(pic9, x*a, y*b, x-1, y-1, this);
					}
				if (move[b][a] == 10){
					g.drawImage(pic10, x*a, y*b, x-1, y-1, this);
					}
				
			g.setColor(Color.BLACK);
				}
				
			for (int a = 1; a <= wide; a++)
				g.drawLine(a*x-1, 0, a*x-1, getSize().height-1);
			
			for (int a = 1; a <= high; a++)
				g.drawLine(0, a*y-1, getSize().width-1, a*y-1);
			
			}
		}
	
	public void makeMove(){
		
		int temp = 0;
		
		
		if (move[row][col] == 0){
			move[row][col] = grid[row][col];
			if (count%2 == 1){
				temp = grid[row][col];
				count++;
				move[row][col] = grid[row][col];
			}
			else{
				if (temp == grid[row][col])
				{
					
					
				}
				
				}
		}
	}
	
	
	public void mouseReleased(MouseEvent e){
		int iX = e.getX();
		int iY = e.getY();
		int xdiff = getSize().width/wide;
		int ydiff = getSize().height/high;
		
			if (iX/xdiff >= 0 && iX/xdiff < 5){
				col = iX/xdiff;
				
				if (iY/ydiff >= 0 && iY/ydiff < 4){
					row = iY/ydiff;
						makeMove();
						repaint();
				}
			}
			
	}
	
	public void mousePressed(MouseEvent e){}
	
	public void mouseClicked(MouseEvent e){}
	
	public void mouseEntered(MouseEvent e){}
	
	public void mouseExited(MouseEvent e){}
	
    
        }

Comments

  • 843789
    843789 Member Posts: 46,655 Green Ribbon
    Here's what I would do ( sry, not gonna write out the code because i've gotta clock out in 20 minutes ). I would make a seperate class ( let's call it Picture ). Inside of the Picture class I could have data such as 2 Images. One holding the picture, and another holding the "back of the memory card". Also a boolean: matched. You could have a getPicture() method in your Picture class and within that, check your matched boolean.
    if(matched)return picture;
    else return backOfCard;
    Somethin like that. An easy way to tell if they're matched is to create a matrix of ints( grid[row][col] ). Depending on how many images you have keep 2 of each number in there. For example, if you have 10 images, fill up the matrix with 1,1,2,2,3,3,4,4,5,5. After shuffling now you need to draw them. You could do this now either of two ways.

    1. Make one Image object, and have your getPicture() require an int parameter. In your image class load up all images and just return one according to which int you receive. Ex. pic.getPicture(1); will return the Image pic1 that you loaded earlier in the class, and so fourth.

    2. Just make x number of images according to how many you think you will have, then just have your getPicture() return the specific image. Then it's a series of the same number x if statements, or a switch case.

    So as of right now your matched is false, so when you're drawing the grid you could do something like this.
        
    for(int x=0;x<gridRows;x++){
    for(int y=0;y<gridCols;y++){
    gr.drawImage(pic.getImage(grid[x][y]),x*(WIDTH/gridCols),y*(HEIGHT/gridRows));
    // I just grabbed this outta my butt, so it just gets the image based on the 
    // number in the corresponding spot in the grid, and drew it at that ugly spot.
    // It's saying take the width of the applet, divide it by the cols or rows, then
    // then same for the height. You'll have to play around with image size sry.
    }
    }
    I would use MouseClicked(MouseEvent e){} in the mouseListener class instead of mouseReleased and such. Just makes it easier to look at and deal with later on. I'm sure you could manipulate the ugly coordinates i used to draw the picture to check where you clicked, otherwise just do a 4 condition if statement to check if your click is in the bounds of a certain pic. Here, psudo code might be easier to follow.
    public void MouseClicked(MouseEvent e){
    int x=e.getX();
    int y=e.getY();
    clickCount++;
    
    if(clickCount%3>0){
    If( your click is within the bounds for a certain rowxcol){
    Image temp1=where you first clicked;
    make that corresponding image matched boolean=true;
    }
    if( your click is within the bounds for another rowxcol){
    Image temp2=where you second clicked;
    make that one's matched=true;
    }
    }else if(!(temp1.isMatched() && temp2.isMatched())){
    temp1.setMatched(false);
    temp2.setMatched(false);
    }
    }
    That way, you keep track of how many times you clicked, if you think about it your first click is for your first image, second for your second, and third could be a reset/continue on click. Make sure you print a string or soemthing letting people know to click to reset/continue for next match. Anyway I gotta clock out so I know that was confusing, but hopefully you can pull SOMETHING out, or that I sparked an idea or something. Good Luck! Any questions e-mail me at [email protected]
This discussion has been closed.