Commit 8dd6d4d6 authored by Shaun Burch's avatar Shaun Burch 💻

#4 Holy shit it worked. Add winning Scenario & Draw detection.

parent 34620e74
Pipeline #22938991 passed with stages
in 7 minutes and 45 seconds
......@@ -6,6 +6,8 @@ import io.reactivex.subjects.PublishSubject;
import java.util.List;
import static com.example.tictactoe.Scenarios.*;
public class Game {
private static final String TAG = Game.class.getSimpleName();
......@@ -21,23 +23,73 @@ public class Game {
mBoardUpdates = PublishSubject.create();
}
Observable<Update> getGameUpdates() {
return mBoardUpdates;
}
void selectSquare(int x, int y) {
Log.d(TAG, String.format("selectSquare: {x: %1d, y: %2d}", x, y));
boolean success = mBoard.markCell(mCurrentPlayer.getMark(), x, y);
// Exit if unsuccessful
if (!success) {
return;
}
// Update
mBoardUpdates.onNext(new Update(mCurrentPlayer, x, y, checkForDraw(), checkForWinner()));
nextTurn();
}
private void nextTurn() {
mCurrentPlayer = mCurrentPlayer.equals(mPlayers.get(0)) ? mPlayers.get(1) : mPlayers.get(0);
}
boolean successfulMove = mBoard.markCell(mCurrentPlayer.getMark(), x, y);
// TODO Check for winning move / draw
private boolean checkForWinner() {
Mark[][] cells = mBoard.getCells();
// 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);
// Check X & Y for 3 in a row
for (int i = 0; i <= 2; i++) {
if (threeHorizontal(cells, i)) {
return true;
}
if (threeVertical(cells, i)) {
return true;
}
}
//Check down diagonal [\] for 3 in a row
if (threeDiagonalDown(cells)) {
return true;
}
// Check up diagonal [/] for 3 in a row
if (threeDiagonalUp(cells)) {
return true;
}
// Otherwise
return false;
}
Observable<Update> getGameUpdates() {
return mBoardUpdates;
private boolean checkForDraw() {
Mark[][] cells = mBoard.getCells();
boolean foundEmpty = false;
// Scan for Empty, exiting at first one.
loop:
for (int x = 0; x <= 2; x++) {
for (int y = 0; y <= 2; y++) {
if (cells[y][x] == Mark.EMPTY) {
foundEmpty = true;
break loop;
}
}
}
// True if there were no Empty's found.
return !foundEmpty;
}
}
......@@ -100,25 +100,26 @@ public class MainActivity extends AppCompatActivity {
// GAME UPDATES
private void onNext(Update update) {
checkForEndScenrio(update);
checkForGameOver(update);
// Update Board
Point move = update.getMove();
Mark mark = update.getPlayer().getMark();
updateBoardView(move, mark);
}
private void checkForEndScenrio(Update update) {
// Check for winning move
private void checkForGameOver(Update update) {
// TODO Disable board on win
// Check for draw move
if (update.isWinner()) {
// TODO Dialog
Toast.makeText(this, "We have a DRAW!!", Toast.LENGTH_LONG).show();
String message = String.format("Player %1s has WON!!", update.getPlayer().getName());
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
// Check for draw move
// Check for winning 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();
Toast.makeText(this, "We have a DRAW!!", Toast.LENGTH_LONG).show();
}
}
......
package com.example.tictactoe;
import android.util.Log;
import java.util.Arrays;
public class Scenarios {
private static final String TAG = Scenarios.class.getSimpleName();
public static boolean threeHorizontal(Mark[][] cells, int i) {
Log.d(TAG, String.format("threeHorizontal: %1s", Arrays.toString(cells)));
return (cells[i][0] == cells[i][1]) && (cells[i][1] == cells[i][2]) // Row matches
&& (cells[i][0] != Mark.EMPTY); // But isn't Empty
}
public static boolean threeVertical(Mark[][] cells, int i) {
Log.d(TAG, String.format("threeVertical: %1s", Arrays.toString(cells)));
return (cells[0][i] == cells[1][i]) && (cells[1][i] == cells[2][i]) // Column matches
&& (cells[0][i] != Mark.EMPTY); // But isn't Empty;
}
public static boolean threeDiagonalUp(Mark[][] cells) {
Log.d(TAG, String.format("threeDiagonalUp: %1s", Arrays.toString(cells)));
return (cells[0][2] == cells[1][1]) && (cells[1][1] == cells[2][0]) // Diagonal matches
&& (cells[0][2] != Mark.EMPTY); // But isn't Empty;
}
public static boolean threeDiagonalDown(Mark[][] cells) {
Log.d(TAG, String.format("threeDiagonalDown: %1s", Arrays.toString(cells)));
return (cells[0][0] == cells[1][1]) && (cells[1][1] == cells[2][2]) // Diagonal matches
&& (cells[0][0] != Mark.EMPTY); // But isn't Empty;
}
}
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