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
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 new game button from the Main Activity layout and add it as an action item on the app bar. The code for New Game will remain the same.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
