Improvement: retrieved flipped discs/ isValidMove
authorgumartinm <gustavo@gumartinm.name>
Tue, 4 Sep 2012 16:45:46 +0000 (18:45 +0200)
committergumartinm <gustavo@gumartinm.name>
Tue, 4 Sep 2012 16:45:46 +0000 (18:45 +0200)
src/de/android/reversi/Board.java
src/de/android/reversi/CheckMovement.java [deleted file]
src/de/android/reversi/FlippedDisc.java [deleted file]
src/de/android/reversi/Movement.java [deleted file]
src/de/android/reversi/Position.java [new file with mode: 0644]
src/de/android/reversi/ReversiView.java
src/de/android/reversi/logic/ReversiLogic.java

index 0697485..b84c358 100644 (file)
@@ -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<Movement> list) {
-        for (final Movement movement : list) {
-            this.updateBoard(Player.NOPLAYER, movement.getColumn(), movement.getRow());
+    public void removeSuggestionsFromBoard(final List<Position> 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<Position> 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<Position> flippedDiscs (final Player player, final short row, final short column) {
+        final List<Position> flippedDiscs = new ArrayList<Position>();
+        List<Position> 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<Position> allowedPositions(final Player player) {
+        final List<Position> list = new ArrayList<Position>();
+
+        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<Position> flippedDiscs(final short moveX, final short moveY,
+            final Player player, short row, short column) {
+        final List<Position> flippedDiscs = new ArrayList<Position>();
+        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 (file)
index 9619918..0000000
+++ /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<FlippedDisc> 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<FlippedDisc> 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<FlippedDisc> 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<FlippedDisc> 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<FlippedDisc> 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<FlippedDisc> 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<FlippedDisc> 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<FlippedDisc> 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<FlippedDisc> outflank(final short moveX, final short moveY,
-            final Square gameBoard[][], final Player player, short row, short column) {
-        final List<FlippedDisc> flippedDiscs = new ArrayList<FlippedDisc>();
-        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 (file)
index 5348066..0000000
+++ /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 (file)
index f0bb72c..0000000
+++ /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<FlippedDisc> flippedDiscs = new ArrayList<FlippedDisc>();
-
-    public List<FlippedDisc> 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 (file)
index 0000000..9757cdd
--- /dev/null
@@ -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;
+    }
+}
index 900cf8f..a998e2b 100644 (file)
@@ -25,7 +25,7 @@ public class ReversiView extends SurfaceView {
     private volatile Player currentPlayer = Player.PLAYER1;
     private volatile boolean isEnableUserTouch;
 
-    private List<Movement> listAllowedMovements;
+    private List<Position> 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);
             }
 
 
index 2ad5f9c..c8d4081 100644 (file)
@@ -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<Movement> allowedMovements(final Player player, final Square gameBoard[][]) {
-        final List<Movement> list = new ArrayList<Movement>();
-
-        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<Movement> listAllowedMovements) {
-        for (final Movement movement : listAllowedMovements) {
+    public static Position retrieveAllowedPosition(final short row, final short column,
+            final List<Position> listAllowedPositions) {
+        for (final Position movement : listAllowedPositions) {
             if ((movement.getRow() == row) && (movement.getColumn() == column)) {
                 return movement;
             }