From 9f850749c5380660bbec165db7d3cb2604e037f6 Mon Sep 17 00:00:00 2001 From: gumartinm Date: Tue, 4 Sep 2012 18:45:46 +0200 Subject: [PATCH] Improvement: retrieved flipped discs/ isValidMove --- src/de/android/reversi/Board.java | 125 +++++++++++++- src/de/android/reversi/CheckMovement.java | 226 ------------------------- src/de/android/reversi/FlippedDisc.java | 19 --- src/de/android/reversi/Movement.java | 28 --- src/de/android/reversi/Position.java | 21 +++ src/de/android/reversi/ReversiView.java | 38 ++--- src/de/android/reversi/logic/ReversiLogic.java | 33 +--- 7 files changed, 160 insertions(+), 330 deletions(-) delete mode 100644 src/de/android/reversi/CheckMovement.java delete mode 100644 src/de/android/reversi/FlippedDisc.java delete mode 100644 src/de/android/reversi/Movement.java create mode 100644 src/de/android/reversi/Position.java diff --git a/src/de/android/reversi/Board.java b/src/de/android/reversi/Board.java index 0697485..b84c358 100644 --- a/src/de/android/reversi/Board.java +++ b/src/de/android/reversi/Board.java @@ -1,8 +1,12 @@ package de.android.reversi; +import java.util.ArrayList; import java.util.List; public class Board { + private final static short [][] directions = { { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, + { 0, -1 }, { -1, -1 }, { -1, 0 }, { -1, -1 } }; + public static final short NUMBER_OF_COLUMNS = 8; public static final short NUMBER_OF_ROWS = 8; public static final short TOP_MARGIN = 0; @@ -24,26 +28,28 @@ public class Board { } } - public void updateBoard(final Player player, final short column, final short row) { - this.updateBoard(player, column, row, false); + public void makeMove(final Player player, final short column, final short row) { + this.makeMove(player, column, row, false); } - public void updateBoard(final Player player, final short column, final short row, + public void makeMove(final Player player, final short column, final short row, final boolean suggestion) { gameBoard[column][row].setPlayer(player); gameBoard[column][row].setSuggestion(suggestion); } - public void removeSuggestionsFromBoard(final List list) { - for (final Movement movement : list) { - this.updateBoard(Player.NOPLAYER, movement.getColumn(), movement.getRow()); + public void removeSuggestionsFromBoard(final List list) { + for (final Position movement : list) { + this.makeMove(Player.NOPLAYER, movement.getColumn(), movement.getRow()); } } - public void flipOpponentDiscs(final Movement movement, final Player currentPlayer) { - for (final FlippedDisc flippedDisc : movement.getFlippedDiscs()) { - this.updateBoard(currentPlayer, flippedDisc.getColumn(), flippedDisc.getRow()); + public void flipOpponentDiscs(final Position movement, final Player currentPlayer) { + final List flippedDiscs = flippedDiscs(currentPlayer, movement.getRow(), + movement.getColumn()); + for (final Position flippedDisc : flippedDiscs) { + this.makeMove(currentPlayer, flippedDisc.getColumn(), flippedDisc.getRow()); } } @@ -112,6 +118,107 @@ public class Board { squareWidth = (width - Board.LEFT_MARGIN * 2) / Board.NUMBER_OF_COLUMNS; } + public final boolean empty(final short column, final short row) { + if (gameBoard[column][row].getPlayer() == Player.NOPLAYER) { + return true; + } + return false; + } + + public final boolean isValidMove (final Player player, final short row, final short column) { + for (int i = 0; i < directions.length; i++) { + if (isValidMove (directions[i][0], directions[i][1], player, + (short)(row + directions[i][1]), (short)(column + directions[i][0])) > 0) { + return true; + } + } + + return false; + } + + public final List flippedDiscs (final Player player, final short row, final short column) { + final List flippedDiscs = new ArrayList(); + List aux; + + for (int i = 0; i < directions.length; i++) { + aux = flippedDiscs (directions[i][0], directions[i][1], player, + (short)(row + directions[i][1]), (short)(column + directions[i][0])); + if (!aux.isEmpty()) { + flippedDiscs.addAll(aux); + } + } + + return flippedDiscs; + } + + public final List allowedPositions(final Player player) { + final List list = new ArrayList(); + + for (short column = 0; column < Board.NUMBER_OF_COLUMNS; column++) { + for (short row = 0; row < Board.NUMBER_OF_ROWS; row++) { + if (empty(column, row)) { + if (isValidMove(player, row, column)) { + list.add(new Position(row, column)); + } + } + } + } + + return list; + } + + private final int isValidMove (final short moveX, final short moveY, final Player player, + short row, short column) { + int flippedDiscs = 0; + boolean match = false; + + while (row >= 0 && column >= 0 && + row < Board.NUMBER_OF_ROWS && column < Board.NUMBER_OF_COLUMNS && + !empty(column, row)) { + + if (gameBoard[column][row].getPlayer() == player) { + match = true; + break; + } + + flippedDiscs++; + column = (short)(column + moveX); + row = (short)(row + moveY); + } + + if (!match) { + flippedDiscs = 0; + } + + return flippedDiscs; + } + + private final List flippedDiscs(final short moveX, final short moveY, + final Player player, short row, short column) { + final List flippedDiscs = new ArrayList(); + boolean match = false; + + while (row > 0 && column > 0 && + row < Board.NUMBER_OF_ROWS && column < Board.NUMBER_OF_COLUMNS && + !empty(column, row)) { + + if (gameBoard[column][row].getPlayer() == player) { + match = true; + break; + } + + flippedDiscs.add(new Position(row, column)); + column = (short)(column + moveX); + row = (short)(row + moveY); + } + + if (!match) { + flippedDiscs.clear(); + } + + return flippedDiscs; + } + /*** Getters ***/ diff --git a/src/de/android/reversi/CheckMovement.java b/src/de/android/reversi/CheckMovement.java deleted file mode 100644 index 9619918..0000000 --- a/src/de/android/reversi/CheckMovement.java +++ /dev/null @@ -1,226 +0,0 @@ -package de.android.reversi; - -import java.util.ArrayList; -import java.util.List; - -public final class CheckMovement { - public static boolean horizontal(final Square gameBoard[][], final Movement movement, - final Player player) { - final boolean outflankRight = outflankRight(gameBoard, movement, player); - final boolean outflankLeft = outflankLeft(gameBoard, movement, player); - - return outflankRight || outflankLeft; - } - - public static boolean vertical(final Square gameBoard[][], final Movement movement, - final Player player) { - final boolean outflankUp = outflankUp(gameBoard, movement, player); - final boolean outflankDown = outflankDown(gameBoard, movement, player); - - return outflankUp || outflankDown; - } - - public static boolean diagonal(final Square gameBoard[][], final Movement movement, - final Player player) { - final boolean outflankDiagonalLeftUp = outflankDiagonalLeftUp(gameBoard, movement, player); - final boolean outflankDiagonalRightDown = outflankDiagonalRightDown(gameBoard, movement, player); - final boolean outflankDiagonalRightUp = outflankDiagonalRightUp(gameBoard, movement, player); - final boolean outflankDiagonalLeftDown = outflankDiagonalLeftDown(gameBoard, movement, player); - - return outflankDiagonalLeftUp || outflankDiagonalRightDown || outflankDiagonalRightUp || - outflankDiagonalLeftDown; - } - - public static boolean empty(final Square gameBoard[][], final short column, final short row) { - if (gameBoard[column][row].getPlayer() == Player.NOPLAYER) { - return true; - } - return false; - } - - private static boolean outflankUp (final Square gameBoard[][], - final Movement movement, final Player player) { - final short row = movement.getRow(); - final short column = movement.getColumn(); - - //Precondition: - if (row <= 1) { - return false; - } - - final List flippedDiscs = outflank((short)0, (short)-1, gameBoard, - player, (short)(row-1), column); - if (flippedDiscs.isEmpty()) { - return false; - } - - movement.getFlippedDiscs().addAll(flippedDiscs); - return true; - } - - private static boolean outflankDown (final Square gameBoard[][], - final Movement movement, final Player player) { - final short row = movement.getRow(); - final short column = movement.getColumn(); - - //Precondition: - if (row >= Board.NUMBER_OF_ROWS -2) { - return false; - } - - final List flippedDiscs = outflank((short)0, (short)1, gameBoard, - player, (short)(row+1), column); - if (flippedDiscs.isEmpty()) { - return false; - } - - movement.getFlippedDiscs().addAll(flippedDiscs); - return true; - } - - private static boolean outflankRight (final Square gameBoard[][], - final Movement movement, final Player player) { - final short row = movement.getRow(); - final short column = movement.getColumn(); - - //Precondition: - if (column >= Board.NUMBER_OF_COLUMNS -2) { - return false; - } - - final List flippedDiscs = outflank((short)1, (short)0, gameBoard, - player, row, (short)(column+1)); - if (flippedDiscs.isEmpty()) { - return false; - } - - movement.getFlippedDiscs().addAll(flippedDiscs); - return true; - } - - private static boolean outflankLeft (final Square gameBoard[][], - final Movement movement, final Player player) { - final short row = movement.getRow(); - final short column = movement.getColumn(); - - //Precondition: - if (column <= 1) { - return false; - } - - final List flippedDiscs = outflank((short)-1, (short)0, gameBoard, - player, row, (short)(column-1)); - if (flippedDiscs.isEmpty()) { - return false; - } - - movement.getFlippedDiscs().addAll(flippedDiscs); - return true; - } - - private static boolean outflankDiagonalLeftUp (final Square gameBoard[][], - final Movement movement, final Player player) { - final short row = movement.getRow(); - final short column = movement.getColumn(); - - //Precondition: - if (column <= 1 || row <= 1) { - return false; - } - - final List flippedDiscs = outflank((short)-1, (short)-1, gameBoard, - player, (short)(row-1), (short)(column-1)); - if (flippedDiscs.isEmpty()) { - return false; - } - - movement.getFlippedDiscs().addAll(flippedDiscs); - return true; - } - - private static boolean outflankDiagonalRightDown (final Square gameBoard[][], - final Movement movement, final Player player) { - final short row = movement.getRow(); - final short column = movement.getColumn(); - - //Precondition: - if (column >= (Board.NUMBER_OF_COLUMNS -2) || row >= (Board.NUMBER_OF_ROWS -2)) { - return false; - } - - final List flippedDiscs = outflank((short)1, (short)1, gameBoard, - player, (short)(row+1), (short)(column+1)); - if (flippedDiscs.isEmpty()) { - return false; - } - - movement.getFlippedDiscs().addAll(flippedDiscs); - return true; - } - - private static boolean outflankDiagonalLeftDown (final Square gameBoard[][], - final Movement movement, final Player player) { - final short row = movement.getRow(); - final short column = movement.getColumn(); - - //Precondition: - if (column <= 1 || row >= (Board.NUMBER_OF_ROWS -2)) { - return false; - } - - final List flippedDiscs = outflank((short)-1, (short)1, gameBoard, player, - (short)(row+1), (short)(column-1)); - if (flippedDiscs.isEmpty()) { - return false; - } - - movement.getFlippedDiscs().addAll(flippedDiscs); - return true; - } - - private static boolean outflankDiagonalRightUp (final Square gameBoard[][], - final Movement movement, final Player player) { - final short row = movement.getRow(); - final short column = movement.getColumn(); - - //Precondition: - if (row <= 1 || column >= (Board.NUMBER_OF_COLUMNS -2)) { - return false; - } - - final List flippedDiscs = outflank((short)1, (short)-1, gameBoard, player, - (short)(row-1), (short)(column+1)); - if (flippedDiscs.isEmpty()) { - return false; - } - - movement.getFlippedDiscs().addAll(flippedDiscs); - return true; - } - - private static List outflank(final short moveX, final short moveY, - final Square gameBoard[][], final Player player, short row, short column) { - final List flippedDiscs = new ArrayList(); - boolean match = false; - - while (row > 0 && column > 0 && - row < Board.NUMBER_OF_ROWS && column < Board.NUMBER_OF_COLUMNS && - !empty(gameBoard, column, row)) { - - if (gameBoard[column][row].getPlayer() == player) { - match = true; - break; - } - - flippedDiscs.add(new FlippedDisc(row, column)); - column = (short)(column + moveX); - row = (short)(row + moveY); - } - - if (!match) { - flippedDiscs.clear(); - } - - return flippedDiscs; - } -} diff --git a/src/de/android/reversi/FlippedDisc.java b/src/de/android/reversi/FlippedDisc.java deleted file mode 100644 index 5348066..0000000 --- a/src/de/android/reversi/FlippedDisc.java +++ /dev/null @@ -1,19 +0,0 @@ -package de.android.reversi; - -public class FlippedDisc { - private final short row; - private final short column; - - public FlippedDisc(final short row, final short column) { - this.row = row; - this.column = column; - } - - public short getRow() { - return row; - } - - public short getColumn() { - return column; - } -} diff --git a/src/de/android/reversi/Movement.java b/src/de/android/reversi/Movement.java deleted file mode 100644 index f0bb72c..0000000 --- a/src/de/android/reversi/Movement.java +++ /dev/null @@ -1,28 +0,0 @@ -package de.android.reversi; - -import java.util.ArrayList; -import java.util.List; - - -public class Movement { - private final short row; - private final short column; - private final List flippedDiscs = new ArrayList(); - - public List getFlippedDiscs() { - return flippedDiscs; - } - - public Movement(final short row, final short column) { - this.row = row; - this.column = column; - } - - public short getRow() { - return row; - } - - public short getColumn() { - return column; - } -} diff --git a/src/de/android/reversi/Position.java b/src/de/android/reversi/Position.java new file mode 100644 index 0000000..9757cdd --- /dev/null +++ b/src/de/android/reversi/Position.java @@ -0,0 +1,21 @@ +package de.android.reversi; + + + +public class Position { + private final short row; + private final short column; + + public Position(final short row, final short column) { + this.row = row; + this.column = column; + } + + public short getRow() { + return row; + } + + public short getColumn() { + return column; + } +} diff --git a/src/de/android/reversi/ReversiView.java b/src/de/android/reversi/ReversiView.java index 900cf8f..a998e2b 100644 --- a/src/de/android/reversi/ReversiView.java +++ b/src/de/android/reversi/ReversiView.java @@ -25,7 +25,7 @@ public class ReversiView extends SurfaceView { private volatile Player currentPlayer = Player.PLAYER1; private volatile boolean isEnableUserTouch; - private List listAllowedMovements; + private List listAllowedPositions; public ReversiView(final Context context) { @@ -86,18 +86,18 @@ public class ReversiView extends SurfaceView { @Override public void surfaceCreated(final SurfaceHolder holder) { //White - board.updateBoard(Player.PLAYER1, (short)3, (short)4); - board.updateBoard(Player.PLAYER1, (short)4, (short)3); + board.makeMove(Player.PLAYER1, (short)3, (short)4); + board.makeMove(Player.PLAYER1, (short)4, (short)3); //Black - board.updateBoard(Player.PLAYER2, (short)4, (short)4); - board.updateBoard(Player.PLAYER2, (short)3, (short)3); + board.makeMove(Player.PLAYER2, (short)4, (short)4); + board.makeMove(Player.PLAYER2, (short)3, (short)3); - //AllowedMovements for Player - listAllowedMovements = ReversiLogic.allowedMovements(currentPlayer, board.getGameBoard()); + //AllowedPositions for Player + listAllowedPositions = board.allowedPositions(currentPlayer); //UpdateBoard with suggestions - for (final Movement movement : listAllowedMovements) { - board.updateBoard(currentPlayer, movement.getColumn(), movement.getRow(), true); + for (final Position movement : listAllowedPositions) { + board.makeMove(currentPlayer, movement.getColumn(), movement.getRow(), true); } } @@ -132,10 +132,10 @@ public class ReversiView extends SurfaceView { final short row = board.transformCoordinateYInRow(event.getY()); if (row != -1 && column != -1 ) { - Movement movement; - if((movement = ReversiLogic.retrieveAllowedMovement(row, column, - listAllowedMovements)) != null) { - board.removeSuggestionsFromBoard(listAllowedMovements); + Position movement; + if((movement = ReversiLogic.retrieveAllowedPosition(row, column, + listAllowedPositions)) != null) { + board.removeSuggestionsFromBoard(listAllowedPositions); this.mainLoop(column, row, movement); } } @@ -212,9 +212,9 @@ public class ReversiView extends SurfaceView { ((TextView)((Activity)this.context).findViewById(R.id.txtPlayer2Score)).setText(String.format(" %d %s", player2Score, "discs")); } - private void mainLoop(final short column, final short row, final Movement movement) { + private void mainLoop(final short column, final short row, final Position movement) { - board.updateBoard(this.currentPlayer, column, row); + board.makeMove(this.currentPlayer, column, row); board.flipOpponentDiscs(movement, currentPlayer); //Switch player. @@ -222,12 +222,12 @@ public class ReversiView extends SurfaceView { if (this.currentPlayer != this.AI) { - //AllowedMovements for player. - listAllowedMovements = ReversiLogic.allowedMovements(currentPlayer, board.getGameBoard()); + //AllowedPositions for player. + listAllowedPositions = board.allowedPositions(currentPlayer); //UpdateBoard with suggestions - for (final Movement suggestedMovement : listAllowedMovements) { - board.updateBoard(currentPlayer, suggestedMovement.getColumn(), suggestedMovement.getRow(), true); + for (final Position suggestedPosition : listAllowedPositions) { + board.makeMove(currentPlayer, suggestedPosition.getColumn(), suggestedPosition.getRow(), true); } diff --git a/src/de/android/reversi/logic/ReversiLogic.java b/src/de/android/reversi/logic/ReversiLogic.java index 2ad5f9c..c8d4081 100644 --- a/src/de/android/reversi/logic/ReversiLogic.java +++ b/src/de/android/reversi/logic/ReversiLogic.java @@ -1,40 +1,15 @@ package de.android.reversi.logic; -import java.util.ArrayList; import java.util.List; -import de.android.reversi.Board; -import de.android.reversi.CheckMovement; -import de.android.reversi.Movement; import de.android.reversi.Player; -import de.android.reversi.Square; +import de.android.reversi.Position; public final class ReversiLogic { - public static List allowedMovements(final Player player, final Square gameBoard[][]) { - final List list = new ArrayList(); - - for (short column = 0; column < Board.NUMBER_OF_COLUMNS; column++) { - for (short row = 0; row < Board.NUMBER_OF_ROWS; row++) { - final Movement movement = new Movement(row, column); - if (CheckMovement.empty(gameBoard, column, row)) { - final boolean diagonal = CheckMovement.diagonal(gameBoard, movement, player); - final boolean horizontal = CheckMovement.horizontal(gameBoard, movement, player); - final boolean vertical = CheckMovement.vertical(gameBoard, movement, player); - - if(diagonal || horizontal || vertical) { - list.add(movement); - } - } - } - } - - return list; - } - - public static Movement retrieveAllowedMovement(final short row, final short column, - final List listAllowedMovements) { - for (final Movement movement : listAllowedMovements) { + public static Position retrieveAllowedPosition(final short row, final short column, + final List listAllowedPositions) { + for (final Position movement : listAllowedPositions) { if ((movement.getRow() == row) && (movement.getColumn() == column)) { return movement; } -- 2.1.4