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:
parent
1569071861
commit
f995df3564
10 changed files with 217 additions and 138 deletions
|
@ -5,7 +5,7 @@
|
||||||
<GradleProjectSettings>
|
<GradleProjectSettings>
|
||||||
<option name="distributionType" value="LOCAL" />
|
<option name="distributionType" value="LOCAL" />
|
||||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
<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">
|
<option name="modules">
|
||||||
<set>
|
<set>
|
||||||
<option value="$PROJECT_DIR$" />
|
<option value="$PROJECT_DIR$" />
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
<modules>
|
<modules>
|
||||||
<module fileurl="file://$PROJECT_DIR$/NumberGuess.iml" filepath="$PROJECT_DIR$/NumberGuess.iml" />
|
<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$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/ngg.iml" filepath="$PROJECT_DIR$/ngg.iml" />
|
||||||
</modules>
|
</modules>
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
|
@ -24,7 +24,7 @@ dependencies {
|
||||||
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
|
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
|
||||||
exclude group: 'com.android.support', module: 'support-annotations'
|
exclude group: 'com.android.support', module: 'support-annotations'
|
||||||
})
|
})
|
||||||
compile 'com.android.support:appcompat-v7:25.1.0'
|
compile 'com.android.support:appcompat-v7:25.1.1'
|
||||||
compile 'com.android.support:support-v4:25.1.0'
|
compile 'com.android.support:support-v4:25.1.1'
|
||||||
testCompile 'junit:junit:4.12'
|
testCompile 'junit:junit:4.12'
|
||||||
}
|
}
|
||||||
|
|
122
app/src/main/java/com/wbrawner/numberguess/GameFragment.java
Normal file
122
app/src/main/java/com/wbrawner/numberguess/GameFragment.java
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,93 +1,31 @@
|
||||||
package com.wbrawner.numberguess;
|
package com.wbrawner.numberguess;
|
||||||
|
|
||||||
import android.app.DialogFragment;
|
import android.app.DialogFragment;
|
||||||
import android.content.Context;
|
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.os.Bundle;
|
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
|
public class MainActivity extends AppCompatActivity
|
||||||
implements GameOverDialogFragment.GameOverListener {
|
implements GameOverDialogFragment.GameOverListener {
|
||||||
|
|
||||||
public EditText guessInput;
|
private String GAME_FRAGMENT_TAG = "GF_TAG";
|
||||||
public TextView guessResponse;
|
|
||||||
public Player user;
|
@Override
|
||||||
public NumberGuess ng;
|
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
|
@Override
|
||||||
public void onDialogPositiveClick(DialogFragment dialog) {
|
public void onDialogPositiveClick(DialogFragment dialog) {
|
||||||
this.resetGame();
|
GameFragment gameFragment = (GameFragment) getFragmentManager()
|
||||||
}
|
.findFragmentByTag(GAME_FRAGMENT_TAG);
|
||||||
|
gameFragment.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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,57 +1,8 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<FrameLayout 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"
|
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
android:layout_width="match_parent"
|
||||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
android:id="@+id/container_main"
|
||||||
android:paddingTop="@dimen/activity_vertical_margin"
|
tools:context=".MainActivity"
|
||||||
tools:context="com.wbrawner.numberguess.MainActivity">
|
tools:ignore="MergeRootFrame" />
|
||||||
|
|
||||||
<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>
|
|
56
app/src/main/res/layout/fragment_game.xml
Normal file
56
app/src/main/res/layout/fragment_game.xml
Normal 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>
|
||||||
|
|
6
app/src/main/res/menu/fragment_game.xml
Normal file
6
app/src/main/res/menu/fragment_game.xml
Normal 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>
|
|
@ -10,4 +10,5 @@
|
||||||
<string name="yes">Sí</string>
|
<string name="yes">Sí</string>
|
||||||
<string name="ok">OK</string>
|
<string name="ok">OK</string>
|
||||||
<string name="game_over">¡Ganaste!</string>
|
<string name="game_over">¡Ganaste!</string>
|
||||||
|
<string name="reset">Restablecer</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
|
@ -10,4 +10,8 @@
|
||||||
<string name="yes">Yes</string>
|
<string name="yes">Yes</string>
|
||||||
<string name="no">No</string>
|
<string name="no">No</string>
|
||||||
<string name="ok">OK</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>
|
</resources>
|
||||||
|
|
Loading…
Reference in a new issue