Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat; import android.os . Bundle; import android.os . CountDownTimer; import android.view.View; import android.widget. * ; public class | MainActivity extends AppCompatActivity {
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.osBundle;
import android.osCountDownTimer;
import android.view.View;
import android.widget.;
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.onCreate savedinstancestate;
setContentViewRlayout.activitymain;
mMemoryGrid findViewByIdRidmemorygrid;
Add the same click handler to all grid buttons
for int buttonIndex ; buttonIndex mMemoryGrid.getchildCount;
Button gridButton Button mMemoryGrid.getchildAtbuttonIndex;
gridButton.setonclickListenerthis: :onTileButtonclick;
mGame new MemoryGame;
startGame;
private void startGame
mGame. newGame;
mTurnNumber ;
mFirstBtnNdx ;
for int buttonIndex ; buttonIndex mMemoryGrid.getChildCount; buttonIndex
Button gridButton Button mMemoryGrid.getchildAtbuttonIndex;
TODO # Call the setEnabled and setBackgroundColor methods on the gridButton
object to enable all grid buttons and set the background color to darkgray from color resources
usage
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.setBackgroundColorContextCompatgetColorcontext: 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;
mTurnNumber ;
public void hideTilesButton btn Button btn
btnsetBackgroundColor ContextCompatgetColor context: this, Rcolor.darkGray;
btnsetBackgroundColor ContextCompatgetColor context: this, Rcolor.darkGray;
btnsetEnabledtrue;
btnsetEnabledtrue;
usage
public void onNewGameClickView viewstartGame;
usage
public void checkForGameOver
boolean gameover true;
for int 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;
usages
public class MemoryGame
usages
private int mGameSize; How many tiles will be displayed
usages
private int mTilecolors; Array to hold the color assigned to each tile in the game
usage
public MemoryGame
Default game size
mGamesize ;
Create color array
mTilecolors new intmGameSize;
public void newGame
Random new Random;
Create an ArrayList and initialize with all possible values for the game tiles...
ArrayList
In MemoryGame.java, complete the definition for the getTileColor method so it will return the indicated element from the mTileColors array.
In MainActivity.java, add code to the startGame method to enable each gridButton and set the background color to darkGray from color resources Use the setEnabled and setBackgroundColor methods to set these attributes.
In MainActivity.java, complete the checkForGameOver method by adding code inside the for loop to check isEnabled for each grid button and set the gameOver to false if you find a button that is enabled.
Add a help activity that displays a simple help message. Make sure you include the code to save the context so you can return to the game in progress.
Create a Landscape Qualifier for activitymain.xml Edit the new land xml so the New Game and Help buttons are located to the right of the game grid.
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