Results 1 to 8 of 8
  1. #1
    arian88 is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default Help to make memory game :=)

    Hello,
    i tryed to make a game with java, Memory game.
    All thing works fine, GUI, and component and all
    but i have not any method to chek choices, and disable or enable button.

    Exmp. if i choice button with value 2 first, and second time the same value in other button so the both button must be disable...
    if not, both button well be enable and text must be hide...
    and user must try again...

    i need a method like that..
    check if both is same value, do something, if not do some other thing...

    i tryed many code... is very simple, but i dont understand how to make that logic to code...

    i put my code here...
    if i must tell better so just say :=)

    method "checkValue" is empty! The are that code i need :=)

    Java Code:
    package MemoryGame;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;
    import javax.swing.*;
    import static java.util.Collections.*;
    
    public class MemoryGame extends JFrame implements ActionListener {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JButton exitBtn, replayBtn;
    	private JButton[] gameBtn = new JButton[16];
    	private ArrayList<Integer> gameList = new ArrayList<Integer>();
    
    	public MemoryGame() {
    		init();
    		panel();
    		setArrayListText();
    		setTitle("MemoryGame");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setSize(500, 500);
    		setVisible(true);
    	}
    
    	public void init() {
    		for (int i = 0; i < gameBtn.length; i++) {
    			gameBtn[i] = new JButton();
    			gameBtn[i].setFont(new Font("Serif", Font.BOLD, 28));
    			gameBtn[i].addActionListener(this);
    		}
    		exitBtn = new JButton("Exit");
    		exitBtn.addActionListener(this);
    		replayBtn = new JButton("Replay");
    		replayBtn.addActionListener(this);
    	}
    
    	public void panel() {
    		Panel gamePnl = new Panel();
    		gamePnl.setLayout(new GridLayout(4, 4));
    		for (int i = 0; i < gameBtn.length; i++) {
    			gamePnl.add(gameBtn[i]);
    		}
    
    		Panel buttonPnl = new Panel();
    		buttonPnl.add(replayBtn);
    		buttonPnl.add(exitBtn);
    		buttonPnl.setLayout(new GridLayout(1, 0));
    
    		add(gamePnl, BorderLayout.CENTER);
    		add(buttonPnl, BorderLayout.SOUTH);
    
    	}
    
    	public void setArrayListText() {
    		for (int i = 0; i < 2; i++) {
    			for (int ii = 1; ii < (gameBtn.length / 2) + 1; ii++) {
    				gameList.add(ii);
    			}
    		}
    		shuffle(gameList);
    
    		// ///////////////////
    		int newLine = 0;
    		for (int a = 0; a < gameList.size(); a++) {
    			newLine++;
    			System.out.print(" " + gameList.get(a));
    			if (newLine == 4) {
    				System.out.println();
    				newLine = 0;
    			}
    		}
    	}
    
    	public void chekValues() {
    
    
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if (exitBtn == e.getSource()) {
    			System.exit(0);
    		}
    		if (replayBtn == e.getSource()) {
    		}
    
    		for (int i = 0; i < gameBtn.length; i++) {
    			if (gameBtn[i] == e.getSource()) {
    				gameBtn[i].setText("" + gameList.get(i));
    				gameBtn[i].setEnabled(false);
    							
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		new MemoryGame();
    	}
    }
    Attached Thumbnails Attached Thumbnails Help to make memory game :=)-memorygame.jpg  

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Think about how the game dynamic works:
    You need a counter for each round of guesses: there are two successive guesses per round.
    On the first guess: save the number the user revealed
    On the second guess:
    1. get second guess and compare the two numbers
      1. if they are the same: score a point
      2. else: maybe no action required
    2. disable the buttons or reset them as desired

    Get ready for the next round of two guesses: reset counter.

    You can use member variables for the counter and to save the first guess.
    Java Code:
    public class MemoryGame extends JFrame implements ActionListener {
    
        // member variables in class scope
        private static final long serialVersionUID = 1L;
        private JButton exitBtn, replayBtn;
        private JButton[] gameBtn = new JButton[16];
        private ArrayList<Integer> gameList = new ArrayList<Integer>();
        int counter = 0;
        int firstGuess = -1;
    Any way you can get this to work is okay. After you get it working the way you want you can step back, take another look and see if you want to refactor your code/design or not.

  3. #3
    arian88 is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default

    Thanks man, its working now...
    here is the source... :=) any tips?

    Java Code:
    package MemoryGame;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;
    import javax.swing.*;
    import static java.util.Collections.*;
    
    public class MemoryGame extends JFrame implements ActionListener {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private JButton exitBtn, replayBtn;
    	private JButton[] gameBtn = new JButton[16];
    	private ArrayList<Integer> gameList = new ArrayList<Integer>();
    	private int counter = 0;
    	private int[] btnID = new int[2];
    	private int[] btnValue = new int[2];
    
    	public MemoryGame() {
    		init();
    		panel();
    		setArrayListText();
    		setTitle("MemoryGame");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setSize(500, 500);
    		setVisible(true);
    	}
    
    	public void init() {
    		for (int i = 0; i < gameBtn.length; i++) {
    			gameBtn[i] = new JButton();
    			gameBtn[i].setFont(new Font("Serif", Font.BOLD, 28));
    			gameBtn[i].addActionListener(this);
    		}
    		exitBtn = new JButton("Exit");
    		exitBtn.addActionListener(this);
    		replayBtn = new JButton("Replay");
    		replayBtn.addActionListener(this);
    	}
    
    	public void panel() {
    		Panel gamePnl = new Panel();
    		gamePnl.setLayout(new GridLayout(4, 4));
    		for (int i = 0; i < gameBtn.length; i++) {
    			gamePnl.add(gameBtn[i]);
    		}
    
    		Panel buttonPnl = new Panel();
    		buttonPnl.add(replayBtn);
    		buttonPnl.add(exitBtn);
    		buttonPnl.setLayout(new GridLayout(1, 0));
    
    		add(gamePnl, BorderLayout.CENTER);
    		add(buttonPnl, BorderLayout.SOUTH);
    
    	}
    
    	public void setArrayListText() {
    		for (int i = 0; i < 2; i++) {
    			for (int ii = 1; ii < (gameBtn.length / 2) + 1; ii++) {
    				gameList.add(ii);
    			}
    		}
    		shuffle(gameList);
    
    		// ///////////////////
    		int newLine = 0;
    		for (int a = 0; a < gameList.size(); a++) {
    			newLine++;
    			System.out.print(" " + gameList.get(a));
    			if (newLine == 4) {
    				System.out.println();
    				newLine = 0;
    			}
    		}
    	}
    
    	public boolean sameValues() {
    		if (btnValue[0] == btnValue[1]) {
    			return true;
    		}
    		return false;
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		if (exitBtn == e.getSource()) {
    			System.exit(0);
    		}
    		if (replayBtn == e.getSource()) {
    
    		}
    		for (int i = 0; i < gameBtn.length; i++) {
    			if (gameBtn[i] == e.getSource()) {
    				gameBtn[i].setText("" + gameList.get(i));
    				gameBtn[i].setEnabled(false);
    				counter++;
    				if (counter == 3) {
    					if (sameValues()) {
    						gameBtn[btnID[0]].setEnabled(false);
    						gameBtn[btnID[1]].setEnabled(false);
    					} else {
    						gameBtn[btnID[0]].setEnabled(true);
    						gameBtn[btnID[0]].setText("");
    						gameBtn[btnID[1]].setEnabled(true);
    						gameBtn[btnID[1]].setText("");
    					}
    					counter = 1;
    				}
    				if (counter == 1) {
    					btnID[0] = i;
    					btnValue[0] = gameList.get(i);
    				}
    				if (counter == 2) {
    					btnID[1] = i;
    					btnValue[1] = gameList.get(i);
    				}
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		new MemoryGame();
    	}
    }

  4. #4
    schnappi is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    How do you get this program to work with images instead?

    I have 8 images and i wish to loop through them through an array?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hello schnappi and welcome to the forum!

    So, what have you tried so far?

  6. #6
    schnappi is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    heres what i have.. but im having trouble storing my images in an array.

    Java Code:
    	public void setImageIcon()
    	{
    		for (int i = 0; i < 2; i++)
    		{
    			for (int ii = 1; ii < (cardButton.length / 2) + 1; ii++)
    			{
    				gameList.add(ii);
    			}	
    		}
    		
    		/*shuffle(gameList);
    		
    		int newLine = 0;
    		for (int a = 0; a < gameList.size(); a++)
    		{
    			newLine++;
    		
    			if (newLine == 4)
    			{			
    				newLine = 0;
    			}
    		}*/
    	}
    	
    	public boolean sameValues()
    	{
    		if (btnValue[0] == btnValue[1])
    		{
    			return true;
    		}
    		return false;
    	}		
    	
    	class displayHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			for (int i = 0; i < cardButton.length; i++)
    			{
    				if (cardButton[i] == e.getSource())
    				{
    					cardButton[i].setIcon(p1);
    					cardButton[i].setEnabled(false);
    					counter++;
    					
    				if (counter == 3)
    				{
    					if (sameValues())
    					{
    						cardButton[btnID[0]].setEnabled(false);
    						cardButton[btnID[1]].setEnabled(false);
    					}
    					else
    					{
    						cardButton[btnID[0]].setEnabled(true);
    						cardButton[btnID[0]].setIcon(p3);
    						cardButton[btnID[1]].setEnabled(true);
    						cardButton[btnID[1]].setIcon(p2);
    					}
    					counter = 1;
    				}
    				
    				if (counter == 1)
    				{
    					btnID[0] = i;
    					btnValue[0] = gameList.get(i);
    				}
    				
    				if (counter == 2)
    				{
    					btnID[1] = i;
    					btnValue[1] = gameList.get(i);
    				}
    								
    			   }
    		    }	
    		}
    				
    	}

  7. #7
    lclclc is offline Member
    Join Date
    Aug 2009
    Posts
    22
    Rep Power
    0

    Default

    how to put image in this code?

  8. #8
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

Similar Threads

  1. Memory Game
    By torres in forum AWT / Swing
    Replies: 6
    Last Post: 04-22-2009, 12:00 AM
  2. Replies: 3
    Last Post: 04-20-2009, 10:53 PM
  3. how to make mastermind game
    By javabeginer in forum New To Java
    Replies: 10
    Last Post: 04-14-2009, 03:11 AM
  4. help with memory game!
    By rac in forum New To Java
    Replies: 6
    Last Post: 04-14-2009, 12:39 AM
  5. Replies: 2
    Last Post: 08-21-2008, 08:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •