Android 我做了一个记忆游戏应用程序,想添加一个计时器,当所有的卡都匹配时停止。这里';这是我的密码:-
我制作了一个记忆游戏应用程序,我想在其中添加一个计时器,当活动开始时开始,当所有卡匹配时停止,我应该如何检查所有卡是否匹配?这是我的密码:Android 我做了一个记忆游戏应用程序,想添加一个计时器,当所有的卡都匹配时停止。这里';这是我的密码:-,android,Android,我制作了一个记忆游戏应用程序,我想在其中添加一个计时器,当活动开始时开始,当所有卡匹配时停止,我应该如何检查所有卡是否匹配?这是我的密码: public class game4x4new extends AppCompatActivity implements View.OnClickListener { TextView moves, score, time; Button reset, submit, start; private int numberOfElements; privat
public class game4x4new extends AppCompatActivity implements View.OnClickListener {
TextView moves, score, time;
Button reset, submit, start;
private int numberOfElements;
private MemoryButton[] buttons;
private int[] buttonGraphicLocations;
private int[] buttonGraphics;
private MemoryButton selectedButton1;
private MemoryButton selectedButton2;
int flippedtimes = 0;
int match = 0;
int seconds = 0;
AlertDialog.Builder adb;
AlertDialog a;
private boolean isBusy = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game4x4new);
GridLayout gridlayout = (GridLayout) findViewById(R.id.grid_layout_4x4);
moves = (TextView) findViewById(R.id.tv1);
score = (TextView) findViewById(R.id.tv2);
time = (TextView) findViewById(R.id.tv3);
reset = (Button) findViewById(R.id.button3);
submit = (Button) findViewById(R.id.button4);
start = (Button) findViewById(R.id.button5);
int numColumns = gridlayout.getColumnCount();
int numRows = gridlayout.getRowCount();
numberOfElements = numColumns * numRows;
buttons = new MemoryButton[numberOfElements];
buttonGraphics = new int[numberOfElements / 2];
buttonGraphics[0] = R.drawable.blue;
buttonGraphics[1] = R.drawable.cyan;
buttonGraphics[2] = R.drawable.green;
buttonGraphics[3] = R.drawable.grey;
buttonGraphics[4] = R.drawable.maroon;
buttonGraphics[5] = R.drawable.pink;
buttonGraphics[6] = R.drawable.red;
buttonGraphics[7] = R.drawable.yellow;
buttonGraphicLocations = new int[numberOfElements];
shuffleButtonGraphics();
for (int r = 0; r < numRows; r++) {
for (int c = 0; c < numColumns; c++) {
MemoryButton tempButton = new MemoryButton(this, r, c, buttonGraphics[buttonGraphicLocations[r * numColumns + c]]);
tempButton.setId(View.generateViewId());
tempButton.setOnClickListener(this);
buttons[r * numColumns + c] = tempButton;
gridlayout.addView(tempButton);
}
}
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shuffleButtonGraphics();
match = 0;
flippedtimes = 0;
seconds = 0;
score.setText("SCORE :");
moves.setText("MOVES :");
time.setText("TIME :");
}
});
}
protected void shuffleButtonGraphics()
{
Random rand= new Random();
for(int i=0; i<numberOfElements; i++)
{
buttonGraphicLocations[i]= i % (numberOfElements / 2);
}
for(int i=0; i<numberOfElements;i++)
{
int temp= buttonGraphicLocations[i];
int swapIndex = rand.nextInt(16);
buttonGraphicLocations[i]=buttonGraphicLocations[swapIndex];
buttonGraphicLocations[swapIndex]=temp;
}
}
@Override
public void onClick(View v)
{
if(isBusy)
return;
MemoryButton button= (MemoryButton) v;
if(selectedButton1==null)
{
selectedButton1=button;
selectedButton1.flipped();
return;
}
if(selectedButton1.getId()==button.getId())
{
return;
}
if (selectedButton1.getFrontDrawableId()==button.getFrontDrawableId())
{
button.flipped();
button.setMatched(true);
selectedButton1.setMatched(true);
selectedButton1.setEnabled(false);
button.setEnabled(false);
selectedButton1=null;
flippedtimes++;
match++;
moves.setText("MOVES : " + flippedtimes);
score.setText("SCORE : " + match);
return;
}
else
{
selectedButton2=button;
selectedButton2.flipped();
isBusy=true;
flippedtimes++;
moves.setText("MOVES : " + flippedtimes);
final Handler handler= new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
selectedButton2.flipped();
selectedButton1.flipped();
selectedButton1=null;
selectedButton2=null;
isBusy=false;
}
}, 1000);
}
}
公共类game4x4new扩展AppCompatActivity实现View.OnClickListener{
TextView移动、分数、时间;
按钮复位、提交、启动;
私有整数元素;
私有内存按钮[]按钮;
私有int[]按钮位置;
私人int[]按钮图;
私有存储器按钮selectedButton1;
私有存储器按钮selectedButton2;
int flipedtimes=0;
int匹配=0;
整数秒=0;
AlertDialog.Builder adb;
Alerta;
私有布尔值isBusy=false;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game4x4new);
GridLayout GridLayout=(GridLayout)findViewById(R.id.grid\u layout\u 4x4);
moves=(TextView)findViewById(R.id.tv1);
分数=(TextView)findViewById(R.id.tv2);
时间=(TextView)findViewById(R.id.tv3);
重置=(按钮)findViewById(R.id.button3);
提交=(按钮)findViewById(R.id.button4);
开始=(按钮)findViewById(R.id.button5);
int numColumns=gridlayout.getColumnCount();
int numRows=gridlayout.getRowCount();
numberOfElements=numColumns*numRows;
按钮=新存储器按钮[numberOfElements];
ButtongGraphics=新整数[numberOfElements/2];
按钮图形[0]=R.drawable.blue;
按钮图形[1]=R.drawable.cyan;
按钮图形[2]=R.drawable.green;
钮扣图案[3]=R.可绘制灰色;
钮扣图案[4]=R.可拉深的褐红色;
钮扣图案[5]=R.可绘制的粉红色;
按钮图形[6]=R.可绘制红色;
按钮图形[7]=R.可绘制.黄色;
buttonGraphicLocations=新整数[numberOfElements];
shuffleButtonGraphics();
对于(int r=0;r 对于(int i=0;i在游戏开始时启动计时器。在显示两张牌后,检查它们是否匹配。如果匹配,检查是否还有任何不匹配的牌。如果是,请不要停止计时器。如果不是,请停止计时器。可能说明投反对票的原因?不要继续将问题标记为“否决票”,这绝不会提高问题的质量或降低投票率鼓励Askes进行富有成效的讨论。请考虑做一个。更好地解释你的问题。现在我必须阅读你的代码来理解你所说的“匹配”的意思。另外,你的代码没有被正确地格式化。上面的代码不包含在代码块中。请把所有的东西移到四个空格中。这很容易。在任何IDE中,例如选择所有内容并点击tab。