Skip to content

Commit

Permalink
Merge branch 'develop' into feature/GAMES_006
Browse files Browse the repository at this point in the history
  • Loading branch information
pkohout authored Apr 29, 2020
2 parents f7d05a5 + e6c0c73 commit a819a7f
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 20 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/apk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: APK CI

on:
push:
branches:
- 'master'

jobs:
apk:
name: Generate APK
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
java-package: jdk
- name: Build debug APK
run: bash ./gradlew assembleDebug --stacktrace
- name: Upload APK
uses: actions/upload-artifact@v1
with:
name: app
path: app/build/outputs/apk/debug/app-debug.apk

74 changes: 57 additions & 17 deletions app/src/androidTest/java/com/swt20/swt_morning2/TicTacToeTests.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
package com.swt20.swt_morning2;

import androidx.test.espresso.Espresso;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.action.ViewActions;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;

import org.junit.Before;
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.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.RootMatchers.withDecorView;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

@RunWith(AndroidJUnit4.class)
public class TicTacToeTests {

/* TODO we have to delete this for findBugs, since it is not used
but we did not wrote that code, so we are not sure if it is ok to remove it.
@Rule
public ActivityTestRule<MainActivity> activityRule
= new ActivityTestRule<>(MainActivity.class);
*/

@Test
public void playGame() {

public void playTheGameABit() {
for (int a = 0; a < 2; a++) {

// Go from Main Menu to Tic-Tac-Toe Menu
onView(withId(R.id.ticTacToeButton)).perform(click());

for (int b = 0; b < 2; b++) {

// Go from TicTacToe Menu to Game
onView(withId(R.id.ttt_menu_button)).perform(click());

// Play
for (int c = 0; c < 2; c++) {
onView(withId(R.id.imageView)).perform(click());
Expand All @@ -46,19 +41,64 @@ public void playGame() {
onView(withId(R.id.imageView4)).perform(click());
onView(withId(R.id.imageView5)).perform(click());
onView(withId(R.id.imageView6)).perform(click());
onView(withId(R.id.imageView7)).perform(click());
onView(withId(R.id.imageView8)).perform(click());
onView(withId(R.id.imageView9)).perform(click());
}

// Go back to Tic-Tac-Toe Menu
Espresso.pressBackUnconditionally();
}

// Go back to Main Menu
Espresso.pressBackUnconditionally();
}
}

@Test
public void playToWin() {
onView(withId(R.id.ticTacToeButton)).perform(click());
onView(withId(R.id.ttt_menu_button)).perform(click());
onView(withId(R.id.imageView)).perform(click());
onView(withId(R.id.imageView2)).perform(click());
onView(withId(R.id.imageView3)).perform(click());
onView(withId(R.id.imageView4)).perform(click());
onView(withId(R.id.imageView5)).perform(click());
onView(withId(R.id.imageView6)).perform(click());
onView(withId(R.id.imageView7)).perform(click());
onView(withId(R.id.ttt_menu_button)).check(matches(isDisplayed()));
onView(withText(R.string.you_win)).inRoot(withDecorView(not(is(activityRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed()));

Espresso.pressBackUnconditionally();
}

@Test
public void playToLose() {
onView(withId(R.id.ticTacToeButton)).perform(click());
onView(withId(R.id.ttt_menu_button)).perform(click());
onView(withId(R.id.imageView)).perform(click());
onView(withId(R.id.imageView3)).perform(click());
onView(withId(R.id.imageView5)).perform(click());
onView(withId(R.id.imageView4)).perform(click());
onView(withId(R.id.imageView9)).perform(click());
onView(withId(R.id.imageView8)).perform(click());
onView(withId(R.id.ttt_menu_button)).check(matches(isDisplayed()));
onView(withText(R.string.you_lose)).inRoot(withDecorView(not(is(activityRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed()));

Espresso.pressBackUnconditionally();
}

@Test
public void checkPointsWhenWinning() {
onView(withId(R.id.ticTacToeButton)).perform(click());
onView(withId(R.id.ttt_menu_button)).perform(click());
onView(withId(R.id.imageView)).perform(click());
onView(withId(R.id.imageView2)).perform(click());
onView(withId(R.id.imageView3)).perform(click());
onView(withId(R.id.imageView4)).perform(click());
onView(withId(R.id.imageView5)).perform(click());
onView(withId(R.id.imageView6)).perform(click());
onView(withId(R.id.imageView7)).perform(click());
onView(withId(R.id.ttt_menu_button)).check(matches(isDisplayed()));
onView(withText(R.string.you_win)).inRoot(withDecorView(not(is(activityRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed()));

Espresso.pressBackUnconditionally();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

public class TicTacToeGameFragment extends Fragment {

Expand Down Expand Up @@ -46,6 +48,20 @@ private void setupCell(final int x, final int y, final ImageView imageView) {
public void onClick(View view) {
if (logic.turn(x, y)) {
imageView.setImageResource(logic.getCell(x, y).getOwner().getResId());
TicTacToeGameLogic.Player winner = logic.getWinner();
if(winner != null) {
String text;
ScoreTracker tracker = new ScoreTracker(imageView.getContext());
if(winner.equals(logic.getFirst())) {
text = getResources().getString(R.string.you_win);
} else {
text = getResources().getString(R.string.you_lose);
}
logic.changeScore(winner, tracker);
Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
NavHostFragment.findNavController(TicTacToeGameFragment.this)
.navigate(R.id.action_Game_to_Menu);
}
}
}
});
Expand Down
83 changes: 83 additions & 0 deletions app/src/main/java/com/swt20/swt_morning2/TicTacToeGameLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,89 @@ public boolean turn(int x, int y) {
return validPlay;
}

public Player getWinner() {
for (int i = 0; i < 3; i++) {
int counter = 0;
for (int j = 0; j < 3; j++) {
if(cells[i][j].getOwner() == null) {
continue;
}
if(cells[i][j].getOwner().equals(this.first)) {
counter++;
} else {
counter--;
}
}
if (counter == 3) {
return this.first;
} else if(counter == -3) {
return this.second;
}
}

for (int i = 0; i < 3; i++) {
int counter = 0;
for (int j = 0; j < 3; j++) {
if(cells[j][i].getOwner() == null) {
continue;
}
if(cells[j][i].getOwner().equals(this.first)) {
counter++;
} else {
counter--;
}
}
if (counter == 3) {
return this.first;
} else if(counter == -3) {
return this.second;
}
}
int counter = 0;
for (int i = 0; i < 3; i++) {
if(cells[i][i].getOwner() == null) {
continue;
}
if(cells[i][i].getOwner().equals(this.first)) {
counter++;
} else {
counter--;
}
}
if (counter == 3) {
return this.first;
} else if(counter == -3) {
return this.second;
}

counter = 0;
for (int i = 2; i >= 0; i--) {
if(cells[i][2-i].getOwner() == null) {
continue;
}
if(cells[i][2-i].getOwner().equals(this.first)) {
counter++;
} else {
counter--;
}
}
if (counter == 3) {
return this.first;
} else if(counter == -3) {
return this.second;
}
return null;
}

public void changeScore(Player winner, ScoreTracker tracker) {
if(winner == null) return;
if(winner.equals(this.first)) {
tracker.addScore(Game.TICTACTOE, 1);
} else {
tracker.reduceScore(Game.TICTACTOE, 2);
}
}

public Player getFirst() {
return first;
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-de-rAT/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@
<string name="hangman_WIN">Gratuliere, du hast das Wort erraten!</string>
<string name="hangman_default_words">{ \"standardWords\": [ \"Test\", \"Apfelbaum\", \"Auto\", \"Mensch\", \"Chrysantheme\", \"Kernspintomografie\", \"Zucchini\", \"Rueckgrat\", \"unentgeltlich\", \"Verlies\", \"Terrasse\", \"Quarzuhr\" ], \"customWords\": [ ] }</string>
<string name="hangman_playagain">Nochmal spielen</string>
<string name="you_win">Du gewinnst!</string>
<string name="you_lose">Du verlierst!</string>
</resources>
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
<string name="tic_tac_toe_7">TicTacToe7</string>
<string name="tic_tac_toe_8">TicTacToe8</string>
<string name="tic_tac_toe_9">TicTacToe9</string>

<string name="hangman_question_guess">Enter your guess:</string>
<string name="hangman_question_txt">guess?</string>
<string name="hangman_WIN">\"Congratulations. You won!\"</string>
<string name="hangman_default_words">{ \"standardWords\": [ \"fuchsia\", \"jigsaw\", \"jogging\", \"jukebox\", \"kilobyte\", \"matrix\", \"unworthy\", \"schnapps\", \"sphinx\", \"bookworm\", \"cryptography\", \"razzmatazz\" ], \"customWords\": [ ] }</string>
<string name="hangman_playagain">Play again</string>

<string name="you_win">You win!</string>
<string name="you_lose">You lose!</string>

</resources>
Loading

0 comments on commit a819a7f

Please sign in to comment.