Android用メモリーゲームを作りました。
私の問題は、すべてのコードが 1 つのクラスにあることです。したがって、コードをより明確にするために、新しいクラスでコードを分離する必要があります。
これはコードです:
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();
}
}