Commit 34620e74 authored by Shaun Burch's avatar Shaun Burch 💻

Merge branch '2-show-tic-tac-toe-board' into 'master'

Resolve "Show Tic Tac Toe Board"

Closes #2

See merge request !1
parents 5eb7006b 3e717763
Pipeline #22935951 passed with stages
in 9 minutes and 17 seconds
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/classes" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
......@@ -17,13 +17,21 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:design:27.1.1'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'io.reactivex.rxjava2:rxjava:2.1.14'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
......
package com.example.tictactoe;
import android.util.Log;
import java.util.Arrays;
public class Board {
private static final String TAG = Board.class.getSimpleName();
private Mark[][] mCells;
Board() {
mCells = new Mark[][]{
{Mark.EMPTY, Mark.EMPTY, Mark.EMPTY},
{Mark.EMPTY, Mark.EMPTY, Mark.EMPTY},
{Mark.EMPTY, Mark.EMPTY, Mark.EMPTY},
};
}
boolean markCell(Mark mark, int cellX, int cellY) {
// Check if cell is empty, return false if not
if(mCells[cellY][cellX] != Mark.EMPTY) {
return false;
}
try {
mCells[cellY][cellX] = mark;
} catch (IndexOutOfBoundsException e) {
Log.e(TAG, String.format("Cell {x: %1d, y: %2d} is out of bounds!", cellX, cellY));
}
Log.d(TAG, Arrays.deepToString(mCells));
return true;
}
public Mark[][] getCells() {
return mCells;
}
}
package com.example.tictactoe;
import android.graphics.Point;
import android.util.Log;
/**
* Helper class to map view cells to {@link Point}s (e.g. coordinate) on the {@link Board}.
* <p>
* Bit of a code smell, but it works!
*/
public class BoardMapper {
private static final String TAG = BoardMapper.class.getSimpleName();
static Point viewToPoint(int cellID) {
switch (cellID) {
case R.id.cell_A1:
return new Point(0, 0);
case R.id.cell_A2:
return new Point(1, 0);
case R.id.cell_A3:
return new Point(2, 0);
case R.id.cell_B1:
return new Point(0, 1);
case R.id.cell_B2:
return new Point(1, 1);
case R.id.cell_B3:
return new Point(2, 1);
case R.id.cell_C1:
return new Point(0, 2);
case R.id.cell_C2:
return new Point(1, 2);
case R.id.cell_C3:
return new Point(2, 2);
default:
ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException = new ArrayIndexOutOfBoundsException();
Log.e(TAG, "viewToPoint: That's not supposed to happen!", arrayIndexOutOfBoundsException);
throw arrayIndexOutOfBoundsException;
}
}
static int pointToView(Point point) {
if (point.equals(0, 0)) {
return R.id.cell_A1;
} else if (point.equals(1, 0)) {
return R.id.cell_A2;
} else if (point.equals(2, 0)) {
return R.id.cell_A3;
} else if (point.equals(0, 1)) {
return R.id.cell_B1;
} else if (point.equals(1, 1)) {
return R.id.cell_B2;
} else if (point.equals(2, 1)) {
return R.id.cell_B3;
} else if (point.equals(0, 2)) {
return R.id.cell_C1;
} else if (point.equals(1, 2)) {
return R.id.cell_C2;
} else if (point.equals(2, 2)) {
return R.id.cell_C3;
} else {
ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException = new ArrayIndexOutOfBoundsException();
Log.e(TAG, "pointToView: That's not supposed to happen!", arrayIndexOutOfBoundsException);
throw arrayIndexOutOfBoundsException;
}
}
}
package com.example.tictactoe;
import android.util.Log;
import io.reactivex.Observable;
import io.reactivex.subjects.PublishSubject;
import java.util.List;
public class Game {
private static final String TAG = Game.class.getSimpleName();
private List<Player> mPlayers;
private Player mCurrentPlayer;
private Board mBoard;
private PublishSubject<Update> mBoardUpdates;
Game(List<Player> players) {
mPlayers = players;
mCurrentPlayer = players.get(0); // Start with Player 1
mBoard = new Board();
mBoardUpdates = PublishSubject.create();
}
void selectSquare(int x, int y) {
Log.d(TAG, String.format("selectSquare: {x: %1d, y: %2d}", x, y));
boolean successfulMove = mBoard.markCell(mCurrentPlayer.getMark(), x, y);
// TODO Check for winning move / draw
// If move is successful
if (successfulMove) {
// Post game update
mBoardUpdates.onNext(new Update(mCurrentPlayer, x, y, false, false));
// Next player's turn
mCurrentPlayer = mCurrentPlayer.equals(mPlayers.get(0)) ? mPlayers.get(1) : mPlayers.get(0);
}
}
Observable<Update> getGameUpdates() {
return mBoardUpdates;
}
}
package com.example.tictactoe;
import android.graphics.Point;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@BindView(R.id.toolbar)
Toolbar mToolbar;
private Game mGame;
private Disposable mGameUpdates;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
// Start new game
onNewGame();
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
@Override
protected void onDestroy() {
mGameUpdates.dispose();
super.onDestroy();
}
@Override
......@@ -37,9 +53,6 @@ public class MainActivity extends AppCompatActivity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
......@@ -49,4 +62,77 @@ public class MainActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item);
}
/**
* Handle click events of the {@link FloatingActionButton}
*
* @param view view that sent the event
*/
@OnClick(R.id.fab)
void onFabClicked(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
private void onNewGame() {
// TODO Prompt for player entry & mark selection
ArrayList<Player> players = new ArrayList<>();
players.add(new Player("Shaun", Mark.X));
players.add(new Player("Guest", Mark.O));
mGame = new Game(players);
mGameUpdates = mGame.getGameUpdates().subscribe(this::onNext, this::onError, this::onComplete);
}
// TODO See if I can move these to a field w/ ButterKnife
@OnClick({R.id.cell_A1, R.id.cell_A2, R.id.cell_A3,
R.id.cell_B1, R.id.cell_B2, R.id.cell_B3,
R.id.cell_C1, R.id.cell_C2, R.id.cell_C3})
void onBoardTapped(View view) {
try {
Point point = BoardMapper.viewToPoint(view.getId());
mGame.selectSquare(point.x, point.y);
} catch (Exception e) {
Toast.makeText(this, "Invalid Move!", Toast.LENGTH_SHORT).show();
}
}
// GAME UPDATES
private void onNext(Update update) {
checkForEndScenrio(update);
Point move = update.getMove();
Mark mark = update.getPlayer().getMark();
updateBoardView(move, mark);
}
private void checkForEndScenrio(Update update) {
// Check for winning move
if (update.isWinner()) {
// TODO Dialog
Toast.makeText(this, "We have a DRAW!!", Toast.LENGTH_LONG).show();
}
// Check for draw move
if (update.isDraw()) {
// TODO Dialog
String message = String.format("Player %1s has WON!!", update.getPlayer().getName());
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
private void onError(Throwable throwable) {
}
private void onComplete() {
}
private void updateBoardView(Point point, Mark mark) {
Log.d(TAG, String.format("updateBoardView: Move: %1s, Mark: %2s", point, mark));
int cellID = BoardMapper.pointToView(point);
((ImageView) findViewById(cellID)).setImageResource(mark.getValue());
}
}
package com.example.tictactoe;
public enum Mark {
X(R.drawable.ic_x),
O(R.drawable.ic_o),
EMPTY(0);
private final int id;
Mark(int id) {
this.id = id;
}
public int getValue() {
return id;
}
}
package com.example.tictactoe;
public class Player {
private String name;
private Mark mark;
public Player(String name, Mark mark) {
this.name = name;
this.mark = mark;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Mark getMark() {
return mark;
}
public void setMark(Mark mark) {
this.mark = mark;
}
}
package com.example.tictactoe;
import android.graphics.Point;
class Update {
private Player player;
private Point move;
private boolean isDraw;
private boolean isWinner;
Update(Player player, int x, int y, boolean isDraw, boolean isWinner) {
this.player = player;
this.move = new Point(x, y);
this.isDraw = isDraw;
this.isWinner = isWinner;
}
public Player getPlayer() {
return player;
}
public Point getMove() {
return move;
}
public boolean isDraw() {
return isDraw;
}
public boolean isWinner() {
return isWinner;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tictactoe.MainActivity">
<android.support.design.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tictactoe.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include layout="@layout/player_card_layout" />
<include layout="@layout/board_layout" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email"/>
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp">
<TableRow style="@style/Row">
<ImageView
android:id="@+id/cell_A1"
style="@style/Cell" />
<ImageView
android:id="@+id/cell_A2"
style="@style/Cell" />
<ImageView
android:id="@+id/cell_A3"
style="@style/Cell" />
</TableRow>
<TableRow style="@style/Row">
<ImageView
android:id="@+id/cell_B1"
style="@style/Cell" />
<ImageView
android:id="@+id/cell_B2"
style="@style/Cell" />
<ImageView
android:id="@+id/cell_B3"
style="@style/Cell" />
</TableRow>
<TableRow style="@style/Row">
<ImageView
android:id="@+id/cell_C1"
style="@style/Cell" />
<ImageView
android:id="@+id/cell_C2"
style="@style/Cell" />
<ImageView
android:id="@+id/cell_C3"
style="@style/Cell" />
</TableRow>
</TableLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="com.example.tictactoe.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"