Skip to content
Snippets Groups Projects
Commit efae3b7e authored by Goik Martin's avatar Goik Martin
Browse files

Minor code prettifying

parent 6b433fb2
No related branches found
No related tags found
No related merge requests found
...@@ -47,7 +47,8 @@ public class Board { ...@@ -47,7 +47,8 @@ public class Board {
Helper.excludeIndex(freeFields, i)); Helper.excludeIndex(freeFields, i));
if (currentActivePlayer.score == score) { if (currentActivePlayer.score == score) {
completeMove(); currentActivePlayer = currentActivePlayer.getOtherPlayer();
numberOfMoves++;
return; // Leave current field occupied by active player. return; // Leave current field occupied by active player.
} else { } else {
if (0 == score) { if (0 == score) {
...@@ -57,15 +58,12 @@ public class Board { ...@@ -57,15 +58,12 @@ public class Board {
board[freeFieldIndex] = null; // Reset field to empty state, continue searching. board[freeFieldIndex] = null; // Reset field to empty state, continue searching.
} }
board[drawOrLooseIndex] = currentActivePlayer; board[drawOrLooseIndex] = currentActivePlayer;
completeMove();
}
private void completeMove() { // To be called after setting a field.
numberOfMoves++; numberOfMoves++;
currentActivePlayer = currentActivePlayer.getOtherPlayer(); currentActivePlayer = currentActivePlayer.getOtherPlayer();
} }
private int[] getFreeFields() {
public int[] getFreeFields() {
final int[] freeFields = new int[numberOfFields - numberOfMoves]; final int[] freeFields = new int[numberOfFields - numberOfMoves];
int index = 0; int index = 0;
...@@ -83,6 +81,7 @@ public class Board { ...@@ -83,6 +81,7 @@ public class Board {
} }
private int getScore(final Player[] evalBoard, final Player activePlayer, final int[] freeFields) { private int getScore(final Player[] evalBoard, final Player activePlayer, final int[] freeFields) {
{ {
final Player winner = getWinner(evalBoard); final Player winner = getWinner(evalBoard);
...@@ -93,9 +92,7 @@ public class Board { ...@@ -93,9 +92,7 @@ public class Board {
} }
} }
int drawOrLooseScore = // Initial value: Opponent is winning boolean drawChildExists = false;
activePlayer.getOtherPlayer().score;
for(int i = 0; i < freeFields.length; i++) { for(int i = 0; i < freeFields.length; i++) {
final int freeFieldIndex = freeFields[i]; final int freeFieldIndex = freeFields[i];
...@@ -108,10 +105,15 @@ public class Board { ...@@ -108,10 +105,15 @@ public class Board {
if (activePlayer.score == score) { // Maximum or minimum (with respect to active player) has been reached. if (activePlayer.score == score) { // Maximum or minimum (with respect to active player) has been reached.
return activePlayer.score; return activePlayer.score;
} else if (0 == score) { } else if (0 == score) {
drawOrLooseScore = 0; // We found a draw. drawChildExists = true; // Continue searching for better (maximal or minimal) score.
} }
} }
return drawOrLooseScore;
if (drawChildExists) {
return 0; // There is at least one "draw" child to choose from.
} else {
return activePlayer.getOtherPlayer().score; // All board children score to the active player's enemy
}
} }
public Player evaluateWinner(){ public Player evaluateWinner(){
...@@ -126,32 +128,32 @@ public class Board { ...@@ -126,32 +128,32 @@ public class Board {
static public Player getWinner(Player[] evalBoard) { static public Player getWinner(Player[] evalBoard) {
if (null != evalBoard[4] && ( // Check both diagonals if (null != evalBoard[4] && ( // Check both diagonals
(evalBoard[4] == evalBoard[0] && evalBoard[4] == evalBoard[8]) || // (evalBoard[4] == evalBoard[0] && evalBoard[4] == evalBoard[8]) || //
(evalBoard[4] == evalBoard[6] && evalBoard[4] == evalBoard[2]))) { // \ / (evalBoard[4] == evalBoard[6] && evalBoard[4] == evalBoard[2]))) { // x x
return evalBoard[4]; // X return evalBoard[4]; // x
} // / \ } // x x
for (int i = 0; i < fieldSize; i++) { // Check all three columns for (int i = 0; i < fieldSize; i++) { // Check all three columns
if (null != evalBoard[i] && // | | | if (null != evalBoard[i] && // +-=
evalBoard[i] == evalBoard[i + fieldSize] && // | | | evalBoard[i] == evalBoard[i + fieldSize] &&
evalBoard[i] == evalBoard[i + 2* fieldSize]) { // | | | evalBoard[i] == evalBoard[i + 2* fieldSize]) { // +-=
return evalBoard[i]; return evalBoard[i]; // +-=
} }
} }
for (int i = 0; i < 2 * fieldSize + 1; i += fieldSize) { // Check all three rows for (int i = 0; i < 2 * fieldSize + 1; i += fieldSize) { // Check all three rows
if (null != evalBoard[i] && // --- if (null != evalBoard[i] && // +++
evalBoard[i] == evalBoard[i + 1] && evalBoard[i] == evalBoard[i + 2]) { // --- evalBoard[i] == evalBoard[i + 1] && evalBoard[i] == evalBoard[i + 2]) { // ---
return evalBoard[i]; // --- return evalBoard[i]; // ===
} }
} }
return null; // No winner found. return null;
} }
/** /**
* Occupy the next field by player {@link #getCurrentActivePlayer()}. * Occupy the next field by player {@link #getCurrentActivePlayer()}.
* *
* Fields are being numbered as: * Fields are being numbered by:
* *
* 1|2|3 * 1|2|3
* -+-+- * -+-+-
...@@ -159,7 +161,7 @@ public class Board { ...@@ -159,7 +161,7 @@ public class Board {
* -+-+- * -+-+-
* 7|8|9 * 7|8|9
* *
* @param field The field in question using user perspective numbering (1,...,9) * @param field The field in question
* *
* @return An error message indication either an occupied field or an index violation. * @return An error message indication either an occupied field or an index violation.
* null if everything is o.K. * null if everything is o.K.
...@@ -174,7 +176,8 @@ public class Board { ...@@ -174,7 +176,8 @@ public class Board {
final Player current = board[fieldIndex]; final Player current = board[fieldIndex];
if (current == null) { if (current == null) {
board[fieldIndex] = currentActivePlayer; board[fieldIndex] = currentActivePlayer;
completeMove(); currentActivePlayer = currentActivePlayer.getOtherPlayer();
numberOfMoves++;
return null; return null;
} else { } else {
return "Field already occupied by " + current.nickname; return "Field already occupied by " + current.nickname;
...@@ -182,6 +185,21 @@ public class Board { ...@@ -182,6 +185,21 @@ public class Board {
} }
} }
static public void printNumberingHint() {
System.out.println("Numbering scheme:\n");
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
System.out.print((char)('1' + x + y * fieldSize));
if (x < fieldSize - 1) {
System.out.print('|');
}
}
if (y < fieldSize - 1) {
System.out.println("\n-+-+-");
}
}
}
/** /**
* Get the player instance which will do the next move. See {@link #nextMove(short)} * Get the player instance which will do the next move. See {@link #nextMove(short)}
* *
......
...@@ -3,7 +3,7 @@ package de.hdm_stuttgart.mi.sd1.tictactoe; ...@@ -3,7 +3,7 @@ package de.hdm_stuttgart.mi.sd1.tictactoe;
import java.util.Scanner; import java.util.Scanner;
/** /**
* Tic-tac-toe human vs. computer. * Playing Tic-tac-toe with two players.
* *
*/ */
public class TicTacToe { public class TicTacToe {
...@@ -20,20 +20,16 @@ public class TicTacToe { ...@@ -20,20 +20,16 @@ public class TicTacToe {
System.out.println("Who is going to start? " + System.out.println("Who is going to start? " +
Player.YOU.ordinal() + " = " + Player.YOU.nickname + Player.YOU.ordinal() + " = " + Player.YOU.nickname +
", " + Player.COM.ordinal() + " = " + Player.COM.nickname); ", other = " + Player.COM.nickname);
final int firstPlayer = scan.nextShort(); final int firstPlayer = scan.nextShort();
final Board board; final Board board;
if (Player.YOU.ordinal() == firstPlayer) { if (Player.YOU.ordinal() == firstPlayer) {
board = new Board(Player.YOU); board = new Board(Player.YOU);
} else if (Player.COM.ordinal() == firstPlayer){ } else {
board = new Board(Player.COM); board = new Board(Player.COM);
board.nextPerfectMove(); board.nextPerfectMove();
} else {
board = null;
System.err.println("Wrong choice, you may re-start this program");
System.exit(1);
} }
board.print(true, true); board.print(true, true);
...@@ -54,13 +50,14 @@ public class TicTacToe { ...@@ -54,13 +50,14 @@ public class TicTacToe {
} while (null == (winner = board.evaluateWinner()) && !board.allMovesFinished()); } while (null == (winner = board.evaluateWinner()) && !board.allMovesFinished());
if (null == winner) { if (null == winner) {
System.out.println("\n\nGame over: Draw"); System.out.println("\n\nGame over: draw");
} else { } else {
switch(winner) { switch(winner) {
case YOU: System.out.println("\n\nCongratulations, you won!");break; case YOU: System.out.println("\n\nCongratulations, you won!");break;
case COM: System.out.println("\n\nSorry, you lost!");break; case COM: System.out.println("\n\nSorry, you lost!");break;
} }
} }
scan.close(); scan.close();
} }
} }
...@@ -49,7 +49,7 @@ public class ScoreTest { ...@@ -49,7 +49,7 @@ public class ScoreTest {
public void testDraw1() { public void testDraw1() {
final Board board = new Board( final Board board = new Board(
COM, // active player X. COM, // active player.
new Player[] { new Player[] {
null, null, null, // | | null, null, null, // | |
...@@ -58,7 +58,7 @@ public class ScoreTest { ...@@ -58,7 +58,7 @@ public class ScoreTest {
// -+-+- // -+-+-
YOU, null, COM}); // O| |X YOU, null, COM}); // O| |X
Assert.assertEquals(0, board.getScore()); Assert.assertEquals(YOU.score, board.getScore());
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment