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();
}
}
Delete the help button from the Main Activity layout and add it to the app bar. The code for Help button will remain the same.

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 Systems For Advanced Applications 9th International Conference Dasfaa 2004 Jeju Island Korea March 2004 Proceedings Lncs 2973

Authors: YoonJoon Lee ,Jianzhong Li ,Kyu-Young Whang

2004th Edition

3540210474, 978-3540210474

More Books

Students also viewed these Databases questions

Question

How does international marketing differ from domestic marketing?

Answered: 1 week ago

Question

What are methods of sterilization?

Answered: 1 week ago

Question

AC114 Unit 10 Lab Assignment Question 2 of 7 Answered: 1 week ago

Answered: 1 week ago

Question

4. Identify cultural variations in communication style.

Answered: 1 week ago