2

こんにちは、私はプログラミングが初めてです。これにはおそらく簡単な答えがあることを知っていますが、そこに到達することはできません。

したがって、基本的には、2 つのボタンの背景画像が一致するかどうか、およびそれら (または他の機能) を無効にするかどうかを知りたいと考えています。ここに私のコードがあります。今のところ、Button1 と Button2 に焦点を当てています。

最後の方法を見てください。うまくいかないかどうかはわかっていますが、それが私がやろうとしていることです。ありがとう。

package com.example.pairsgame;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);}


    public final static int Button_Count = 12;

    private Button[] Buttons = new Button[Button_Count];
    {

        Buttons[0] = (Button) findViewById(R.id.Button1);
        Buttons[1] = (Button) findViewById(R.id.Button2);
        Buttons[2] = (Button) findViewById(R.id.Button3);
        Buttons[3] = (Button) findViewById(R.id.Button4);
        Buttons[4] = (Button) findViewById(R.id.Button5);
        Buttons[5] = (Button) findViewById(R.id.Button6);
        Buttons[6] = (Button) findViewById(R.id.Button7);
        Buttons[7] = (Button) findViewById(R.id.Button8);
        Buttons[8] = (Button) findViewById(R.id.Button9);
        Buttons[9] = (Button) findViewById(R.id.Button10);
        Buttons[10] = (Button) findViewById(R.id.Button11);
        Buttons[11] = (Button) findViewById(R.id.Button12);

        Buttons[0].setOnClickListener(this);
        Buttons[1].setOnClickListener(this);
        Buttons[2].setOnClickListener(this);
        Buttons[3].setOnClickListener(this);
        Buttons[4].setOnClickListener(this);
        Buttons[5].setOnClickListener(this);
        Buttons[6].setOnClickListener(this);
        Buttons[7].setOnClickListener(this);
        Buttons[8].setOnClickListener(this);
        Buttons[9].setOnClickListener(this);
        Buttons[10].setOnClickListener(this);
        Buttons[11].setOnClickListener(this);

    }

    public static int Background_Total = 8;

    public final static int[] Backgrounds = { R.drawable.ic_launcher };

    public final static int[] Game_Board = {

    0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };

    @Override
    public void onClick(View v) {

        for (final int but = 0; but < Button_Count; but++) {
            if (v == Buttons[but]) {
                new CountDownTimer(1000, 500) {

                    public void onTick(long millisUntilFinished) {
                        Buttons[but].setBackgroundResource(Backgrounds[Game_Board[but]]);
                    }

                    public void onFinish() {
                        Buttons[but].setBackgroundResource(android.R.drawable.btn_default);
                    }
                }.start();
                if (Game_Board[0] == Game_Board[2])
                    Buttons[0].setEnabled(false);
                Buttons[2].setEnabled(false);
            }
        }

    }
}
4

2 に答える 2

2

通常、データをその表現から分離する必要があります。これには多くの明白な利点があります。そのうちの 1 つは、データの視覚的表現を気にせずにデータを簡単にテストできることです。

まず最初に、次のようにボタンを配列に格納します。

public final static int BUTTON_COUNT = 12; 

private Button[] buttons = new Button[BUTTON_COUNT];

Button[0] = (Button)findViewById( R.id.Button1 );           // Button 1
Button[1] = (Button)findViewById( R.id.Button2 );           // Button 2
Button[2] = (Button)findViewById( R.id.Button3 );           // Button 3
Button[3] = (Button)findViewById( R.id.Button4 );           // Button 4
Button[4] = (Button)findViewById( R.id.Button5 );           // Button 5
Button[5] = (Button)findViewById( R.id.Button6 );           // Button 6
Button[6] = (Button)findViewById( R.id.Button7 );           // Button 7
Button[7] = (Button)findViewById( R.id.Button8 );           // Button 8
Button[8] = (Button)findViewById( R.id.Button9 );           // Button 9
Button[9] = (Button)findViewById( R.id.Button10 );          // Button 10
Button[10] = (Button)findViewById( R.id.Button11 );         // Button 11
Button[11] = (Button)findViewById( R.id.Button12 );         // Button 12

