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

Mvc #13

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

Mvc #13

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
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=".controller.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
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,87 @@

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.model.Board;
import com.acme.tictactoe.model.Player;

public class TicTacToeActivity extends AppCompatActivity {

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

private Board model;

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

@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);

model = new Board();
}

@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:
reset();
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 + "]");

Player playerThatMoved = model.mark(row, col);

if(playerThatMoved != null) {
button.setText(playerThatMoved.toString());
if (model.getWinner() != null) {
winnerPlayerLabel.setText(playerThatMoved.toString());
winnerPlayerViewGroup.setVisibility(View.VISIBLE);
}
}

}

private void reset() {
winnerPlayerViewGroup.setVisibility(View.GONE);
winnerPlayerLabel.setText("");

model.restart();

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

}
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 }
95 changes: 91 additions & 4 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"
android:gravity="center_horizontal"
tools:context="com.acme.tictactoe.controller.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=".controller.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>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">TicTacToe</string>
<string name="winner">Winner</string>
<string name="action_reset">Reset</string>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="tictactoebutton">
<item name="android:layout_width">100dp</item>
<item name="android:layout_height">100dp</item>
<item name="android:textSize">30sp</item>
</style>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void test3inRowAcrossTopForX() {


/**
* This test will simulate and verify x is the winner.
* This test will simulate and verify o is the winner.
*
* O | X | X
* | O |
Expand Down