0

I made a Memory Game for Android.

My problem is that all the code is in one class. So I need to separate the code with a new class to help make it more clear.

This is the code:

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();

}

}
4

1 回答 1

2

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

此外,您可能希望使用视图可以具有的属性,而不是制作NivelAimplement 。如果您为视图分配属性OnClickListenerandroid:onClick

android:onClick="aClickMethod"

那么你只需要实现一个公共方法:

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

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

于 2012-07-02T03:59:06.490 回答