我做了一个记忆游戏应用程序,其中我想添加一个计时器,当活动开始时启动,并在所有卡匹配时停止,我应该如何检查所有卡是否匹配?这是我的代码:
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);
}
}
答案 0 :(得分:0)
游戏开始时启动计时器。在显示两张牌后,检查它们是否匹配。如果有,请检查是否有任何不匹配的卡片。如果是,请不要停止计时器。如果不是,请停止计时器。