Moved game to fragment for easier tablet layout management. Fixed redundant string formatting on game over text. Added menu with reset game option

This commit is contained in:
William Brawner 2017-03-04 23:15:09 -06:00
parent 1569071861
commit f995df3564
10 changed files with 217 additions and 138 deletions

View file

@ -5,7 +5,7 @@
<GradleProjectSettings>
<option name="distributionType" value="LOCAL" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="$APPLICATION_HOME_DIR$/gradle/gradle-2.14.1" />
<option name="gradleHome" value="C:\Program Files\Android\Android Studio\gradle\gradle-2.14.1" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />

View file

@ -4,6 +4,7 @@
<modules>
<module fileurl="file://$PROJECT_DIR$/NumberGuess.iml" filepath="$PROJECT_DIR$/NumberGuess.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/ngg.iml" filepath="$PROJECT_DIR$/ngg.iml" />
</modules>
</component>
</project>

View file

@ -24,7 +24,7 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:support-v4:25.1.0'
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:support-v4:25.1.1'
testCompile 'junit:junit:4.12'
}

View file

@ -0,0 +1,122 @@
package com.wbrawner.numberguess;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Map;
public class GameFragment extends Fragment {
private EditText guessInput;
private Button guessButton;
private Player user;
private NumberGuess ng;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_game, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_reset_game) {
resetGame();
}
return super.onOptionsItemSelected(item);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_game, container, false);
guessInput = (EditText) rootView.findViewById(R.id.guessInput);
guessButton = (Button) rootView.findViewById(R.id.guessButton);
guessButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!guessInput.getText().toString().equals("")) {
user.addGuess(
Integer.parseInt(
guessInput.getText().toString()
)
);
}
setResponse(
ng.checkAnswer(user)
);
guessInput.setText(null);
}
});
resetGame();
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
public void gameOver(String message) {
GameOverDialogFragment gameOverPrompt = new GameOverDialogFragment();
gameOverPrompt.setMessage(message);
gameOverPrompt.show(getActivity().getFragmentManager(), "gameOver");
}
public void resetGame() {
guessInput.setText("50");
guessInput.setSelection(2);
user = null;
ng = null;
user = new Player();
user.clearGuesses();
ng = new NumberGuess();
ng.setAnswer();
// Log the number for quicker debugging
Log.d("DEBUG", "Number: " + ng.getAnswer());
}
public void setResponse(Map response) {
CharSequence text;
Context context = getActivity();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, null, duration);
if (response.get("code").equals("correct")) {
String popup = getActivity().getString(R.string.correct_answer, response.get("count"));
text = getString(R.string.game_over);
gameOver(popup);
} else {
toast.setGravity(Gravity.CENTER, 0, 0);
if (response.get("code").equals("too_big")) {
text = getString(R.string.too_big);
} else {
text = getString(R.string.too_small);
}
}
toast.setText(text);
toast.show();
}
}

View file

@ -1,93 +1,31 @@
package com.wbrawner.numberguess;
import android.app.DialogFragment;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Map;
public class MainActivity extends AppCompatActivity
implements GameOverDialogFragment.GameOverListener {
implements GameOverDialogFragment.GameOverListener {
public EditText guessInput;
public TextView guessResponse;
public Player user;
public NumberGuess ng;
private String GAME_FRAGMENT_TAG = "GF_TAG";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager()
.beginTransaction()
.add(R.id.container_main, new GameFragment(), GAME_FRAGMENT_TAG)
.commit();
}
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
this.resetGame();
}
public void gameOver(String message) {
GameOverDialogFragment gameOverPrompt = new GameOverDialogFragment();
gameOverPrompt.setMessage(message);
gameOverPrompt.show(getFragmentManager(), "gameOver");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.guessInput = (EditText) findViewById(R.id.guessInput);
resetGame();
}
public void resetGame() {
this.guessInput.setText("50");
this.guessInput.setSelection(2);
this.user = null;
this.ng = null;
this.user = new Player();
this.user.clearGuesses();
this.ng = new NumberGuess();
this.ng.setAnswer();
}
public void setResponse(Map response) {
CharSequence text;
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, null, duration);
if (response.get("code").equals("correct")) {
String popup = String.format(getString(R.string.correct_answer), response.get("count"));
text = getString(R.string.game_over);
gameOver(popup);
} else {
toast.setGravity(Gravity.CENTER, 0, 0);
if (response.get("code").equals("too_big")) {
text = getString(R.string.too_big);
} else {
text = getString(R.string.too_small);
}
}
toast.setText(text);
toast.show();
}
public void setResponse(String response) {
this.guessResponse.setText(response);
}
public void onClickGuessButton(View view) {
if (!guessInput.getText().toString().equals("")) {
this.user.addGuess(
Integer.parseInt(
guessInput.getText().toString()
)
);
}
this.setResponse(
this.ng.checkAnswer(this.user)
);
this.guessInput.setText(null);
GameFragment gameFragment = (GameFragment) getFragmentManager()
.findFragmentByTag(GAME_FRAGMENT_TAG);
gameFragment.resetGame();
}
}

View file

@ -1,57 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.wbrawner.numberguess.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/guess_prompt"
android:textAlignment="center"
android:layout_marginTop="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/guessPrompt" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/guessPrompt"
android:layout_marginTop="50dp"
android:weightSum="10">
<EditText
android:imeOptions="flagNoExtractUi"
android:layout_weight="5"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:inputType="number"
android:id="@+id/guessInput"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAlignment="center"
android:text="50" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:id="@+id/guessButton"
android:text="@string/guess_button"
android:onClick="onClickGuessButton"/>
</LinearLayout>
</RelativeLayout>
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container_main"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" />

View file

@ -0,0 +1,56 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_game"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.wbrawner.numberguess.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/guess_prompt"
android:textAlignment="center"
android:layout_marginTop="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/guessPrompt" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/guessPrompt"
android:layout_marginTop="50dp"
android:weightSum="10">
<EditText
android:imeOptions="flagNoExtractUi"
android:layout_weight="5"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:inputType="number"
android:id="@+id/guessInput"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAlignment="center"
android:text="50" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:id="@+id/guessButton"
android:text="@string/guess_button" />
</LinearLayout>
</RelativeLayout>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_reset_game"
android:title="@string/reset" />
</menu>

View file

@ -10,4 +10,5 @@
<string name="yes"></string>
<string name="ok">OK</string>
<string name="game_over">¡Ganaste!</string>
<string name="reset">Restablecer</string>
</resources>

View file

@ -10,4 +10,8 @@
<string name="yes">Yes</string>
<string name="no">No</string>
<string name="ok">OK</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="reset">Reset</string>
</resources>