Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,338 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,689 people online right now. Registration is fast and FREE... Join Now!




Memory Game code

 

Memory Game code

djokovic08

7 Jun, 2009 - 11:12 AM
Post #1

New D.I.C Head
*

Joined: 7 Jun, 2009
Posts: 3

I can't figure out what is the problem with my code when I try to play the game. When I press one button, other buttons randomly show up and the code (for the matchUp method, I think) does not work like I planned it to. Please help me!

CODE

import javax.swing.*;
import BreezySwing.*;
import java.util.Random;

public class MemoryGame extends GBFrame{
    //Declares all necessary private instance variables.
    private JButton one;
    private JButton two;
    private JButton three;
    private JButton four;
    private JButton five;
    private JButton six;
    private JButton seven;
    private JButton eight;
    private JButton nine;
    private JButton ten;
    private JButton eleven;
    private JButton twelve;
    private JButton blankOne;
    private JButton blankTwo;
    private JButton blankThree;
    private JButton blankFour;
    private JButton blankFive;
    private JButton blankSix;
    private JButton blankSeven;
    private JButton blankEight;
    private JButton blankNine;
    private JButton blankTen;
    private JButton blankEleven;
    private JButton blankTwelve;
    private JButton match;
    private JButton tileOne;
    private JButton tileTwo;
    private JTextField output;
    private int count;
    private int tileCount;
    private String[] numbers;
    
    public MemoryGame(){
        //Adds all of the window objects to the GUI.
        
        //The blank buttons are placed on top of the tiles
        //to reveal/cover up the numbers(placed randomly).
        blankOne = addButton("  ",1,1,1,1);
        blankTwo = addButton("  ",1,2,1,1);
        blankThree = addButton("  ",1,3,1,1);
        blankFour = addButton("  ",1,4,1,1);
        blankFive = addButton("  ",2,1,1,1);
        blankSix = addButton("  ",2,2,1,1);
        blankSeven = addButton("  ",2,3,1,1);
        blankEight = addButton("  ",2,4,1,1);
        blankNine = addButton("  ",3,1,1,1);
        blankTen = addButton("  ",3,2,1,1);
        blankEleven = addButton("  ",3,3,1,1);
        blankTwelve = addButton("  ",3,4,1,1);
        one = addButton("",1,1,1,1);
        two = addButton("",1,2,1,1);
        three = addButton("",1,3,1,1);
        four = addButton("",1,4,1,1);
        five = addButton("",2,1,1,1);
        six = addButton("",2,2,1,1);
        seven = addButton("",2,3,1,1);
        eight = addButton("",2,4,1,1);
        nine = addButton("",3,1,1,1);
        ten = addButton("",3,2,1,1);
        eleven = addButton("",3,3,1,1);
        twelve = addButton("",3,4,1,1);
        match = addButton("Match??",4,2,1,1);
        output = addTextField("",4,4,1,1);
        count = 0;
        tileCount = 0; //Counts how many tiles are flipped over.
        numbers = new String[12]; //This is a parallel array
                                  //that contains the generated
                                  //number of the corresponding
                                  //button.
        
        //Randomly generates the numbers of the tiles.
        JButton[] tiles = {one, two, three, four, five, six, seven,
                           eight, nine, ten, eleven, twelve};
        Random generator = new Random();
        int k = 0;
        //The tiles array is split in half to make sure
        //a number between 1-6 does not appear twice.
        
        //This while loop sets buttons one through six
        //with numbers one through six.
        while(k <= 5){
            int m = generator.nextInt(6)+ 1;
            String look = m + ""; //The randomly generated number is
                                  //set as a String so it can be a
                                  //parameter for the setLabel method.
            int num = 0;
            //This linear search makes sure the generated number
            //does not already appear in this half of the tiles array.
            for(int j = 0; j < (tiles.length / 2); j++){
                if(tiles[j].getLabel().equals(look))
                    num = -1; //num is set equal to -1 if the generated
                              //number is already in the array.
            }
            if(num != -1){
                tiles[k].setLabel(look);
                numbers[k] = look;
                k++;
            }
        }
        //This while loop sets buttons seven through
        //twelve with numbers one through six.
        //Uses the same system as the first while loop.
        while(k >= 6 && k < 12){
            int m = generator.nextInt(6)+ 1;
            String look = m + "";
            int num = 0;
            for(int j = 6; j < tiles.length; j++){
                if(tiles[j].getLabel().equals(look))
                    num = -1;
            }
            if(num != -1){
                tiles[k].setLabel(look);
                numbers[k] = look;
                k++;
            }
        }
    }
    //Determines what happens when a button is clicked.
    public void buttonClicked(JButton button){
        //If one of the blank buttons on top of the tiles is clicked, the
        //flip method is called.
        if(button == blankOne || button == blankTwo || button == blankThree
           || button == blankFour || button == blankFive || button ==
           blankSix || button == blankSeven || button == blankEight ||
           button == blankNine || button == blankTen || button == blankEleven
           || button == blankTwelve){
               flip(button);
               if(tileCount == 1)
                   tileOne = button; //First tile
               else
                   tileTwo = button; //Second tile
        }
        //If the match button is clicked, the matchUp method is called
        //with the first and second tile as the parameters.
        else if(button == match)
            matchUp(tileOne, tileTwo);
        
    }
    //"Flips" the tile - aka makes the blank button on top invisible,
    //revealing the label of the bottom button.
    private void flip(JButton tile){
        tile.setVisible(false);
        if(tileCount < 2)
            tileCount++;     //Keeps track of the number of tiles flipped.
        else
            tileCount= 0;    //Sets tileCount back to 0.    
    }
    //Determines whether or not the two tiles are equal - have the
    //same number.
    private void matchUp(JButton tileOne, JButton tileTwo){
        String first, second;
        //Uses the parallel array, numbers, to determine
        //what number is on the first tile.
        if(tileOne == blankOne)
            first = numbers[0];
        else if(tileOne == blankTwo)
            first = numbers[1];
        else if(tileOne == blankThree)
            first = numbers[2];
        else if(tileOne == blankFour)
            first = numbers[3];
        else if(tileOne == blankFive)
            first = numbers[4];
        else if(tileOne == blankSix)
            first = numbers[5];
        else if(tileOne == blankSeven)
            first = numbers[6];
        else if(tileOne == blankEight)
            first = numbers[7];
        else if(tileOne == blankNine)
            first = numbers[8];
        else if(tileOne == blankTen)
            first = numbers[9];
        else if(tileOne == blankEleven)
            first = numbers[10];
        else
            first = numbers[11];
        
        //Uses the parallel array, numbers, to determine
        //what number is on the second tile.    
        if(tileTwo == blankOne)
            second = numbers[0];
        else if(tileTwo == blankTwo)
            second = numbers[1];
        else if(tileTwo == blankThree)
            second = numbers[2];
        else if(tileTwo == blankFour)
            second = numbers[3];
        else if(tileTwo == blankFive)
            second = numbers[4];
        else if(tileTwo == blankSix)
            second = numbers[5];
        else if(tileTwo == blankSeven)
            second = numbers[6];
        else if(tileTwo == blankEight)
            second = numbers[7];
        else if(tileTwo == blankNine)
            second = numbers[8];
        else if(tileTwo == blankTen)
            second = numbers[9];
        else if(tileTwo == blankEleven)
            second = numbers[10];
        else
            second = numbers[11];
        
        //If the two tiles match, the message "YAY FOR
        //A MATCH =D" appears in the text box.
        //Then, the blank button is enabled, causing
        //the tiles underneath to stay visible for the
        //duration of the game.
        if(first.equals(second)){
            output.setText("YAY FOR A MATCH =D");
            tileOne.setEnabled(true);
            tileTwo.setEnabled(true);
            count++; //Keeps track of the number of matches.
        }
        //If the two tiles do not match, the message "YOU
        //SUCK. Guess again!" appears in the text box.
        //The tiles are covered again by setting the blank buttons
        //visible.
        else{
            output.setText("YOU SUCK. Guess again!");
            tileOne.setVisible(true);
            tileTwo.setVisible(true);
        }
        //If count is equal to 12 - aka all of the matches are found -
        //the message "HOORAY, YOU ARE THE MEMORY GAME MASTER!" appears
        //in the text box. YAY =]
        if(count == 12)
            output.setText("HOORAY, YOU ARE THE MEMORY GAME MASTER!");
    }
    //Displays the game!!
    public static void main(String [] args){
        MemoryGame gameboard = new MemoryGame();
        gameboard.setSize(750,500);
        gameboard.setVisible(true);
    }
}