これにより、onClick() でわかるように、それらの使用方法が大幅に強化されます。

次に、必要な一意の背景の数と、それぞれを表す画像を定義する必要があります。

public final static int BACKGROUND_TOTAL = 8;

public final static int[] BACKGROUNDS = {
   R.drawable.ic_launcher,                           // Image 0
   R.drawable.ic_launcher2,                          // Image 1
   R.drawable.ic_launcher3,                          // Image 2
   R.drawable.ic_launcher4,                          // Image 3
   R.drawable.ic_launcher5,                          // Image 4
   R.drawable.ic_launcher6,                          // Image 5
   R.drawable.ic_launcher7,                          // Image 6
   R.drawable.ic_launcher8                           // Image 7
};

また、「ゲーム ボード」がどのように見えるかを定義する必要があります。これが行うことは、画像を各ボタンに「関連付ける」ことです。これは無数の方法で実行できますが、例として固定配列を使用してみましょう。これらの各エントリは、ボタンの 1 つと一致することに注意してください。

 public final static int[] GAME_BOARD = {
    0,                                         // Button 1
    0,                                         // Button 2
    1,                                         // Button 3
    1,                                         // Button 4        
    2,                                         // Button 5
    2,                                         // Button 6        
    3,                                         // Button 7
    3,                                         // Button 8        
    4,                                         // Button 9
    4,                                         // Button 10       
    5,                                         // Button 11
    5,                                         // Button 12       
 };

ここでの各値は、背景配列の「インデックス」です。したがって、すべての値は 0 から (BACKGROUND_TOTAL-1) の間でなければなりません。シーケンシャル ペアを追加しただけですが、実際のゲームでは、ここでランダムな値を生成する必要があることに注意してください。上記の例では、ボタン 3 はイメージ 1 を使用し、ボタン 9 はイメージ 4 を使用します。

この情報がすべて揃っていると、onClick は次のようになります。

