Results 1 to 2 of 2
  1. #1
    iTry is offline Member
    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Default Please help with Android.

    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.

    <code = Java>

    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;
    }
    }

    </end>

    [code = java]

    package com.example.memorymatching;

    public class matchChecker
    {
    public static boolean match;
    //public static class User
    {

    //int firstCard;
    // int secondCard;
    // boolean matchCheck;

    new User()
    {
    {
    firstCard = 0;
    secondCard = 0;
    }

    public int firstCard()
    {
    return firstCard;
    }

    public int secondCard()
    {
    return secondCard;
    }
    //public boolean getMatch()
    //{
    // return (match);
    // }


    };
    }
    }
    [/end]

    [code = java]

    package com.example.memorymatching;

    public class User {

    static int firstCard;
    static int secondCard;

    public User()
    {
    firstCard = -1;
    secondCard = -1;
    }

    public int firstCard()
    {
    return firstCard;
    }

    public int secondCard()
    {
    return secondCard;
    }
    public void setFirstCard(int i)
    {
    firstCard = i;
    }

    public void setSecondCard(int j)
    {
    secondCard = j;
    }


    }
    [/end]

    [code = java]

    package com.example.memorymatching;

    public class MemoryCards

    {
    int position;
    int status; //0 = open; 1 = picked; 2 = unavailable
    public MemoryCards(int pos)
    {
    //position = pos;
    status = 0;
    }

    public int cardStatus()
    {
    return status;
    }

    public void setStatus(int stat)
    {
    status = stat;
    }


    }
    [/end code ]

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

Similar Threads

  1. JNI on Android
    By moriya in forum Android
    Replies: 1
    Last Post: 05-31-2012, 04:34 PM
  2. Replies: 1
    Last Post: 12-24-2011, 04:41 AM
  3. Android Help
    By fresh83 in forum Android
    Replies: 1
    Last Post: 05-19-2011, 08:44 PM
  4. Android USB
    By Blackberrylerner in forum Android
    Replies: 0
    Last Post: 01-15-2011, 05:05 AM
  5. Android
    By sspkiet in forum Android
    Replies: 1
    Last Post: 09-03-2010, 01:14 AM

Tags for this Thread

Posting Permissions

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