User is offlineProfile CardPM
+Quote Post


bear(NP)

RE: Memory Game Code

7 Jun, 2009 - 12:20 PM
Post #2

D.I.C Head
**

Joined: 18 Feb, 2009
Posts: 66



Thanked: 1 times
My Contributions
hey classmate tongue.gif

What was happening when you were editing the Grid World Code? Do you still have it?
PS- I'll look at this and see what I find
User is online!Profile CardPM
+Quote Post

djokovic08

RE: Memory Game Code

7 Jun, 2009 - 12:27 PM
Post #3

New D.I.C Head
*

Joined: 7 Jun, 2009
Posts: 3

QUOTE(bear(NP) @ 7 Jun, 2009 - 12:20 PM) *

hey classmate tongue.gif

What was happening when you were editing the Grid World Code? Do you still have it?
PS- I'll look at this and see what I find


This isn't for gridworld...
User is offlineProfile CardPM
+Quote Post

amooseinabox

RE: Memory Game Code

7 Jun, 2009 - 12:27 PM
Post #4

New D.I.C Head
*

Joined: 10 Mar, 2009
Posts: 6


My Contributions
I've been looking at this for awhile and can't find anything. i will take a look at your code again and hopefully something shows up. but BreezySwing is making it a pain.

@djokovic08 did you ever use gridworld to begin with?



This post has been edited by amooseinabox: 7 Jun, 2009 - 12:28 PM
User is offlineProfile CardPM
+Quote Post

djokovic08

RE: Memory Game Code

7 Jun, 2009 - 01:14 PM
Post #5

New D.I.C Head
*

Joined: 7 Jun, 2009
Posts: 3

yeah i did, but then i started all over again.
User is offlineProfile CardPM
+Quote Post

bear(NP)

RE: Memory Game Code

7 Jun, 2009 - 05:50 PM
Post #6

D.I.C Head
**

Joined: 18 Feb, 2009
Posts: 66



Thanked: 1 times
My Contributions
I've taken an hour straight to look at it, and i don't see anything... However, when selecting two things to be compared against, you can select any number of buttons to be compared, 1-12, and it seems that when a button stays lit up it's completely random...
User is online!Profile CardPM
+Quote Post

pbl

RE: Memory Game Code

7 Jun, 2009 - 09:19 PM
Post #7

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,533



Thanked: 1124 times
Dream Kudos: 450
My Contributions
You should seriously consider learning how to use array
That will reduce your code lines by at least 10 and then the error will be easier to identify:

CODE

private JButton[] btn = new JButton[12];
private JButton[] btnBlank = new JButton[12];

...

for(int i = 0; i < 12; i++) {
  if(tileOne == btnBlank[i]) {
     first = i;
     break;
  }
}

...

Honestly what will you do when asked to make a memory game of 64 X 64 ?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 05:17PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month