Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications Dexa 2021 Workshops Biokdd Iwcfs Mlkgraphs Al Cares Protime Alsys 2021 Virtual Event September 27 30 2021 Proceedings

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Atif Mashkoor ,Johannes Sametinger ,Anna Fensel ,Jorge Martinez-Gil ,Lukas Fischer

1st Edition

3030871002, 978-3030871000

More Books

Students also viewed these Databases questions

Question

14.6 Spearman Rank Correlation

Answered: 1 week ago