-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from glocka/feature/GAMES_006
feature/GAMES_006
- Loading branch information
Showing
11 changed files
with
448 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
app/src/androidTest/java/com/swt20/swt_morning2/HangmanTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.swt20.swt_morning2; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import androidx.test.rule.ActivityTestRule; | ||
|
||
import junit.framework.AssertionFailedError; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static androidx.test.espresso.Espresso.onView; | ||
|
||
import static androidx.test.espresso.action.ViewActions.click; | ||
import static androidx.test.espresso.action.ViewActions.typeText; | ||
import static androidx.test.espresso.assertion.ViewAssertions.matches; | ||
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; | ||
import static androidx.test.espresso.matcher.ViewMatchers.withId; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class HangmanTests { | ||
|
||
@Rule | ||
public ActivityTestRule<MainActivity> activityRule | ||
= new ActivityTestRule<>(MainActivity.class); | ||
|
||
@Test | ||
public void playGame() { | ||
Activity activity = activityRule.getActivity(); | ||
ScoreTracker st = new ScoreTracker(activity.getApplicationContext()); | ||
int old_score = st.getScore(Game.HANGMAN); | ||
// Go from Main Menu to Hangman Menu | ||
onView(withId(R.id.hangmanButton)).perform(click()); | ||
|
||
// Go from Hangman Menu to Game | ||
onView(withId(R.id.ttt_menu_button)).perform(click()); | ||
String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; | ||
for (String str : letters) { | ||
onView(withId(R.id.plainText_nextChar)).perform(typeText(str)); | ||
try { | ||
onView(withId(R.id.button_playagain)).check(matches(isDisplayed())); | ||
// View displayed | ||
assert (st.getScore(Game.HANGMAN) == old_score+1); | ||
return; | ||
} catch (AssertionFailedError e) { | ||
// View not displayed | ||
} | ||
} | ||
assert (false); | ||
} | ||
|
||
@Test | ||
public void playGameTwice() { | ||
|
||
// Go from Main Menu to Hangman Menu | ||
onView(withId(R.id.hangmanButton)).perform(click()); | ||
|
||
// Go from Hangman Menu to Game | ||
onView(withId(R.id.ttt_menu_button)).perform(click()); | ||
|
||
String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; | ||
for(int i = 0; i < 2;i++) { | ||
for (String str : letters) { | ||
onView(withId(R.id.plainText_nextChar)).perform(typeText(str)); | ||
try { | ||
onView(withId(R.id.button_playagain)).check(matches(isDisplayed())); | ||
// View displayed | ||
if(i < 1) { | ||
onView(withId(R.id.button_playagain)).perform(click()); | ||
} else { | ||
return; | ||
} | ||
} catch (AssertionFailedError e) { | ||
// View not displayed | ||
} | ||
} | ||
} | ||
assert (false); | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
app/src/main/java/com/swt20/swt_morning2/HangmanGameFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package com.swt20.swt_morning2; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.os.Bundle; | ||
import android.os.SystemClock; | ||
import android.text.Editable; | ||
import android.text.TextWatcher; | ||
import android.util.Log; | ||
import android.view.LayoutInflater; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.view.WindowManager; | ||
import android.view.inputmethod.InputMethodManager; | ||
import android.widget.EditText; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.fragment.app.Fragment; | ||
import androidx.navigation.fragment.NavHostFragment; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Random; | ||
|
||
import static androidx.core.content.ContextCompat.getSystemService; | ||
|
||
public class HangmanGameFragment extends Fragment { | ||
private static final String TAG = "[HangmanGameFragment]"; | ||
private InputMethodManager imgr; | ||
private TextView nextChar; | ||
private TextView textView; | ||
private String word2guess; | ||
private String word2guess_viewtext = ""; | ||
private TextView textViewWord2Guess; | ||
private WordList wordList; | ||
private SharedPreferences sharedPreferences; | ||
|
||
@Override | ||
public View onCreateView( | ||
LayoutInflater inflater, ViewGroup container, | ||
Bundle savedInstanceState | ||
) { | ||
// Inflate the layout for this fragment | ||
|
||
String lang = Locale.getDefault().getDisplayLanguage(); | ||
if(lang.contains("de")) | ||
{ | ||
sharedPreferences = requireContext().getSharedPreferences("HANGMAN_WORDS_DE", 0); | ||
} else { | ||
sharedPreferences = requireContext().getSharedPreferences("HANGMAN_WORDS_EN", 0); | ||
} | ||
String wordsJson = sharedPreferences.getString("WORDS",getString(R.string.hangman_default_words)); | ||
Gson gson = new Gson(); | ||
wordList = gson.fromJson(wordsJson, WordList.class); | ||
|
||
return inflater.inflate(R.layout.hangman_game, container, false); | ||
} | ||
|
||
public void onViewCreated(@NonNull final View view, Bundle savedInstanceState) { | ||
super.onViewCreated(view, savedInstanceState); | ||
nextChar = view.findViewById(R.id.plainText_nextChar); | ||
textView = view.findViewById(R.id.textView); | ||
imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); | ||
textViewWord2Guess = view.findViewById(R.id.textView_word2guess); | ||
//TODO TCs | ||
|
||
word2guess = wordList.getRandomWord().toUpperCase(Locale.getDefault()); | ||
for (int i = 0; i < word2guess.length(); i++) { | ||
word2guess_viewtext += "_ "; | ||
} | ||
textViewWord2Guess.setText(word2guess_viewtext); | ||
|
||
view.findViewById(R.id.button_playagain).setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
word2guess = wordList.getRandomWord().toUpperCase(Locale.getDefault()); | ||
word2guess_viewtext = ""; | ||
for (int i = 0; i < word2guess.length(); i++) { | ||
word2guess_viewtext += "_ "; | ||
} | ||
textViewWord2Guess.setText(word2guess_viewtext); | ||
textView.setVisibility(View.VISIBLE); | ||
nextChar.setVisibility(View.VISIBLE); | ||
view.findViewById(R.id.button_playagain).setVisibility(View.INVISIBLE); | ||
} | ||
}); | ||
|
||
nextChar.addTextChangedListener(new TextWatcher() { | ||
public void afterTextChanged(Editable s) { | ||
} | ||
|
||
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | ||
} | ||
|
||
public void onTextChanged(CharSequence s, int start, int before, int count) { | ||
String chrs = nextChar.getText().toString().toUpperCase(Locale.getDefault()); | ||
if (chrs.isEmpty()) { | ||
Log.i(TAG, "chrs empty"); | ||
return; | ||
} | ||
char chr = chrs.charAt(0); | ||
Log.i(TAG, "" + (int) chr); | ||
if (chr == ' ') { | ||
return; | ||
} | ||
if (word2guess.contains(Character.toString(chr))) { | ||
int idx = word2guess.indexOf(chr); | ||
StringBuilder newText = new StringBuilder(word2guess_viewtext); | ||
while (idx >= 0) { | ||
newText.setCharAt(idx * 2, chr); | ||
idx = word2guess.indexOf(chr, idx + 1); | ||
} | ||
word2guess_viewtext = newText.toString(); | ||
textViewWord2Guess.setText(word2guess_viewtext); | ||
} | ||
nextChar.setText(""); | ||
|
||
String result = word2guess_viewtext.replace(" ", ""); | ||
if (word2guess.equalsIgnoreCase(result)) { | ||
ScoreTracker st = new ScoreTracker(getContext()); | ||
st.addScore(Game.HANGMAN, 1); | ||
Toast.makeText(getContext(), getString(R.string.hangman_WIN), Toast.LENGTH_LONG).show(); | ||
|
||
view.findViewById(R.id.button_playagain).setVisibility(View.VISIBLE); | ||
textView.setVisibility(View.INVISIBLE); | ||
nextChar.setVisibility(View.INVISIBLE); | ||
|
||
} | ||
} | ||
}); | ||
|
||
nextChar.requestFocus(); | ||
imgr.showSoftInput(nextChar, InputMethodManager.SHOW_IMPLICIT); | ||
} | ||
|
||
public class WordList { | ||
public List<String> standardWords; | ||
public List<String> customWords; | ||
|
||
public String getRandomWord() { | ||
List<String> tmpList = new ArrayList<String>(); | ||
tmpList.addAll(standardWords); | ||
tmpList.addAll(customWords); | ||
Random rand = new Random(); | ||
return tmpList.get(rand.nextInt(tmpList.size())); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/swt20/swt_morning2/HangmanMenuFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.swt20.swt_morning2; | ||
|
||
import android.os.Bundle; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.fragment.app.Fragment; | ||
import androidx.navigation.fragment.NavHostFragment; | ||
|
||
public class HangmanMenuFragment extends Fragment { | ||
|
||
@Override | ||
public View onCreateView( | ||
LayoutInflater inflater, ViewGroup container, | ||
Bundle savedInstanceState | ||
) { | ||
// Inflate the layout for this fragment | ||
return inflater.inflate(R.layout.hangman_menu, container, false); | ||
} | ||
|
||
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { | ||
super.onViewCreated(view, savedInstanceState); | ||
|
||
view.findViewById(R.id.ttt_menu_button).setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
NavHostFragment.findNavController(HangmanMenuFragment.this) | ||
.navigate(R.id.action_Menu_to_Game); | ||
} | ||
}); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:visibility="visible" | ||
tools:context=".HangmanGameFragment"> | ||
|
||
|
||
<TextView | ||
android:id="@+id/textView_word2guess" | ||
android:layout_width="301dp" | ||
android:layout_height="38dp" | ||
android:layout_marginTop="512dp" | ||
android:textAlignment="center" | ||
android:textSize="21sp" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<EditText | ||
android:id="@+id/plainText_nextChar" | ||
android:layout_width="181dp" | ||
android:layout_height="47dp" | ||
android:layout_marginStart="12dp" | ||
android:layout_marginTop="40dp" | ||
android:ems="10" | ||
android:hint="@string/hangman_question_txt" | ||
android:inputType="textPersonName" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.051" | ||
app:layout_constraintStart_toEndOf="@+id/textView" | ||
app:layout_constraintTop_toBottomOf="@+id/textView_word2guess" | ||
android:autofillHints="@string/hangman_question_txt" /> | ||
|
||
<TextView | ||
android:id="@+id/textView" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginStart="52dp" | ||
android:layout_marginTop="60dp" | ||
android:text="@string/hangman_question_guess" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/textView_word2guess" /> | ||
|
||
<Button | ||
android:id="@+id/button_playagain" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/hangman_playagain" | ||
android:visibility="invisible" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.498" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@+id/textView_word2guess" | ||
app:layout_constraintVertical_bias="0.3" /> | ||
|
||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".HangmanMenuFragment"> | ||
|
||
<Button | ||
android:id="@+id/ttt_menu_button" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="492dp" | ||
android:text="@string/start_game" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.498" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
Oops, something went wrong.