dimanche 26 avril 2015

Trouble figuring out how to generate set values only twice for a set amount of times

I will start off with this is my first post on StackOverflow, so I'm sorry if i leave anything important out, i am also very new at coding and still learning.

What i am trying to is make a form of a memory game, a user will input a number, it will generate that number of tiles (even only) and give them set values. I am stuck on the giving them 2 set values each and only two for x number of tiles.

An example would be if the user choose the lowest number of tiles, 4, there would be two with A and two with B. The code i have right now is a mess but kind of a draft and it is setting text on a button, not a value. This code is also only for the 4 tiles example.

On a side note, I'm sure there is a simpler way to declare all 20 of my buttons, maybe someone could point me in the right direction.

    @Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.second_layout);

    Button btn1 = (Button)findViewById(R.id.btn1);
    Button btn2 = (Button)findViewById(R.id.btn2);
    Button btn3 = (Button)findViewById(R.id.btn3);
    Button btn4 = (Button)findViewById(R.id.btn4);
    Button btn5 = (Button)findViewById(R.id.btn5);
    Button btn6 = (Button)findViewById(R.id.btn6);
    Button btn7 = (Button)findViewById(R.id.btn7);
    Button btn8 = (Button)findViewById(R.id.btn8);
    Button btn9 = (Button)findViewById(R.id.btn9);
    Button btn10 = (Button)findViewById(R.id.btn10);
    Button btn11 = (Button)findViewById(R.id.btn11);
    Button btn12 = (Button)findViewById(R.id.btn12);
    Button btn13 = (Button)findViewById(R.id.btn13);
    Button btn14 = (Button)findViewById(R.id.btn14);
    Button btn15 = (Button)findViewById(R.id.btn15);
    Button btn16 = (Button)findViewById(R.id.btn16);
    Button btn17 = (Button)findViewById(R.id.btn17);
    Button btn18 = (Button)findViewById(R.id.btn18);
    Button btn19 = (Button)findViewById(R.id.btn19);
    Button btn20 = (Button)findViewById(R.id.btn20);



int tiles = 0;        
super.onCreate(savedInstanceState);
    TextView tv = (TextView)findViewById(R.id.calling_activity_info_text_view);

    String tileNum = getIntent().getExtras().getString("tilesNumber");

    String[] tileSet = {"A","B","C","D","E","F","G","H","I","J"};
    Button[] btnA = {btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13
            , btn14, btn15, btn16, btn17, btn18, btn19, btn20};

    if(tileNum.equals("4")) {
        tiles = Integer.parseInt(tileNum);
        tv.setText("" + tiles);
        for(int i = 0; i <= 3; i++){
            btnA[i].setVisibility(View.VISIBLE);
            btnA[i].setText(tileSet[i]);
        }




Aucun commentaire:

Enregistrer un commentaire