Question: public class MainActivity extends AppCompatActivity { 5 usages private MemoryGame mGame; 1 0 usages private GridLayout mMemoryGrid; 6 usages private int mTurnNumber; 5 usages private

public class MainActivity extends AppCompatActivity {
5 usages
private MemoryGame mGame;
10 usages
private GridLayout mMemoryGrid;
6 usages
private int mTurnNumber;
5 usages
private int mFirstBtnNdx;
@override
protected void onCreate(Bundle savedInstancestate){
super.oncreate(savedinstancestate);
setContentView(R.layout.activity_main);
mMemoryGrid = findViewById(R.id.memory_grid);
// Add the same click handler to all grid buttons
for (int buttonIndex =0; buttonIndex < mMemoryGrid.getChildCount (); buttonIndex ++)
{
Button gridButton =(Button) mMemoryGrid.getchildAt(buttonIndex);
gridButton.setonclickListener(this: : onTileButtonclick);
}
mGame = new MemoryGame();
startGame();
}
2 usages
private void startGame(){
mGame. newGame ();
mTurnNumber =0;
mFirstBtnNdx =-1;
for (int buttonIndex =0; buttonIndex < mMemoryGrid.getChildCount(); buttonIndex++)
{
Button gridButton Button) mMemoryGrid.getChildAt(buttonIndex
private void onTileButtonclick(View view){
// Try to prevent fast-clicking tiles creating errors
if (mTurnNumber =2)
{
return;
}
// Find the button's row and col
int buttonIndex = mMemoryGrid.indexofChild(view);
// show the color for this tile
Button gridButton =(Button) mMemoryGrid.getChildAt(buttonIndex);
gridButton.setBackgroundColor(ContextCompat.getColor( context: this, mGame.getTileColor(buttonIndex)));
if (mTurnNumber =0)
{
mTurnNumber =1;
mFirstBtnNdx = buttonIndex;
// Disable the first grid button so we can't click it a second time
gridButton.setEnabled(false);
}
else
{
mTurnNumber =2;
// Is it a match??
if(mGame.getTileColor(mFirstBtnNdx)== mGame.getTileColor(buttonIndex))
{
// Yes, disable the buttons and leave the colors displayed
Button firstButton Button) mMemoryGrid.getChildAt (mFirstBtnNdx);
firstButton.setEnabled(false);
gridButton.setEnabled(false);
Toast.makeText( context: this, "It's a match!", Toast.LENGTH_SHORT).show();
// Check if game is over...
checkForGameover();
}
else
{
// No, delay 750 then hide the tiles
new CountDownTimer ( millisinFuture: 750, countDowninterval: 750){
5 usages
public void onTick(long millisUntilFinished){
}
public void onFinish(){
Button firstButton =(Button) mMemoryGrid.getChildAt(mFirstBtnNdx);
hideTiles(firstButton, gridButton);
}
}.start();
}
1 usage
public void hideTiles(Button btn1, Button btn2)
{
btn1.setBackgroundColor (ContextCompat.getColor (context: this, R.color.darkGray));
btn2.setBackgroundColor(ContextCompat.getColor( context: this, R.color.darkGray));
btn1. setEnabled(true);
btn2. setEnabled(true);
}
1 usage
public void onNewGameClick(View view) startGame(); }
1 usage
public void checkForGameover ()
{
boolean gameover = true;
for (int buttonIndex =0; buttonIndex < mMemoryGrid.getchildCount (); buttonIndex++
{
// TODO #3- Get a reference to each grid button and call the isEnabled method
// to determine if any grid buttons are enabled. If the enabled property is true
// the gameover variable should be set to false
}
// Congratulate the user if the game is over
if (gameover){
Toast.makeText( context: this, "Congratulations!", Toast.LENGTH_LONG).show();
}
}
Add an action item to the app bar that allows you to choose from 3 options for the countdown timer. Create a member field in MainActivity class that will allow you to save and use the timer value from the app bar. Valus like 500ms,1000ms, and 2000ms are viable options.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!