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

Mvp #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Mvp #11

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
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ dependencies {
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-all:1.9.5'
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

/**
* Instrumentation test, which will execute on an Android device.
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".controller.TicTacToeActivity">
<activity android:name=".view.TicTacToeActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Expand Down

This file was deleted.

11 changes: 10 additions & 1 deletion app/src/main/java/com/acme/tictactoe/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public Board() {
*/
public void restart() {
clearCells();
winner = null;
currentTurn = Player.X;
state = GameState.IN_PROGRESS;
}
Expand All @@ -33,11 +34,17 @@ public void restart() {
*
* @param row 0..2
* @param col 0..2
* @return the player that moved or null if we did not move anything.
*
*/
public void mark( int row, int col ) {
public Player mark( int row, int col ) {

Player playerThatMoved = null;

if(isValid(row, col)) {

cells[row][col].setValue(currentTurn);
playerThatMoved = currentTurn;

if(isWinningMoveByPlayer(currentTurn, row, col)) {
state = GameState.FINISHED;
Expand All @@ -48,6 +55,8 @@ public void mark( int row, int col ) {
flipCurrentTurn();
}
}

return playerThatMoved;
}

public Player getWinner() {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/acme/tictactoe/model/Player.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.acme.tictactoe.model;

enum Player { X , O }
public enum Player { X , O }
11 changes: 11 additions & 0 deletions app/src/main/java/com/acme/tictactoe/presenter/Presenter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.acme.tictactoe.presenter;


public interface Presenter {

void onCreate();
void onPause();
void onResume();
void onDestroy();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.acme.tictactoe.presenter;

import com.acme.tictactoe.model.Board;
import com.acme.tictactoe.model.Player;
import com.acme.tictactoe.view.TicTacToeView;

public class TicTacToePresenter implements Presenter {

private TicTacToeView view;
private Board model;

public TicTacToePresenter(TicTacToeView view) {
this.view = view;
this.model = new Board();
}

@Override
public void onCreate() {
model = new Board();
}

@Override
public void onPause() {

}

@Override
public void onResume() {

}

@Override
public void onDestroy() {

}

public void onButtonSelected(int row, int col) {
Player playerThatMoved = model.mark(row, col);

if(playerThatMoved != null) {
view.setButtonText(row, col, playerThatMoved.toString());

if (model.getWinner() != null) {
view.showWinner(playerThatMoved.toString());
}
}
}

public void onResetSelected() {
view.clearWinnerDisplay();
view.clearButtons();
model.restart();
}


}
107 changes: 107 additions & 0 deletions app/src/main/java/com/acme/tictactoe/view/TicTacToeActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.acme.tictactoe.view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
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.TextView;

import com.acme.tictactoe.R;
import com.acme.tictactoe.presenter.TicTacToePresenter;

public class TicTacToeActivity extends AppCompatActivity implements TicTacToeView {

private static String TAG = TicTacToeActivity.class.getName();

private ViewGroup buttonGrid;
private View winnerPlayerViewGroup;
private TextView winnerPlayerLabel;

TicTacToePresenter presenter = new TicTacToePresenter(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tictactoe);
winnerPlayerLabel = (TextView) findViewById(R.id.winnerPlayerLabel);
winnerPlayerViewGroup = findViewById(R.id.winnerPlayerViewGroup);
buttonGrid = (ViewGroup) findViewById(R.id.buttonGrid);
presenter.onCreate();
}

@Override
protected void onPause() {
super.onPause();
presenter.onPause();
}

@Override
protected void onResume() {
super.onResume();
presenter.onResume();
}

@Override
protected void onDestroy() {
super.onDestroy();
presenter.onDestroy();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_tictactoe, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_reset:
presenter.onResetSelected();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

public void onCellClicked(View v) {

Button button = (Button) v;
String tag = button.getTag().toString();
int row = Integer.valueOf(tag.substring(0,1));
int col = Integer.valueOf(tag.substring(1,2));
Log.i(TAG, "Click Row: [" + row + "," + col + "]");

presenter.onButtonSelected(row, col);

}

@Override
public void setButtonText(int row, int col, String text) {
Button btn = (Button) buttonGrid.findViewWithTag("" + row + col);
if(btn != null) {
btn.setText(text);
}
}

public void clearButtons() {
for( int i = 0; i < buttonGrid.getChildCount(); i++ ) {
((Button) buttonGrid.getChildAt(i)).setText("");
}
}

public void showWinner(String winningPlayerDisplayLabel) {
winnerPlayerLabel.setText(winningPlayerDisplayLabel);
winnerPlayerViewGroup.setVisibility(View.VISIBLE);
}

public void clearWinnerDisplay() {
winnerPlayerViewGroup.setVisibility(View.GONE);
winnerPlayerLabel.setText("");
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/acme/tictactoe/view/TicTacToeView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.acme.tictactoe.view;

public interface TicTacToeView {
void showWinner(String winningPlayerDisplayLabel);
void clearWinnerDisplay();
void clearButtons();
void setButtonText(int row, int col, String text);
}
97 changes: 92 additions & 5 deletions app/src/main/res/layout/tictactoe.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,104 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tictactoe"
android:orientation="vertical"
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.acme.tictactoe.controller.TicTacToeActivity">
android:gravity="center_horizontal"
tools:context="com.acme.tictactoe.view.TicTacToeActivity">

<TextView
<GridLayout
android:id="@+id/buttonGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
android:columnCount="3"
android:rowCount="3">

<Button
android:tag="00"
android:onClick="onCellClicked"
style="@style/tictactoebutton"
/>

<Button
android:tag="01"
android:onClick="onCellClicked"
style="@style/tictactoebutton"
/>

<Button
android:tag="02"
android:onClick="onCellClicked"
style="@style/tictactoebutton"
/>

<Button
android:tag="10"
android:onClick="onCellClicked"
style="@style/tictactoebutton"
/>

<Button
android:tag="11"
android:onClick="onCellClicked"
style="@style/tictactoebutton"
/>

<Button
android:tag="12"
android:onClick="onCellClicked"
style="@style/tictactoebutton"
/>

<Button
android:tag="20"
android:onClick="onCellClicked"
style="@style/tictactoebutton"
/>

<Button
android:tag="21"
android:onClick="onCellClicked"
style="@style/tictactoebutton"
/>

<Button
android:tag="22"
android:onClick="onCellClicked"
style="@style/tictactoebutton"
/>

</GridLayout>

<LinearLayout
android:id="@+id/winnerPlayerViewGroup"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible"
>

<TextView
android:id="@+id/winnerPlayerLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:layout_margin="20dp"
tools:text="X" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="@string/winner" />

</LinearLayout>

</LinearLayout>
11 changes: 11 additions & 0 deletions app/src/main/res/menu/menu_tictactoe.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<menu 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"
tools:context=".view.TicTacToeActivity">

<item android:id="@+id/action_reset"
android:title="@string/action_reset"
android:orderInCategory="100"
app:showAsAction="ifRoom" />

</menu>
2 changes: 1 addition & 1 deletion app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="activity_vertical_margin">44dp</dimen>
</resources>
Loading