Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Andorid Memory Matching game help

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 5 Times in 1 Post

    Default Andorid Memory Matching game help

    Hello, I am trying to make a android application memory matching game. I have a lot of problems with my code, but it works somewhat. Here are two of my main questions here.
    When I click on the first button it displays the color, but no matter if I get a match, or no match, it will not display the second buttons color.
    Also, when i click both of the buttons, if it is a match I have them both set to white. But, if i don't get a match I have them set to gray because I don't know how to set them back to the orginal button view. The problem is once multiple buttons turn gray you cannot see where a button ends/starts. I am new to asking questions/this site so if I am doing something wrong please let me know. Here is my code.

    public class Memory_Matcher extends Activity
    {
    private android.view.View.OnClickListener buttonListener;
    private MemoryCards Cards[] = new MemoryCards [16];
    private User player = new User();
    int firstCard = -1;
    int secondCard = -1;
    //public boolean match;
     
    private int matchCount;
     
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
     
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_memory__matcher);
     
    buttonListener = new Button.OnClickListener()
    {
    public void onClick(View v)
    {
     
    if (player.firstCard() == -1 || player.secondCard() == -1)
    { //change button color
    if(v.getId() == 0 || v.getId()==10)
    {
    v.setBackgroundColor(Color.RED);
     
    }
    else if(v.getId() == 3 || v.getId()==4)
    {
    v.setBackgroundColor(Color.BLUE);
     
    }
    else if(v.getId() == 5 || v.getId()==9)
    {
    v.setBackgroundColor(Color.GREEN);
     
    }
    else if(v.getId() == 6 || v.getId()==13)
    {
    v.setBackgroundColor(Color.YELLOW);
     
    }
    else if(v.getId() == 1 || v.getId()==14)
    {
    v.setBackgroundColor(Color.DKGRAY);
     
    }
    else if(v.getId() == 2|| v.getId()==7)
    {
    v.setBackgroundColor(Color.CYAN);
     
    }
    else if(v.getId() ==11||v.getId()==12)
    {
    v.setBackgroundColor(Color.BLACK);
     
    }
    else if(v.getId() == 8 || v.getId()==15)
    {
    v.setBackgroundColor(Color.MAGENTA);
     
    }
     
     
    }
     
     
    if(player.firstCard() == -1)
    {
    player.setFirstCard(v.getId());
    Cards[v.getId()].setStatus(1);
    }
    else if(player.secondCard() == -1)
    {
    player.setSecondCard(v.getId());
    Cards[v.getId()].setStatus(1);
    //attempting to wait
     
     
    }
     
     
     
    if(matchCheck() == true)
     
    {
     
    Button button1 = (Button) findViewById(player.firstCard());
    Button button2 = (Button) findViewById(player.secondCard());
    button1.setBackgroundColor(Color.WHITE);
    button2.setBackgroundColor(Color.WHITE);
     
     
     
    for (int i = 0; i<=15; i++)
    {
    if(Cards[i].cardStatus() == 1)
    {
    Cards[i].setStatus(2);
    player.setFirstCard(-1);
    player.setSecondCard(-1);
    matchCount++;
    }
    if(matchCount==8)
    {
    //displaytoast
    }
    }
    }
    else
    {
     
    if (player.firstCard() != -1 && player.secondCard() != -1)
    {
    for (int i = 0; i<=15; i++)
    {
     
    if(Cards[i].cardStatus() == 1)
    {
    Button button1 = (Button) findViewById(player.firstCard());
    Button button2 = (Button) findViewById(player.secondCard());
    button1.setBackgroundColor(Color.GRAY);
    button2.setBackgroundColor(Color.GRAY);
     
     
    Cards[i].setStatus(0);
     
    }
     
     
     
     
    }
    player.setFirstCard(-1);
    player.setSecondCard(-1);
    }
    }
    };
    };
     
     
    // super.onCreate(savedInstanceState);
     
     
     
     
     
    TableLayout tableLayout = new TableLayout(this);
     
    //TableRow tablerow0 = new TableRow(this);
    TableRow tableRow1 = new TableRow(this);
    TableRow tableRow2 = new TableRow(this);
    TableRow tableRow3 = new TableRow(this);
    TableRow tableRow4 = new TableRow(this);
    //LinearLayout linear0 = new LinearLayout(this);
    LinearLayout linear1 = new LinearLayout(this);
    LinearLayout linear2 = new LinearLayout(this);
    LinearLayout linear3 = new LinearLayout(this);
    LinearLayout linear4 = new LinearLayout(this);
    //linear0.setOrientation(LinearLayout.HORIZONTAL);
     
    linear1.setOrientation(LinearLayout.HORIZONTAL);
    linear2.setOrientation(LinearLayout.HORIZONTAL);
    linear3.setOrientation(LinearLayout.HORIZONTAL);
    linear4.setOrientation(LinearLayout.HORIZONTAL);
    Button button0 = new Button (this);
    Button button1 = new Button (this);
    Button button2 = new Button (this);
    Button button3 = new Button (this);
    Button button4 = new Button (this);
    Button button5 = new Button (this);
    Button button6 = new Button (this);
    Button button7 = new Button (this);
    Button button8 = new Button (this);
    Button button9 = new Button (this);
    Button button10 = new Button (this);
    Button button11= new Button (this);
    Button button12 = new Button (this);
    Button button13 = new Button (this);
    Button button14= new Button (this);
    Button button15= new Button (this);
     
     
     
    //button0.setBackgroundColor(Color.GRAY);
    // button1.setBackgroundColor(Color.GRAY);
    //button2.setBackgroundColor(Color.GRAY);
    //button3.setBackgroundColor(Color.GRAY);
    //button4.setBackgroundColor(Color.GRAY);
    //button5.setBackgroundColor(Color.GRAY);
    //button6.setBackgroundColor(Color.GRAY);
    //button7.setBackgroundColor(Color.GRAY);
    //button8.setBackgroundColor(Color.GRAY);
    // button9.setBackgroundColor(Color.GRAY);
    //button10.setBackgroundColor(Color.GRAY);
    // button11.setBackgroundColor(Color.GRAY);
    //button12.setBackgroundColor(Color.GRAY);
    //button13.setBackgroundColor(Color.GRAY);
    // button14.setBackgroundColor(Color.GRAY);
    //button15.setBackgroundColor(Color.GRAY);
     
     
     
     
    //set more button id's with respect to position
    //this way it will be easier to reset button color
    button0.setId(0);
    button1.setId(1);
    button2.setId(2);
    button3.setId(3);
    button4.setId(4);
    button5.setId(5);
    button6.setId(6);
    button7.setId(7);
    button8.setId(8);
    button9.setId(9);
    button10.setId(10);
    button11.setId(11);
    button12.setId(12);
    button13.setId(13);
    button14.setId(14);
    button15.setId(15);
     
    //OnClickListener buttonListener1;
    button0.setOnClickListener(buttonListener);
    button1.setOnClickListener(buttonListener);
    button2.setOnClickListener(buttonListener);
    button3.setOnClickListener(buttonListener);
    button4.setOnClickListener(buttonListener);
    button5.setOnClickListener(buttonListener);
    button6.setOnClickListener(buttonListener);
    button7.setOnClickListener(buttonListener);
    button8.setOnClickListener(buttonListener);
    button9.setOnClickListener(buttonListener);
    button10.setOnClickListener(buttonListener);
    button11.setOnClickListener(buttonListener);
    button12.setOnClickListener(buttonListener);
    button13.setOnClickListener(buttonListener);
    button14.setOnClickListener(buttonListener);
    button15.setOnClickListener(buttonListener);
     
     
    //tablerow0.addView(linear0);
    tableRow1.addView(linear1);
    tableRow1.addView(linear2);
    tableRow1.addView(linear3);
    tableRow1.addView(linear4);
     
    //linear0.addView(null);
    linear1.addView(button0);
    linear2.addView(button1);
    linear3.addView(button2);
    linear4.addView(button3);
     
    tableRow2.addView(button4);
    tableRow2.addView(button5);
    tableRow2.addView(button6);
    tableRow2.addView(button7);
    tableRow3.addView(button8);
    tableRow3.addView(button9);
    tableRow3.addView(button10);
    tableRow3.addView(button11);
    tableRow4.addView(button12);
    tableRow4.addView(button13);
    tableRow4.addView(button14);
    tableRow4.addView(button15);
     
    //tableLayout.addview(tableRow0);
    tableLayout.addView(tableRow1);
    tableLayout.addView(tableRow2);
    tableLayout.addView(tableRow3);
    tableLayout.addView(tableRow4);
     
    //sets the game board
    int count = 0;
    for (int i = 0; i<= 15; i++)
     
    {
    Cards[i] = new MemoryCards(count);
    count++;
     
    }
     
     
     
    setContentView(tableLayout);
     
    }
     
     
     
    public boolean matchCheck()
    {
     
    if (player.firstCard() == 0 && player.secondCard()==10 || player.firstCard()==10 && player.secondCard()==0)
    {
    return true;
    }
    else if (player.firstCard() == 1 && player.secondCard()==14)
    {
    return true;
    }
    else if (player.firstCard() == 2 && player.secondCard()==7)
     
    {
    return true;
    }
    else if (player.firstCard() == 3 && player.secondCard()==4)
    {
    return true;
    }
    else if (player.firstCard() == 5 && player.secondCard()==9)
    {
    return true;
    }
    else if (User.firstCard == 6 && User.secondCard==13)
    {
    return true;
    }
    else if (User.firstCard == 8 && User.secondCard==15)
    {
    return true;
    }
    else if (User.firstCard == 11 && User.secondCard==12)
    {
    return true;
    }
     
    else
    {
    return false;
    }
     
     
     
    }
     
     
     
     
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.memory__matcher, menu);
    return true;
    }
    }

    other minor classes in attachments. Thanks!
    Attached Files Attached Files


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Andorid Memory Matching game help

    Please help with Android.

    Duplicate post.
    Improving the world one idiot at a time!

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Andorid Memory Matching game help

    "it displays color" --> it what?
    "it works somewhat" --> what works, what does not?
    "it will not display" it what?

    Without trying to read all of the code and figure out what you are doing, there is little advice I have.
    Write a SSCCE and ask specific questions about the problems you are having.

    Please post any related documents on the forum, not in attachments

  4. #4
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 5 Times in 1 Post

    Default Re: Andorid Memory Matching game help

    Okay. I have been coding for sometime now and have figured some things out. Here is an additional question, and I'll try to ask correctly. In my checkMatch boolean method, in my else block, I would like to have the runtime stop for 1-2 seconds before it excutes the next line. Is this possible? I'll post the block here and comment where I would like the code to stop/slowdown/sleep. I have messed around with thread.sleep, but I'm quite sure I understand.

     
                                                            if(Cards[i].cardStatus() == 1)
    							{
    								Button button1 = (Button) findViewById(player.firstCard());
    								Button button2 = (Button) findViewById(player.secondCard());
     
     
    								button1.setBackgroundColor(Color.WHITE);
    								button2.setBackgroundColor(Color.WHITE);
     
     
     
    								//Thread.sleep(time); DESIRED SLEEP/STOP POINTS
     
     
    								button1.setBackgroundColor(Color.GRAY);
    								button2.setBackgroundColor(Color.GRAY);
     
     
     
     
    								Cards[i].setStatus(0);
     
    							}

    Thanks guy, I hope I make more sense this time.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Andorid Memory Matching game help

    Yes it is possible. What happens when you uncomment the Thread.sleep in the code as it is?

  6. The Following User Says Thank You to jps For This Useful Post:

    iTry (May 9th, 2013)

  7. #6
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    1
    Thanked 5 Times in 1 Post

    Default Re: Andorid Memory Matching game help

    Get an error (time). edit options> creat local variable. That puts time long; over it. then it has an error for thread.sleep - edit option - surround with try catch. Is this how it works? I guess I'm just unfamiliar of how it would work. Thanks !

    additional questions - anyone way I can apply a border to buttons and or edit size of them?

    Thanks!

    --- Update ---

    I have the thread.sleep working with try catch statement. Now it's just where to correctly put it.

  8. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Andorid Memory Matching game help

    That is a very good question indeed.
    Where have you tried it? What were the results?
    If you do not understand why it is working the way it is, post the code with your question.

    I am guessing you are trying to get the buttons to change color for a period of time, and then change to a different (possibly previous) color.
    Read this page and ask any questions you have
    Watch for information about borders and backgrounds etc, this functionality is built into the buttons already for you to use

Similar Threads

  1. Need help finishing on Matching Game Memory Matching Game
    By Hernanhe1 in forum Member Introductions
    Replies: 1
    Last Post: April 25th, 2013, 05:29 PM
  2. Matching Game
    By Hernanhe1 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 25th, 2013, 04:52 PM
  3. Please Help Finish Project Memory Matching Game
    By Hernanhe1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2013, 01:13 PM
  4. Matching Game Help
    By nubshat in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 2nd, 2013, 03:15 PM

Tags for this Thread