如何更清晰地分离此代码?

时间:2012-07-02 03:51:45

标签: android

我为Android制作了记忆游戏。

我的问题是所有代码都在一个类中。因此,我需要将代码与新类分开,以帮助更清晰。

这是代码:

public class NivelA extends Activity implements OnClickListener{

private int [] fichasIds = {
        R.drawable.img_1,
        R.drawable.img_2,
        R.drawable.img_3,
        R.drawable.img_4,
        R.drawable.img_5,
        R.drawable.img_6,
        R.drawable.imgOculto,   
};

protected int[] fichas;

private ImageView[] imageviews;

private static final int cantFichas = 6;

private int fichasUp;

private int currentIndex = -1;

private int lastIndex = -1;

private int fichasIguales = 0;

private int vidas = 3;
private TextView vidas_txt;

Handler handler;

private int [] imgIds = {
        R.id.img_1,
        R.id.img_2,
        R.id.img_3,
        R.id.img_4,
        R.id.img_5,
        R.id.img_6,
        R.id.img_7,
        R.id.img_8,
        R.id.img_9,
        R.id.img_10,
        R.id.img_11,
        R.id.img_12,
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nivel1);

    //Toast
    Toast toast = Toast.makeText(NivelA.this, "Nivel: 1", Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER|Gravity.CENTER, 0, 65);
    toast.show();

    handler = new Handler ();

    fichasIguales = 0;

    vidas_txt = (TextView) findViewById(R.id.vidas_iguales);

    fichas = new int [12];

    for (int i=0; i<12; i++)
    {
        fichas[i] = -1;
    }

    imageviews = new ImageView [imgIds.length];
    for (int i=0; i<imgIds.length; i++)
    {

        imageviews[i] = (ImageView) findViewById(imgIds[i]);
    }

    Random random = new Random ();

    for (int i=0; i<6; i++){
        for (int z=0; z<2; z++)
        {
            int fichasRandom = random.nextInt(12);
            while (fichas[fichasRandom]!= -1)
            {
                fichasRandom = random.nextInt(12);
            }
            fichas[fichasRandom]= i;
        }
    }

    //Los listener
    for (int i=0; i<12; i++)
    {
        ((ImageView)findViewById(imgIds[i])).setOnClickListener(this);
    }

    for (int i=0; i<12; i++)
    {
        ((ImageView)findViewById(imgIds[i])).setImageResource(R.drawable.imgOculto);
    }
}
@Override
public void onClick(View v) {
    int index = Integer.parseInt((String)v.getTag());

    for (int i=0; i<12; i++)
    {
        if (v.getId() == imgIds [i])
        {
            index = i;
            ((ImageView)findViewById(imgIds[i])).setImageResource(fichasIds[fichas[i]]);

            imageviews[i].setFocusable(false);
            imageviews[i].setClickable(false);
            break;
        }
    }
    fichasUp++;

    if (fichasUp == 2)
    {
        vidas--;
        vidas_txt.setText(String.valueOf(vidas));

        currentIndex = index;

        for (ImageView view:imageviews)
        {
            view.setFocusable(false);
            view.setClickable(false);
        }

        fichasUp = 0;
        handler.postDelayed(fichasOcultas, 1000);

    }
    else{
        lastIndex = index;
    }

}

Runnable fichasOcultas = new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub

        if (fichas[currentIndex]== fichas[lastIndex])
        {
            ((ImageView)findViewById(imgIds[lastIndex])).setVisibility(View.INVISIBLE);
            ((ImageView)findViewById(imgIds[currentIndex])).setVisibility(View.INVISIBLE);

            fichasIguales ++;


            //Toast [ Hay Coincidencia ]
            Toast toast = Toast.makeText(NivelA.this, "Hay Coincidencia!", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER|Gravity.CENTER, 14, 0);
            toast.show();

            if (fichasIguales == cantFichas)
            {
                win ();
            }
        }else
        {
            ((ImageView)findViewById(imgIds[currentIndex])).setImageResource(R.drawable.imgOculto);
            ((ImageView)findViewById(imgIds[lastIndex])).setImageResource(R.drawable.imgOculto);
        }


        for (ImageView view:imageviews)
        {
            view.setFocusable(true);
            view.setClickable(true);
        }
    }

};
protected void win() {
    Toast toast = Toast.makeText(NivelA.this, "Ganaste!!", Toast.LENGTH_SHORT);
    toast.show();

}

}

1 个答案:

答案 0 :(得分:2)

我建议您将所有游戏逻辑移出NivelA课程。这应该对简化代码有很大帮助。一般来说,每个Activity子类应该负责创建屏幕,可能响应事件,而不是其他。

此外,您可能希望使用视图可以拥有的NivelA属性,而不是使OnClickListener实现android:onClick。如果为视图指定属性

android:onClick="aClickMethod"

然后你只需要实现一个公共方法:

public void aClickMethod(View view) { . . . }

(方法的名称完全取决于您。)自动分配android:onClick方法使视图可以点击。你会惊讶地发现用这个技巧可以消除多少代码。