Commit 3e717763 authored by Shaun Burch's avatar Shaun Burch 💻

#2 Update BoardMapper for pointToView, toggle between player's turns, start of...

#2 Update BoardMapper for pointToView, toggle between player's turns, start of UI update for game over.
parent f38545e9
Pipeline #22935564 passed with stages
in 7 minutes and 6 seconds
......@@ -3,57 +3,64 @@ 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 cellToPoint(int cellID) {
int x, y;
// Map cell to X,Y
static Point viewToPoint(int cellID) {
switch (cellID) {
case R.id.cell_A1:
x = 0;
y = 0;
break;
return new Point(0, 0);
case R.id.cell_A2:
x = 1;
y = 0;
break;
return new Point(1, 0);
case R.id.cell_A3:
x = 2;
y = 0;
break;
return new Point(2, 0);
case R.id.cell_B1:
x = 0;
y = 1;
break;
return new Point(0, 1);
case R.id.cell_B2:
x = 1;
y = 1;
break;
return new Point(1, 1);
case R.id.cell_B3:
x = 2;
y = 1;
break;
return new Point(2, 1);
case R.id.cell_C1:
x = 0;
y = 2;
break;
return new Point(0, 2);
case R.id.cell_C2:
x = 1;
y = 2;
break;
return new Point(1, 2);
case R.id.cell_C3:
x = 2;
y = 2;
break;
return new Point(2, 2);
default:
Log.wtf(TAG, "That's not supposed to happen!");
throw new ArrayIndexOutOfBoundsException();
ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException = new ArrayIndexOutOfBoundsException();
Log.e(TAG, "viewToPoint: That's not supposed to happen!", arrayIndexOutOfBoundsException);
throw arrayIndexOutOfBoundsException;
}
return new Point(x,y);
}
static int pointToCell(Point point) {
return R.id.cell_A3;
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;
}
}
}
......@@ -24,9 +24,15 @@ public class Game {
void selectSquare(int x, int y) {
Log.d(TAG, String.format("selectSquare: {x: %1d, y: %2d}", x, y));
// If move is successful, post game update
if (mBoard.markCell(mCurrentPlayer.getMark(), 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);
}
}
......
package com.example.tictactoe;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
......@@ -23,7 +22,8 @@ import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@BindView(R.id.toolbar) Toolbar mToolbar;
@BindView(R.id.toolbar)
Toolbar mToolbar;
private Game mGame;
private Disposable mGameUpdates;
......@@ -38,6 +38,12 @@ public class MainActivity extends AppCompatActivity {
onNewGame();
}
@Override
protected void onDestroy() {
mGameUpdates.dispose();
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
......@@ -59,6 +65,7 @@ public class MainActivity extends AppCompatActivity {
/**
* Handle click events of the {@link FloatingActionButton}
*
* @param view view that sent the event
*/
@OnClick(R.id.fab)
......@@ -77,32 +84,44 @@ public class MainActivity extends AppCompatActivity {
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) {
Point point;
try {
point = BoardMapper.cellToPoint(view.getId());
Point point = BoardMapper.viewToPoint(view.getId());
mGame.selectSquare(point.x, point.y);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
Toast.makeText(this, "Invalid Move!", Toast.LENGTH_SHORT).show();
return;
}
mGame.selectSquare(point.x, point.y);
}
// GAME UPDATES
private void onNext(Update update) {
checkForEndScenrio(update);
Point move = update.getMove();
Mark mark = update.getPlayer().getMark();
// TODO Check update
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) {
}
......@@ -111,9 +130,9 @@ public class MainActivity extends AppCompatActivity {
}
private void updateBoardView(Point move, Mark mark) {
Log.d(TAG, String.format("updateBoardView: Move: %1s, Mark: %2s", move, mark));
int cellID = BoardMapper.pointToCell(move);
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());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment