Question: Java In MainActivity.java, implement a method public void resetAction(View view) which will serve as a listener for the reset button. This method should make the
Java
In MainActivity.java, implement a method public void resetAction(View view) which will serve as a listener for the reset button. This method should make the appropriate method calls( reset() in TwentyFortyEight ) to reset both the visible game board and the internal game board in TwentyFortyEight.java.
import javax.swing.*; import java.awt.event.* import android.net.wifi.p2p.WifiP2pManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.GridView; import android.widget.TextView; public class MainActivity extends AppCompatActivity { //The context of the app. Context is used to refer to certain resources of the app outside of //the MainActivity class private static Context mContext; public static Context getAppContext() { return mContext; } private TwentyFortyEight twentyFortyEight; private CustomGrid customGrid; private TextView scoreBox; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = getApplicationContext(); twentyFortyEight = new TwentyFortyEight(4); customGrid = new CustomGrid(); GridView grid = (GridView) findViewById(R.id.mainGrid); scoreBox = (TextView) findViewById(R.id.scoreBox); grid.setAdapter(customGrid); //first starts twentyFortyEight.reset(); } /** * Method invoked when the Up button is pressed * * @param view - the UI of the app */ public void upAction(View view) { while(twentyFortyEight.moveUp()){twentyFortyEight.placeRandom();} customGrid.updateGrid(twentyFortyEight.getBoard()); scoreBox.setText(String.valueOf(twentyFortyEight.getScore())); //the score from your TwentyFortyEight //NOTE: You should pass the String value of score to setText. Eg. scoreBox.setText(String.valueOf(twentyFortyEighty.getScore())); } /** * Method invoked when the Down button is pressed * * @param view - the UI of the app */ public void downAction(View view) { while(twentyFortyEight.moveDown()){twentyFortyEight.placeRandom();} customGrid.updateGrid(twentyFortyEight.getBoard()); scoreBox.setText(String.valueOf(twentyFortyEight.getScore())); //the score from your TwentyFortyEight //NOTE: You should pass the String value of score to setText. Eg. scoreBox.setText(String.valueOf(twentyFortyEighty.getScore())); } /** * Method invoked when the Left button is pressed * * @param view - the UI of the app */ public void leftAction(View view) { while(twentyFortyEight.moveLeft()){twentyFortyEight.placeRandom();} customGrid.updateGrid(twentyFortyEight.getBoard()); scoreBox.setText(String.valueOf(twentyFortyEight.getScore())); //the score from your TwentyFortyEight //NOTE: You should pass the String value of score to setText. Eg. scoreBox.setText(String.valueOf(twentyFortyEighty.getScore())); } /** * Method invoked when the Right button is pressed * * @param view - the UI of the app */ public void rightAction(View view) { while(twentyFortyEight.moveRight()){twentyFortyEight.placeRandom();} customGrid.updateGrid(twentyFortyEight.getBoard()); scoreBox.setText(String.valueOf(twentyFortyEight.getScore())); //the score from your TwentyFortyEight //NOTE: You should pass the String value of score to setText. Eg. scoreBox.setText(String.valueOf(twentyFortyEighty.getScore())); } public void resetAction(View view){ //To do } }
<TextView android:background="@drawable/option_box" android:text="Reset" android:layout_height="30dp" android:layout_width="100dp" android:textAlignment="center" android:textSize="20dp" android:paddingTop="3dp" android:textColor="#ffffff" android:id="@+id/resetBox" android:layout_below="@+id/titleBox" android:layout_toEndOf="@+id/scoreBox" android:onClick="resetAction"/>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