@Override
public void onClick(View v) {    

   if ( v == buttons[0] )  {
      new CountDownTimer(1000, 500) {
         public void onTick(long millisUntilFinished) {
             buttons[0].setBackgroundResource( BACKGROUNDS[GAME_BOARD[0]] );
         }

         public void onFinish() {
             buttons[0].setBackgroundResource( android.R.drawable.btn_default );
         }
      }.start();       
   }
   if ( v == buttons[1] )  {
      new CountDownTimer(1000, 500) {
         public void onTick(long millisUntilFinished) {
             buttons[1].setBackgroundResource( BACKGROUNDS[GAME_BOARD[1]] );
         }

         public void onFinish() {
             buttons[1].setBackgroundResource( android.R.drawable.btn_default );
         }
      }.start();       
   }
   if ( v == buttons[2] )  {
      new CountDownTimer(1000, 500) {
         public void onTick(long millisUntilFinished) {
             buttons[2].setBackgroundResource( BACKGROUNDS[GAME_BOARD[2]] );
         }

         public void onFinish() {
             buttons[2].setBackgroundResource( android.R.drawable.btn_default );
         }
      }.start();       
   }
   if ( v == buttons[3] )  {
      new CountDownTimer(1000, 500) {
         public void onTick(long millisUntilFinished) {
             buttons[3].setBackgroundResource( BACKGROUNDS[GAME_BOARD[0]] );
         }

         public void onFinish() {
             buttons[3].setBackgroundResource( android.R.drawable.btn_default );
         }
      }.start();       
   }
   if ( v == buttons[4] )  {
      new CountDownTimer(1000, 500) {
         public void onTick(long millisUntilFinished) {
             buttons[4].setBackgroundResource( BACKGROUNDS[GAME_BOARD[4]] );
         }

         public void onFinish() {
             buttons[4].setBackgroundResource( android.R.drawable.btn_default );
         }
      }.start();       
   }
   if ( v == buttons[5] )  {
      new CountDownTimer(1000, 500) {
         public void onTick(long millisUntilFinished) {
             buttons[5].setBackgroundResource( BACKGROUNDS[GAME_BOARD[5]] );
         }

         public void onFinish() {
             buttons[5].setBackgroundResource( android.R.drawable.btn_default );
         }
      }.start();       
   }
   if ( v == buttons[6] )  {
      new CountDownTimer(1000, 500) {
         public void onTick(long millisUntilFinished) {
             buttons[6].setBackgroundResource( BACKGROUNDS[GAME_BOARD[6]] );
         }

         public void onFinish() {
             buttons[6].setBackgroundResource( android.R.drawable.btn_default );
         }
      }.start();       
   }
   if ( v == buttons[7] )  {
      new CountDownTimer(1000, 500) {
         public void onTick(long millisUntilFinished) {
             buttons[7].setBackgroundResource( BACKGROUNDS[GAME_BOARD[7]] );
         }

         public void onFinish() {
             buttons[7].setBackgroundResource( android.R.drawable.btn_default );
         }
      }.start();       
   }
   if ( v == buttons[8] )  {
      new CountDownTimer(1000, 500) {
         public void onTick(long millisUntilFinished) {
             buttons[8].setBackgroundResource( BACKGROUNDS[GAME_BOARD[8]] );
         }

         public void onFinish() {
             buttons[8].setBackgroundResource( android.R.drawable.btn_default );
         }
      }.start();       
   }
   if ( v == buttons[9] )  {
      new CountDownTimer(1000, 500) {
         public void onTick(long millisUntilFinished) {
             buttons[9].setBackgroundResource( BACKGROUNDS[GAME_BOARD[9]] );
         }

         public void onFinish() {
             buttons[9].setBackgroundResource( android.R.drawable.btn_default );
         }
      }.start();       
   }
   if ( v == buttons[10] )  {
      new CountDownTimer(1000, 500) {
         public void onTick(long millisUntilFinished) {
             buttons[10].setBackgroundResource( BACKGROUNDS[GAME_BOARD[10]] );
         }

         public void onFinish() {
             buttons[10].setBackgroundResource( android.R.drawable.btn_default );
         }
      }.start();       
   }
   if ( v == buttons[11] )  {
      new CountDownTimer(1000, 500) {
         public void onTick(long millisUntilFinished) {
             buttons[11].setBackgroundResource( BACKGROUNDS[GAME_BOARD[11]] );
         }

         public void onFinish() {
             buttons[11].setBackgroundResource( android.R.drawable.btn_default );
         }
      }.start();       
   }
   if ( v == buttons[12] )  {
      new CountDownTimer(1000, 500) {
         public void onTick(long millisUntilFinished) {
             buttons[12].setBackgroundResource( BACKGROUNDS[GAME_BOARD[12]] );
         }

         public void onFinish() {
             buttons[12].setBackgroundResource( android.R.drawable.btn_default );
         }
      }.start();       
   }
}

最後に、画像を一致させる代わりに、次のようにして、2 つのボタンの背景が同じかどうかをテストできます。

if ( GAME_BOARD[button1] == GAME_BOARD[button2] )
   // disable both buttons

この場合、button1 と button2 は、配列内のボタンのインデックス値である必要があります。

これは大まかな例ですが、別のより適切な方法で問題について考える道を歩むことができるはずです。頑張って!

于 2013-05-30T09:49:07.087 に答える
0

ロジックはおそらく、UI 要素であるボタンの背景画像に依存するべきではなく、ゲームのロジックから分離する必要があります (関心の分離)。

私がすることは、Applicationどの画像が各ボタンの背景として設定されているかを「知っている」構造をメモリのどこかに(多分?)保持し、それを使用してリンクされたボタンの無効化を比較および駆動することです:比較しようとするのではなく2 つの UI 要素の場合、2 つの画像オブジェクトを比較するために記述したメソッドを含む通常のクラスを作成し (ある種の ID から)、それに応じてボタンを設定することができます。

他の利点 (関心の分離、より明確な設計など) の中でも、ロジックがテスト可能になります。

于 2013-05-29T15:19:34.463 に答える