Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issues #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/husaynhakeem/io/tictactoe_mvvm/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_baseline_replay_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,5V1L7,6l5,5V7c3.31,0 6,2.69 6,6s-2.69,6 -6,6 -6,-2.69 -6,-6H4c0,4.42 3.58,8 8,8s8,-3.58 8,-8 -3.58,-8 -8,-8z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/menu/navigation_bar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/replay"
android:icon="@drawable/ic_baseline_replay_24"
android:title="@string/new_game"
app:showAsAction = "ifRoom" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<string name="game_dialog_empty_name">Name mustn\'t be empty</string>
<string name="game_dialog_same_names">Names mustn\'t be the same</string>
<string name="game_end_dialog_headline">The winner is</string>
<string name="new_game">New Game</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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