diff --git a/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/model/Cell.java b/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/model/Cell.java index 49bff81..ecb5ad0 100644 --- a/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/model/Cell.java +++ b/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/model/Cell.java @@ -5,13 +5,17 @@ public class Cell { - public Player player; + private Player player; public Cell(Player player) { this.player = player; } public boolean isEmpty() { - return player == null || StringUtility.isNullOrEmpty(player.value); + return player == null || StringUtility.isNullOrEmpty(player.getValue()); + } + + public Player getPlayerCell(){ + return player; } } diff --git a/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/model/Game.java b/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/model/Game.java index 7366352..9012170 100644 --- a/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/model/Game.java +++ b/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/model/Game.java @@ -97,12 +97,12 @@ private boolean areEqual(Cell... cells) { return false; for (Cell cell : cells) - if (cell == null || isNullOrEmpty(cell.player.value)) + if (cell == null || isNullOrEmpty(cell.getPlayerCell().getValue())) return false; Cell comparisonBase = cells[0]; for (int i = 1; i < cells.length; i++) - if (!comparisonBase.player.value.equals(cells[i].player.value)) + if (!comparisonBase.getPlayerCell().getValue().equals(cells[i].getPlayerCell().getValue())) return false; return true; diff --git a/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/model/Player.java b/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/model/Player.java index def77fe..bfbfeed 100644 --- a/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/model/Player.java +++ b/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/model/Player.java @@ -3,11 +3,19 @@ public class Player { - public String name; - public String value; + private String name; + private String value; public Player(String name, String value) { this.name = name; this.value = value; } + + public String getName(){ + return this.name; + } + + public String getValue(){ + return this.value; + } } diff --git a/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/view/GameActivity.java b/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/view/GameActivity.java index 5f98204..69e8179 100644 --- a/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/view/GameActivity.java +++ b/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/view/GameActivity.java @@ -5,6 +5,10 @@ import android.os.Bundle; import android.support.annotation.VisibleForTesting; import android.support.v7.app.AppCompatActivity; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.widget.Toast; import husaynhakeem.io.tictactoe_mvvm.R; import husaynhakeem.io.tictactoe_mvvm.databinding.ActivityGameBinding; @@ -20,12 +24,47 @@ public class GameActivity extends AppCompatActivity { private static final String NO_WINNER = "No one"; private GameViewModel gameViewModel; + private int counter = 0; + private Toast toast; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); promptForPlayers(); } + + @Override + public void onBackPressed() { + counter++; + if(counter == 1) { + toast = Toast.makeText(getApplicationContext(), "Press again to exit", Toast.LENGTH_SHORT); + toast.show(); + } + if(counter == 2) { + if(toast != null) + toast.cancel(); + finish(); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater menuInflater = getMenuInflater(); + menuInflater.inflate(R.menu.navigation_bar, menu); + return super.onCreateOptionsMenu(menu); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == R.id.replay) { + promptForPlayers(); + return true; + } + return super.onOptionsItemSelected(item); + } + + public void promptForPlayers() { GameBeginDialog dialog = GameBeginDialog.newInstance(this); dialog.setCancelable(false); @@ -50,7 +89,7 @@ private void setUpOnGameEndListener() { @VisibleForTesting public void onGameWinnerChanged(Player winner) { - String winnerName = winner == null || isNullOrEmpty(winner.name) ? NO_WINNER : winner.name; + String winnerName = winner == null || isNullOrEmpty(winner.getName()) ? NO_WINNER : winner.getName(); GameEndDialog dialog = GameEndDialog.newInstance(this, winnerName); dialog.setCancelable(false); dialog.show(getSupportFragmentManager(), GAME_END_DIALOG_TAG); diff --git a/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/viewmodel/GameViewModel.java b/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/viewmodel/GameViewModel.java index e82c3b5..0066f7e 100644 --- a/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/viewmodel/GameViewModel.java +++ b/app/src/main/java/husaynhakeem/io/tictactoe_mvvm/viewmodel/GameViewModel.java @@ -24,7 +24,7 @@ public void init(String player1, String player2) { public void onClickedCellAt(int row, int column) { if (game.cells[row][column] == null) { game.cells[row][column] = new Cell(game.currentPlayer); - cells.put(stringFromNumbers(row, column), game.currentPlayer.value); + cells.put(stringFromNumbers(row, column), game.currentPlayer.getValue()); if (game.hasGameEnded()) game.reset(); else diff --git a/app/src/main/res/drawable/ic_baseline_replay_24.xml b/app/src/main/res/drawable/ic_baseline_replay_24.xml new file mode 100644 index 0000000..477731f --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_replay_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/menu/navigation_bar.xml b/app/src/main/res/menu/navigation_bar.xml new file mode 100644 index 0000000..2591328 --- /dev/null +++ b/app/src/main/res/menu/navigation_bar.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7a7927c..399a96f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,4 +7,5 @@ Name mustn\'t be empty Names mustn\'t be the same The winner is + New Game diff --git a/build.gradle b/build.gradle index bed19a5..cf95a39 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.3.2' + classpath 'com.android.tools.build:gradle:4.2.2' } } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 7612b0f..9d73a73 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip