Results 1 to 15 of 15
  1. #1
    yanipao is offline Member
    Join Date
    Oct 2009
    Posts
    27
    Rep Power
    0

    Default how i use the random class to random the cards i have

    Hi... I have new code for the memory game and the problem is how can i random them? what's next after having a random class that will generate the number. Here is my code for the memory game:

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;
    import javax.swing.*;
    import static java.util.Collections.*;
    import java.util.Random;
    
    class GuessMe extends JFrame implements ActionListener {
    	
    	JPanel wholePanel, firstPanel, secondPanel;
    	JButton newButton, quitButton; //,scoreButton;			
    	ImageIcon[] pictureIcon = new ImageIcon[13];
    	JButton[] pictureButton = new JButton[12];
    	private ArrayList<Integer> gameList = new ArrayList<Integer>();
    	private int counter = 0;
    	private int[] btnID = new int[2];
    	private int[] btnValue = new int[2];
    	int temp;
    	
    	//Random rnd = new Random();
    
    	//mga declarations
    
    
    	public GuessMe(){
    		Container c = getContentPane();
    		panel ();				
    		firstPanelButtons ();		
    		secondPanelButtons ();					
    		setArrayListText();	
    		c.add(wholePanel); //dapat i-add sa container yung panel para makita
    		
    	}
    	
    	public void panel(){
    		wholePanel = new JPanel();
    		firstPanel = new JPanel();
    		secondPanel = new JPanel();
    		
    		wholePanel.setLayout(new BorderLayout ());
    		firstPanel.setLayout(new FlowLayout());
    		secondPanel.setLayout(new FlowLayout());
    		
    		wholePanel.add(firstPanel, BorderLayout.NORTH); //para sa taas
    		wholePanel.add(secondPanel, BorderLayout.CENTER); //center para i-fill up		
    	}	
    		
    	public void firstPanelButtons (){
    		newButton = new JButton ("New Game");
    		newButton.addActionListener(this);		
    //		scoreButton = new JButton ("Score");
    //		scoreButton.addActionListener(this);
    		quitButton = new JButton ("Quit Game");
    		quitButton.addActionListener(this);		
    		firstPanel.add(newButton);
    		firstPanel.add(quitButton);	
    		}
    		
    	public void secondPanelButtons (){	
    		initPictureIcons();		
    		for(int i=0; i<pictureButton.length; i++){
    		pictureButton[i]=new JButton(pictureIcon[12]);	
    		secondPanel.add(pictureButton[i]);	
    		pictureButton[i].addActionListener(this);	
    		}		
    		}
    		
    	public void initPictureIcons (){
    		pictureIcon[12]=new ImageIcon("images/U0.jpg");
    		pictureIcon[1]=new ImageIcon("images/U1.jpg");
    		pictureIcon[2]=new ImageIcon("images/U2.jpg");
    		pictureIcon[3]=new ImageIcon("images/U3.jpg");
    		pictureIcon[4]=new ImageIcon("images/U4.jpg");
    		pictureIcon[5]=new ImageIcon("images/U5.jpg");
    		pictureIcon[6]=new ImageIcon("images/U6.jpg");
    		pictureIcon[7]=new ImageIcon("images/U7.jpg");
    		pictureIcon[8]=new ImageIcon("images/U8.jpg");
    		pictureIcon[9]=new ImageIcon("images/U9.jpg");
    		pictureIcon[10]=new ImageIcon("images/U10.jpg");
    		pictureIcon[11]=new ImageIcon("images/U11.jpg");
    		pictureIcon[0]=new ImageIcon("images/U12.jpg");		
    		}	
    	
    	public void setArrayListText() {
    		for (int i = 0; i < 2; i++) {
    			for (int ii = 1; ii < (pictureButton.length / 2) + 1; ii++) {
    				gameList.add(ii);
    			}
    		}
    		shuffle(gameList);
    	}
    	
    	public boolean sameValues() {
    		if (btnValue[0] == btnValue[1]) {
    			return true;
    		}
    		return false;
    	}
    	
    	public static void main(String args[]){
    		GuessMe application = new GuessMe();
    	//	Toolkit kit = Toolkit.getDefaultToolkit();
    	//	Image img = kit.getImage("any picture na maliit para icon sa taas");
    	//	application.setIconImage(img); 
    		application.setTitle("Guess Me");
    		application.setSize(610,610);
    		application.setVisible(true);		
    		application.setResizable(false); //para di nababago size
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		if (newButton == e.getSource()){
    			System.(new GuessMe());
    			
    		}
    		if (quitButton == e.getSource()){
    			System.exit(0);
    		}
    		for (int i = 0; i < pictureButton.length; i++) {
    			if (pictureButton[i] == e.getSource()) {
    				temp = gameList.get(i);
    				pictureButton[i].setIcon(pictureIcon[temp]);
    				pictureButton[i].setEnabled(false);
    				counter++;
    				if (counter == 3) {
    					if (sameValues()) {
    						pictureButton[btnID[0]].setEnabled(false);
    						pictureButton[btnID[1]].setEnabled(false);
    					} else {
    						pictureButton[btnID[0]].setEnabled(true);
    						pictureButton[btnID[0]].setIcon(pictureIcon[12]);
    						pictureButton[btnID[1]].setEnabled(true);
    						pictureButton[btnID[1]].setIcon(pictureIcon[12]);
    					}
    					counter = 1;
    				}
    				if (counter == 1) {
    					btnID[0] = i;
    					btnValue[0] = gameList.get(i);
    				}
    				if (counter == 2) {
    					btnID[1] = i;
    					btnValue[1] = gameList.get(i);
    				}
    			}
    		}
    		}
    
    }
    Thanks for the help...

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Do you need a bunch of random numbers, or do you need objects to be sorted randomly?

    If you need a bunch of random numbers, use the Random class. When using the Random class, most methods return a number between 0 and 1 or between 0 and <some passed value>.

    If the return is between 0 and 1, but you need a value in a different range, you can always scale it.

    For example, to get a random double between a and b such that a <= x < b, do the following

    Java Code:
    1) Call [url=http://java.sun.com/javase/6/docs/api/java/util/Random.html#nextDouble()]random.nextDouble()[/url] - the return is a number such that, 0 <= num < 1
    2) Multiple the number by (b - a)
        0 <= (b - a) * num < b - a
    3) Add <a>
        a <= (b - a) * num + a < b

    If the return is instead a number such that, 0 <= num < value, and you want a number such that a <= x < b
    Java Code:
    1) Choose <value> to be (b - a)
    2) 0 <= num < b - a
    3) Add <a>
        a <= num + a < b

    Now, if you have a bunch of values, and you want them in random order, use the Collections.shuffle method. If the values are in an array, you must first convert the array to a list via the Arrays.asList(T...) method.

    Java Code:
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    /**
     * A test
     */
    public class Test
    {
    	/**
    	 * @param args
    	 *            not used
    	 */
    	public static void main(String[] args)
    	{
    		//cannot use int because asList needs an array of objects
    		Integer[] deckArray = new Integer[52];
    		
    		for (int i = 0; i < deckArray.length; i++)
    		{
    			deckArray[i] = i;
    		}
    				
    		List<Integer> deck = Arrays.asList(deckArray);
    		
    		Collections.shuffle(deck);
    		
    		System.out.println(deck);
    	}
    }
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

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

    Default

    thank you for your reply...=) I will actually random a set of images.. how will I do that? thank you.

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    So, you have several images that you want to be sorted in a random order? Use the Collections.shuffle method (it works for any list of objects).

    If, instead, you have an array of objects, use Arrays.asList(T...) to first convert the array to a list, then shuffle it.

    Java Code:
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    
    import javax.swing.ImageIcon;
    
    /**
     * A test
     */
    public class Test
    {
    	private static ImageIcon[] pictureIcon = new ImageIcon[13];
    	
    	/**
    	 * @param args
    	 *            not used
    	 */
    	public static void main(String[] args)
    	{										
    		initPictureIcons();
    
    		//works for any array of objects	
    		List<ImageIcon> images = Arrays.asList(pictureIcon);
    
    		//works for any list of objects
    		Collections.shuffle(images);
    
    		System.out.println(images);
    	}
    	
    	public static void initPictureIcons (){
    		pictureIcon[12]=new ImageIcon("images/U0.jpg");
    		pictureIcon[1]=new ImageIcon("images/U1.jpg");
    		pictureIcon[2]=new ImageIcon("images/U2.jpg");
    		pictureIcon[3]=new ImageIcon("images/U3.jpg");
    		pictureIcon[4]=new ImageIcon("images/U4.jpg");
    		pictureIcon[5]=new ImageIcon("images/U5.jpg");
    		pictureIcon[6]=new ImageIcon("images/U6.jpg");
    		pictureIcon[7]=new ImageIcon("images/U7.jpg");
    		pictureIcon[8]=new ImageIcon("images/U8.jpg");
    		pictureIcon[9]=new ImageIcon("images/U9.jpg");
    		pictureIcon[10]=new ImageIcon("images/U10.jpg");
    		pictureIcon[11]=new ImageIcon("images/U11.jpg");
    		pictureIcon[0]=new ImageIcon("images/U12.jpg");		
    		}	
    
    }
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

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

    Default

    You are so nice...thank you for replying.
    But I got an error when i tried the example code.
    the error says that the reference List is ambiguous, both class.java.util.List in java.util and class java.awt.List in java.awt.match...
    I am new here in java so really don't know what doe that mean. I guess I made a conflict on my code. Will you please help me...really really thank you.

  6. #6
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    It's because your use of star imports (e.g. java.util.*). There is evidently a List class in java.awt, as well as the one I meant, which lies in java.util. In your code, you have both packages importing ALL classes (by using star imports). So, it doesn't know which to use.

    Java Code:
    [COLOR="Red"]import java.awt.*;[/COLOR]
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    [COLOR="#ff0000"]import java.util.*;[/COLOR]
    import javax.swing.*;
    import static java.util.Collections.*;
    import java.util.Random;
    Note: I would suggest not using star imports because things like this can come up. Also, when working in an IDE (like Eclipse or NetBeans) the IDE will automatically handle importing the right class for you.


    When running the below code. Eclipse tells me that "The type List is ambiguous" (note both java.util and java.awt are star imports). It then gives a quick fix to import java.util.List (since java.util.* comes before java.awt.* in my import list). If I switch the order for the two, it allows me to import java.awt.List directly. Why it doesn't offer both suggestions, regardless, is beyond me...

    Java Code:
    import java.util.*;
    import java.awt.*;
    import javax.swing.ImageIcon;
    
    /**
     * A test
     */
    public class Test
    {
    	private static ImageIcon[] pictureIcon = new ImageIcon[13];
    
    	/**
    	 * @param args
    	 *            not used
    	 */
    	public static void main(String[] args)
    	{
    		initPictureIcons();
    
    		// works for any array of objects
    		List<ImageIcon> images = Arrays.asList(pictureIcon);
    
    		// works for any list of objects
    		Collections.shuffle(images);
    
    		System.out.println(images);
    	}
    
    	public static void initPictureIcons()
    	{
    		pictureIcon[12] = new ImageIcon("images/U0.jpg");
    		pictureIcon[1] = new ImageIcon("images/U1.jpg");
    		pictureIcon[2] = new ImageIcon("images/U2.jpg");
    		pictureIcon[3] = new ImageIcon("images/U3.jpg");
    		pictureIcon[4] = new ImageIcon("images/U4.jpg");
    		pictureIcon[5] = new ImageIcon("images/U5.jpg");
    		pictureIcon[6] = new ImageIcon("images/U6.jpg");
    		pictureIcon[7] = new ImageIcon("images/U7.jpg");
    		pictureIcon[8] = new ImageIcon("images/U8.jpg");
    		pictureIcon[9] = new ImageIcon("images/U9.jpg");
    		pictureIcon[10] = new ImageIcon("images/U10.jpg");
    		pictureIcon[11] = new ImageIcon("images/U11.jpg");
    		pictureIcon[0] = new ImageIcon("images/U12.jpg");
    	}
    }

    So, my suggestion would be to not use star imports. However, if you do use them, and this error comes up, just import the class you want, directly, and it will work fine.

    Java Code:
    import java.util.*;
    [COLOR="Red"]import java.util.List;[/COLOR]
    import java.awt.*;
    import javax.swing.ImageIcon;
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

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

    Default

    oh i see... i tried what you have advised me but errors came out again
    can i just use the list in java.awt?

  8. #8
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Quote Originally Posted by yanipao View Post
    oh i see... i tried what you have advised me but errors came out again
    can i just use the list in java.awt?
    Can you post your code then, because I'm not sure what's causing the error.

    And no, you can't use the List class in java.awt as a replacement for the List class in java.util. They provide different functionality, methods, etc - the fact that they share the same name is just a coincidence. If they could be used interchangeably, they wouldn't have specified the class twice.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

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

    Default

    Here is my code..

    //di ko ni random pictures
    // di ko hinide yung pictures
    //walang epekto if click yung mga buttons
    //pwede mong gamiting guide to
    //God bless

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.List;
    import javax.swing.*;
    import java.util.Collections.*;
    import java.util.Random;
    
    class GuessMe extends JFrame implements ActionListener {
    	
    	JPanel wholePanel, firstPanel, secondPanel;
    	JButton newButton, quitButton; //,scoreButton;			
    	ImageIcon[] pictureIcon = new ImageIcon[13];
    	JButton[] pictureButton = new JButton[12];
    	private ArrayList<Integer> gameList = new ArrayList<Integer>();
    	private int counter = 0;
    	private int[] btnID = new int[2];
    	private int[] btnValue = new int[2];
    	int temp;	
    	int numClick;
    	
    	//Random rnd = new Random();
    
    	//mga declarations
    
    
    	public GuessMe(){
    		Container c = getContentPane();
    		panel ();				
    		firstPanelButtons ();		
    		secondPanelButtons ();					
    		setArrayListText();	
    		c.add(wholePanel); //dapat i-add sa container yung panel para makita
    		
    	}
    	
    	public void panel(){
    		wholePanel = new JPanel();
    		firstPanel = new JPanel();
    		secondPanel = new JPanel();
    		
    		wholePanel.setLayout(new BorderLayout ());
    		firstPanel.setLayout(new FlowLayout());
    		secondPanel.setLayout(new GridLayout(4,3));
    		
    		wholePanel.add(firstPanel, BorderLayout.NORTH); //para sa taas
    		wholePanel.add(secondPanel, BorderLayout.CENTER); //center para i-fill up		
    	}	
    		
    	public void firstPanelButtons (){
    		newButton = new JButton ("New Game");
    		newButton.addActionListener(this);		
    //		scoreButton = new JButton ("Score");
    //		scoreButton.addActionListener(this);
    		quitButton = new JButton ("Quit Game");
    		quitButton.addActionListener(this);		
    		firstPanel.add(newButton);
    		firstPanel.add(quitButton);	
    		}
    		
    	public void secondPanelButtons (){	
    		initPictureIcons();		
    		for(int i=0; i<pictureButton.length; i++){
    		pictureButton[i]=new JButton(pictureIcon[12]);	
    		secondPanel.add(pictureButton[i]);	
    		pictureButton[i].addActionListener(this);	
    		}		
    		}
    		
    	public void initPictureIcons (){
    		pictureIcon[12]=new ImageIcon("images/U0.jpg");
    		pictureIcon[1]=new ImageIcon("images/U1.jpg");
    		pictureIcon[2]=new ImageIcon("images/U2.jpg");
    		pictureIcon[3]=new ImageIcon("images/U3.jpg");
    		pictureIcon[4]=new ImageIcon("images/U4.jpg");
    		pictureIcon[5]=new ImageIcon("images/U5.jpg");
    		pictureIcon[6]=new ImageIcon("images/U6.jpg");
    		pictureIcon[7]=new ImageIcon("images/U7.jpg");
    		pictureIcon[8]=new ImageIcon("images/U8.jpg");
    		pictureIcon[9]=new ImageIcon("images/U9.jpg");
    		pictureIcon[10]=new ImageIcon("images/U10.jpg");
    		pictureIcon[11]=new ImageIcon("images/U11.jpg");
    		pictureIcon[0]=new ImageIcon("images/U12.jpg");		
    		}	
    	
    	public void setArrayListText() {
    		for (int i = 0; i < 2; i++) {
    			for (int ii = 1; ii < (pictureButton.length / 2) + 1; ii++) {
    				gameList.add(ii);
    			}
    		}
    		//shuffle(gameList);
    	}
    	
    	public boolean sameValues() {
    		if (btnValue[0] == btnValue[1]) {
    			return true;
    		}
    		return false;
    	}
    	
    	public static void main(String args[]){
    		GuessMe application = new GuessMe();
    	//	Toolkit kit = Toolkit.getDefaultToolkit();
    	//	Image img = kit.getImage("any picture na maliit para icon sa taas");
    	//	application.setIconImage(img); 
    		application.setTitle("Guess Me");
    		application.setSize(610,610);
    		application.setVisible(true);		
    		application.setResizable(false); //para di nababago size
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		if (newButton == e.getSource()){
    			initPictureIcons();
    			List<ImageIcon> images = Arrays.asList(pictureIcon);
    
    			Collections.shuffle(images);
    			
    		}
    		if (quitButton == e.getSource()){
    			System.exit(0);
    		}
    		for (int i = 0; i < pictureButton.length; i++) {
    			if (pictureButton[i] == e.getSource()) {
    				temp = gameList.get(i);
    				this.pictureButton[i].setIcon(pictureIcon[temp]);
    				pictureButton[i].setEnabled(true);
    				counter++;
    				if (counter == 3) {
    					if (sameValues()) {
    						pictureButton[btnID[0]].setEnabled(false);
    						pictureButton[btnID[1]].setEnabled(false);
    					} else {
    						pictureButton[btnID[0]].setEnabled(true);
    						pictureButton[btnID[0]].setIcon(pictureIcon[12]);
    						pictureButton[btnID[1]].setEnabled(true);
    						pictureButton[btnID[1]].setIcon(pictureIcon[12]);
    					}
    					counter = 1;
    				}
    				if (counter == 1) {
    					btnID[0] = i;
    					btnValue[0] = gameList.get(i);
    				}
    				if (counter == 2) {
    					btnID[1] = i;
    					btnValue[1] = gameList.get(i);
    				}
    			}
    		}
    		}
    		
    }
    I have to randomize the pictures when the user click the new button game
    Thank you..
    Could i ask another favor?
    would please check my code and tell me how can i make the images not in grayish color. Thank you very very much
    i really really appreciate your help

  10. #10
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Why did you remove the import for java.util.*? By doing so, all classes that used that import no longer work. This means your use of ArrayList, Arrays, and Collections don't work.

    Instead, you should have imported java.util.List in addition to to the imports you already had. By including java.util.List directly, the compiler knows you meant the List class in the java.util package, and not the one in java.awt. However, you still require the star import, since you have other uses of the package in your code. Of course, you can directly import them, but you have to import them nonetheless.


    I have to randomize the pictures when the user click the new button game
    Thank you..
    Could i ask another favor?
    would please check my code and tell me how can i make the images not in grayish color. Thank you very very much
    i really really appreciate your help
    You're quite welcome, glad to assist.

    For randomizing the pictures, I'm confused as to the question. Are you having difficulty implementing the code I gave you? Or is it a different problem?

    As for the grayish color pictures, Java shouldn't have had any effect on the picture color. Dumb question, maybe, but do the pictures look like that outside of Java (as seen in a picture editor)?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

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

    Default

    it is not really grayish. it is because i setEnabled(false) the pictures. because that is the only way i could compare the pictures. But i want to revise my program. i just don't know how to improve it.

    may i ask that you take a look on it. i tried many experiments bu the recent code I gave you is the only thing that works when two images are matched.

    thank you

  12. #12
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Oh, yeah, I see in your actionPerformed you disable the pictures, why? And what do you mean by "compare". That is, what are you trying to compare (and what result do you expect)?

    As for your loop, it looks like you check to see if one of the picture buttons was pressed. If it is, you "flip the card over", enable the card (wasn't it already enabled??), then do the following

    The first time, you store the picture. The second time you store the picture. The third time a card is flipped you check to see if the last two cards were the same. If so, you disable them (presumably to prevent future events from being fired). If they are different, you "flip the cards back over", and enable the cards.

    The first time through you check if the last two cards are the same only after the third card is picked. After the first time, it works as expected, after the second card.


    Some questions:
    1) Why do you enable the cards, since they were enabled before
    2) Why do you check if two cards are the same only after picking the third card? Why not do it after two cards are picked?
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

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

    Default

    what do you mean by i enable the cards even if it is already enabled? in what part of the code? i just played with that codes. i compare the two pictures after the third click because it is the logic i understand or i can implement. i don't know what loop to use if i will do what you have suggested to already compare the two pictures. i am really thankful that you have time to reply on this. thank you for your time. sorry i keep on asking. i hope you don't mind. thank much.

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

    Default

    i am really glad that there is a forum like this. this is really helpful.. thank you.

  15. #15
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    No, no, questions are good, keep asking as many as you need.

    In your for loop, you enable your picture buttons, but I don't see how it's possible for them to have been disabled for you to need to enable them.

    The only time that they are disabled is if two cards match. However, in this case, shouldn't they remain disabled?

    Java Code:
    for (int i = 0; i < pictureButton.length; i++) {
    	if (pictureButton[i] == e.getSource()) {
    		temp = gameList.get(i);
    		this.pictureButton[i].setIcon(pictureIcon[temp]);
    [COLOR="Red"]		pictureButton[i].setEnabled(true);[/COLOR]
    		counter++;
    		if (counter == 3) {
    			if (sameValues()) {
    				pictureButton[btnID[0]].setEnabled(false);
    				pictureButton[btnID[1]].setEnabled(false);
    			} else {
    [COLOR="#ff0000"]				pictureButton[btnID[0]].setEnabled(true);
    [/COLOR]				pictureButton[btnID[0]].setIcon(pictureIcon[12]);
    [COLOR="#ff0000"]				pictureButton[btnID[1]].setEnabled(true);
    [/COLOR]				pictureButton[btnID[1]].setIcon(pictureIcon[12]);
    			}
    			counter = 1;
    		}
    		if (counter == 1) {
    			btnID[0] = i;
    			btnValue[0] = gameList.get(i);
    		}
    		if (counter == 2) {
    			btnID[1] = i;
    			btnValue[1] = gameList.get(i);
    		}
    	}
    }

    As for having it compare values after the second card is selected, can you describe the "psudo-code" (the algorithm) you used for your for loop?


    In memory, you

    Java Code:
    1a) Select a card
    1b) and flip the card over. 
    
    2a) Then, you select another card
    2b) (different from the first one selected)
    2c) and flip it over.
    
    3a) If the two cards are equal, 
    3b) Keep the two cards flip faced up (and they remain out of play)
    
    4a) If the two cards are not equal,
    4b) Flip the two cards back over
    
    5) Repeat until all cards are flipped face up

    For the first selection, your for loop doesn't check the cards until after the third card is picked. How would you go about checking that the two cards are equal, after the second card is flipped?

    Also, as a note, I don't think you implemented a check to ensure the second card that is selected is different than the first.


    I'm heading to sleep, so I leave you with that. If you have any further questions, don't hesitate to ask. I'm a big question asker myself, I want to know how EVERYTHING works, and I'll keep asking questions until I understand how it all works. So, PLEASE ask questions - it's how we grow, as people, and as programmers.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  2. Help with class project, random number generator.
    By Christopher The Great in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 03:12 AM
  3. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM
  4. Display random cards
    By carl in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 08:01 AM
  5. Help with Random cards
    By carl in forum Java Applets
    Replies: 1
    Last Post: 08-03-2007, 09:48 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
  •