From efae3b7e4792ddf8065bc0a43d9eeecb38b75cc4 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Fri, 18 Sep 2015 21:09:41 +0200 Subject: [PATCH] Minor code prettifying --- .../hdm_stuttgart/mi/sd1/tictactoe/Board.java | 76 ++++++++++++------- .../mi/sd1/tictactoe/TicTacToe.java | 13 ++-- .../mi/sd1/connectfour/ScoreTest.java | 4 +- 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/P/Sd1/TicTacToe/V4/src/main/java/de/hdm_stuttgart/mi/sd1/tictactoe/Board.java b/P/Sd1/TicTacToe/V4/src/main/java/de/hdm_stuttgart/mi/sd1/tictactoe/Board.java index f7023e55c..7d1982065 100644 --- a/P/Sd1/TicTacToe/V4/src/main/java/de/hdm_stuttgart/mi/sd1/tictactoe/Board.java +++ b/P/Sd1/TicTacToe/V4/src/main/java/de/hdm_stuttgart/mi/sd1/tictactoe/Board.java @@ -47,7 +47,8 @@ public class Board { Helper.excludeIndex(freeFields, i)); if (currentActivePlayer.score == score) { - completeMove(); + currentActivePlayer = currentActivePlayer.getOtherPlayer(); + numberOfMoves++; return; // Leave current field occupied by active player. } else { if (0 == score) { @@ -57,15 +58,12 @@ public class Board { board[freeFieldIndex] = null; // Reset field to empty state, continue searching. } board[drawOrLooseIndex] = currentActivePlayer; - completeMove(); - } - - private void completeMove() { // To be called after setting a field. numberOfMoves++; currentActivePlayer = currentActivePlayer.getOtherPlayer(); } - private int[] getFreeFields() { + + public int[] getFreeFields() { final int[] freeFields = new int[numberOfFields - numberOfMoves]; int index = 0; @@ -83,6 +81,7 @@ public class Board { } private int getScore(final Player[] evalBoard, final Player activePlayer, final int[] freeFields) { + { final Player winner = getWinner(evalBoard); @@ -93,9 +92,7 @@ public class Board { } } - int drawOrLooseScore = // Initial value: Opponent is winning - activePlayer.getOtherPlayer().score; - + boolean drawChildExists = false; for(int i = 0; i < freeFields.length; i++) { final int freeFieldIndex = freeFields[i]; @@ -108,10 +105,15 @@ public class Board { if (activePlayer.score == score) { // Maximum or minimum (with respect to active player) has been reached. return activePlayer.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(){ @@ -126,32 +128,32 @@ public class Board { static public Player getWinner(Player[] evalBoard) { if (null != evalBoard[4] && ( // Check both diagonals - (evalBoard[4] == evalBoard[0] && evalBoard[4] == evalBoard[8]) || // - (evalBoard[4] == evalBoard[6] && evalBoard[4] == evalBoard[2]))) { // \ / - return evalBoard[4]; // X - } // / \ - - for (int i = 0; i < fieldSize; i++) { // Check all three columns - if (null != evalBoard[i] && // | | | - evalBoard[i] == evalBoard[i + fieldSize] && // | | | - evalBoard[i] == evalBoard[i + 2* fieldSize]) { // | | | - return evalBoard[i]; + (evalBoard[4] == evalBoard[0] && evalBoard[4] == evalBoard[8]) || // + (evalBoard[4] == evalBoard[6] && evalBoard[4] == evalBoard[2]))) { // x x + return evalBoard[4]; // x + } // x x + + for (int i = 0; i < fieldSize; i++) { // Check all three columns + if (null != evalBoard[i] && // +-= + evalBoard[i] == evalBoard[i + fieldSize] && + evalBoard[i] == evalBoard[i + 2* fieldSize]) { // +-= + return evalBoard[i]; // +-= } } - for (int i = 0; i < 2 * fieldSize + 1; i += fieldSize) { // Check all three rows - if (null != evalBoard[i] && // --- - evalBoard[i] == evalBoard[i + 1] && evalBoard[i] == evalBoard[i + 2]) { // --- - return evalBoard[i]; // --- + for (int i = 0; i < 2 * fieldSize + 1; i += fieldSize) { // Check all three rows + if (null != evalBoard[i] && // +++ + evalBoard[i] == evalBoard[i + 1] && evalBoard[i] == evalBoard[i + 2]) { // --- + return evalBoard[i]; // === } } - return null; // No winner found. + return null; } /** * Occupy the next field by player {@link #getCurrentActivePlayer()}. * - * Fields are being numbered as: + * Fields are being numbered by: * * 1|2|3 * -+-+- @@ -159,7 +161,7 @@ public class Board { * -+-+- * 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. * null if everything is o.K. @@ -174,7 +176,8 @@ public class Board { final Player current = board[fieldIndex]; if (current == null) { board[fieldIndex] = currentActivePlayer; - completeMove(); + currentActivePlayer = currentActivePlayer.getOtherPlayer(); + numberOfMoves++; return null; } else { return "Field already occupied by " + current.nickname; @@ -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)} * diff --git a/P/Sd1/TicTacToe/V4/src/main/java/de/hdm_stuttgart/mi/sd1/tictactoe/TicTacToe.java b/P/Sd1/TicTacToe/V4/src/main/java/de/hdm_stuttgart/mi/sd1/tictactoe/TicTacToe.java index 8b5610a2c..04b4b462e 100644 --- a/P/Sd1/TicTacToe/V4/src/main/java/de/hdm_stuttgart/mi/sd1/tictactoe/TicTacToe.java +++ b/P/Sd1/TicTacToe/V4/src/main/java/de/hdm_stuttgart/mi/sd1/tictactoe/TicTacToe.java @@ -3,7 +3,7 @@ package de.hdm_stuttgart.mi.sd1.tictactoe; import java.util.Scanner; /** - * Tic-tac-toe human vs. computer. + * Playing Tic-tac-toe with two players. * */ public class TicTacToe { @@ -20,20 +20,16 @@ public class TicTacToe { System.out.println("Who is going to start? " + Player.YOU.ordinal() + " = " + Player.YOU.nickname + - ", " + Player.COM.ordinal() + " = " + Player.COM.nickname); + ", other = " + Player.COM.nickname); final int firstPlayer = scan.nextShort(); final Board board; if (Player.YOU.ordinal() == firstPlayer) { board = new Board(Player.YOU); - } else if (Player.COM.ordinal() == firstPlayer){ + } else { board = new Board(Player.COM); board.nextPerfectMove(); - } else { - board = null; - System.err.println("Wrong choice, you may re-start this program"); - System.exit(1); } board.print(true, true); @@ -54,13 +50,14 @@ public class TicTacToe { } while (null == (winner = board.evaluateWinner()) && !board.allMovesFinished()); if (null == winner) { - System.out.println("\n\nGame over: Draw"); + System.out.println("\n\nGame over: draw"); } else { switch(winner) { case YOU: System.out.println("\n\nCongratulations, you won!");break; case COM: System.out.println("\n\nSorry, you lost!");break; } } + scan.close(); } } diff --git a/P/Sd1/TicTacToe/V4/src/test/java/de/hdm_stuttgart/mi/sd1/connectfour/ScoreTest.java b/P/Sd1/TicTacToe/V4/src/test/java/de/hdm_stuttgart/mi/sd1/connectfour/ScoreTest.java index cf69f9741..4cf14dfe9 100644 --- a/P/Sd1/TicTacToe/V4/src/test/java/de/hdm_stuttgart/mi/sd1/connectfour/ScoreTest.java +++ b/P/Sd1/TicTacToe/V4/src/test/java/de/hdm_stuttgart/mi/sd1/connectfour/ScoreTest.java @@ -49,7 +49,7 @@ public class ScoreTest { public void testDraw1() { final Board board = new Board( - COM, // active player X. + COM, // active player. new Player[] { null, null, null, // | | @@ -58,7 +58,7 @@ public class ScoreTest { // -+-+- YOU, null, COM}); // O| |X - Assert.assertEquals(0, board.getScore()); + Assert.assertEquals(YOU.score, board.getScore()); } -- GitLab