Answered step by step
Verified Expert Solution
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
usages
private MemoryGame mGame;
usages
private GridLayout mMemoryGrid;
usages
private int mTurnNumber;
usages
private int mFirstBtnNdx;
@override
protected void onCreateBundle savedInstancestate
super.oncreatesavedinstancestate;
setContentViewRlayout.activitymain;
mMemoryGrid findViewByIdRidmemorygrid;
Add the same click handler to all grid buttons
for int buttonIndex ; buttonIndex mMemoryGrid.getChildCount ; buttonIndex
Button gridButton Button mMemoryGrid.getchildAtbuttonIndex;
gridButton.setonclickListenerthis: : onTileButtonclick;
mGame new MemoryGame;
startGame;
usages
private void startGame
mGame. newGame ;
mTurnNumber ;
mFirstBtnNdx ;
for int buttonIndex ; buttonIndex mMemoryGrid.getChildCount; buttonIndex
Button gridButton Button mMemoryGrid.getChildAtbuttonIndex
private void onTileButtonclickView view
Try to prevent fastclicking tiles creating errors
if mTurnNumber
return;
Find the button's row and col
int buttonIndex mMemoryGrid.indexofChildview;
show the color for this tile
Button gridButton Button mMemoryGrid.getChildAtbuttonIndex;
gridButton.setBackgroundColorContextCompatgetColor context: this, mGame.getTileColorbuttonIndex;
if mTurnNumber
mTurnNumber ;
mFirstBtnNdx buttonIndex;
Disable the first grid button so we can't click it a second time
gridButton.setEnabledfalse;
else
mTurnNumber ;
Is it a match??
ifmGamegetTileColormFirstBtnNdx mGame.getTileColorbuttonIndex
Yes, disable the buttons and leave the colors displayed
Button firstButton Button mMemoryGrid.getChildAt mFirstBtnNdx;
firstButton.setEnabledfalse;
gridButton.setEnabledfalse;
Toast.makeText context: this, "It's a match!", Toast.LENGTHSHORTshow;
Check if game is over...
checkForGameover;
else
No delay then hide the tiles
new CountDownTimer millisinFuture: countDowninterval:
usages
public void onTicklong millisUntilFinished
public void onFinish
Button firstButton Button mMemoryGrid.getChildAtmFirstBtnNdx;
hideTilesfirstButton gridButton;
start;
usage
public void hideTilesButton btn Button btn
btnsetBackgroundColor ContextCompatgetColor context: this, Rcolor.darkGray;
btnsetBackgroundColorContextCompatgetColor context: this, Rcolor.darkGray;
btn setEnabledtrue;
btn setEnabledtrue;
usage
public void onNewGameClickView view startGame;
usage
public void checkForGameover
boolean gameover true;
for int buttonIndex ; buttonIndex mMemoryGrid.getchildCount ; buttonIndex
TODO # 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.LENGTHLONGshow;
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started