Answered step by step
Verified Expert Solution
Link Copied!

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.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.*;
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(); ))
{
Button gridButton =(Button) mMemoryGrid.getchildAt(buttonIndex);
gridButton.setonclickListener(this: :onTileButtonclick);
}
mGame = new MemoryGame();
startGame();
}
private void startGame(){
mGame. newGame();
mTurnNumber =0;
mFirstBtnNdx =-1;
for (int buttonIndex =0; buttonIndex < mMemoryGrid.getChildCount(); buttonIndex++)
{
Button gridButton =(Button) mMemoryGrid.getchildAt(buttonIndex);
// TODO #2- 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)
}
}
1 usage
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();
}
mTurnNumber =0;
}
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; ) 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();
}
}
2 usages
public class MemoryGame {
3 usages
private int mGameSize; // How many tiles will be displayed
2 usages
private int[] mTilecolors; // Array to hold the color assigned to each tile in the game
1 usage
public MemoryGame()
{
// Default game size
mGamesize =16;
// Create color array
mTilecolors = new int[mGameSize];
}
public void newGame()
{
Random rn= new Random();
// Create an ArrayList and initialize with all possible values for the game tiles...
ArrayList<
1) In MemoryGame.java, complete the definition for the getTileColor method so it will return the indicated element from the mTileColors array.
2) 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.
3) 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.
4) 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.
5) Create a Landscape Qualifier for activity_main.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

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 1 Lnai 6321

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215879X, 978-3642158797

More Books

Students also viewed these Databases questions