Checkmovement and flip opponent discs working.
authorgumartinm <gustavo@gumartinm.name>
Fri, 31 Aug 2012 12:23:40 +0000 (14:23 +0200)
committergumartinm <gustavo@gumartinm.name>
Fri, 31 Aug 2012 12:23:40 +0000 (14:23 +0200)
src/de/android/reversi/CheckMovement.java
src/de/android/reversi/Movement.java
src/de/android/reversi/ReversiActivity.java
src/de/android/reversi/ReversiView.java

index dda95d7..b34ef64 100644 (file)
@@ -1,24 +1,27 @@
 package de.android.reversi;
 
+import java.util.ArrayList;
+import java.util.List;
+
 public final class CheckMovement {
-    public static boolean horizontal(final Square gameBoard[][], short column, short row,
+    public static boolean horizontal(final Square gameBoard[][], final Movement movement,
             final Player player) {
-        return right(gameBoard, column, row, player) ||
-                left(gameBoard, column, row, player);
+        return outflankRight(gameBoard, movement, player) ||
+                outflankLeft(gameBoard, movement, player);
     }
 
-    public static boolean vertical(final Square gameBoard[][], short column, short row,
+    public static boolean vertical(final Square gameBoard[][], final Movement movement,
             final Player player) {
-        return up(gameBoard, column, row, player) ||
-                down(gameBoard, column, row, player);
+        return outflankUp(gameBoard, movement, player) ||
+                outflankDown(gameBoard, movement, player);
     }
 
-    public static boolean diagonal(final Square gameBoard[][], short column, short row,
+    public static boolean diagonal(final Square gameBoard[][], final Movement movement,
             final Player player) {
-        return diagonalLeftUp(gameBoard, column, row, player) ||
-                diagonalRightDown(gameBoard, column, row, player) ||
-                diagonalRightUp(gameBoard, column, row, player) ||
-                diagonalLeftDown(gameBoard, column, row, player);
+        return outflankDiagonalLeftUp(gameBoard, movement, player) ||
+                outflankDiagonalRightDown(gameBoard, movement, player) ||
+                outflankDiagonalRightUp(gameBoard, movement, player) ||
+                outflankDiagonalLeftDown(gameBoard, movement, player);
     }
 
     public static boolean empty(final Square gameBoard[][], final short column, final short row) {
@@ -28,8 +31,10 @@ public final class CheckMovement {
         return false;
     }
 
-    private static boolean up (final Square gameBoard[][], short column, short row,
-            final Player player) {
+    private static boolean outflankUp (final Square gameBoard[][],
+            final Movement movement, final Player player) {
+        final short row = movement.getRow();
+        final short column = movement.getColumn();
 
         //Precondition 1.
         if  (row <= 1) {
@@ -42,11 +47,20 @@ public final class CheckMovement {
             return false;
         }
 
-        return outflank((short)0, (short)-1, gameBoard, player, (short)(row-2), column);
+        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 down (final Square gameBoard[][], short column, short row,
-            final Player player) {
+    private static boolean outflankDown (final Square gameBoard[][],
+            final Movement movement, final Player player) {
+        final short row = movement.getRow();
+        final short column = movement.getColumn();
 
         //Precondition 1:
         if  (row >= ReversiView.NUMBER_OF_ROWS -2) {
@@ -59,11 +73,20 @@ public final class CheckMovement {
             return false;
         }
 
-        return outflank((short)0, (short)1, gameBoard, player, (short)(row+2), column);
+        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 right (final Square gameBoard[][], short column, short row,
-            final Player player) {
+    private static boolean outflankRight (final Square gameBoard[][],
+            final Movement movement, final Player player) {
+        final short row = movement.getRow();
+        final short column = movement.getColumn();
 
         //Precondition 1:
         if  (column >= ReversiView.NUMBER_OF_COLUMNS -2) {
@@ -76,11 +99,20 @@ public final class CheckMovement {
             return false;
         }
 
-        return outflank((short)1, (short)0, gameBoard, player, row, (short)(column+2));
+        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 left (final Square gameBoard[][], short column, short row,
-            final Player player) {
+    private static boolean outflankLeft (final Square gameBoard[][],
+            final Movement movement, final Player player) {
+        final short row = movement.getRow();
+        final short column = movement.getColumn();
 
         //Precondition 1:
         if  (column <= 1) {
@@ -93,11 +125,20 @@ public final class CheckMovement {
             return false;
         }
 
-        return outflank((short)-1, (short)0, gameBoard, player, row, (short)(column-2));
+        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 diagonalLeftUp (final Square gameBoard[][], short column,
-            short row, final Player player) {
+    private static boolean outflankDiagonalLeftUp (final Square gameBoard[][],
+            final Movement movement, final Player player) {
+        final short row = movement.getRow();
+        final short column = movement.getColumn();
 
         //Precondition 1:
         if  (column <= 1 || row <= 1) {
@@ -110,12 +151,20 @@ public final class CheckMovement {
             return false;
         }
 
-        return outflank((short)-1, (short)-1, gameBoard, player, (short)(row-2),
-                (short)(column-2));
+        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 diagonalRightDown (final Square gameBoard[][], short column,
-            short row, final Player player) {
+    private static boolean outflankDiagonalRightDown (final Square gameBoard[][],
+            final Movement movement, final Player player) {
+        final short row = movement.getRow();
+        final short column = movement.getColumn();
 
         //Precondition 1:
         if  (column >= (ReversiView.NUMBER_OF_COLUMNS -2) || row >= (ReversiView.NUMBER_OF_ROWS -2)) {
@@ -128,12 +177,20 @@ public final class CheckMovement {
             return false;
         }
 
-        return outflank((short)1, (short)1, gameBoard, player, (short)(row+2),
-                (short)(column+2));
+        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 diagonalLeftDown (final Square gameBoard[][], short column,
-            short row, final Player player) {
+    private static boolean outflankDiagonalLeftDown (final Square gameBoard[][],
+            final Movement movement, final Player player) {
+        final short row = movement.getRow();
+        final short column = movement.getColumn();
 
         //Precondition 1:
         if  (column <= 1 || row >= (ReversiView.NUMBER_OF_ROWS -2)) {
@@ -146,12 +203,20 @@ public final class CheckMovement {
             return false;
         }
 
-        return outflank((short)1, (short)1, gameBoard, player, (short)(row+2),
-                (short)(column-2));
+        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 diagonalRightUp (final Square gameBoard[][], short column, short row,
-            final Player player) {
+    private static boolean outflankDiagonalRightUp (final Square gameBoard[][],
+            final Movement movement, final Player player) {
+        final short row = movement.getRow();
+        final short column = movement.getColumn();
 
         //Precondition 1:
         if  (row <= 1 || column >= (ReversiView.NUMBER_OF_COLUMNS -2)) {
@@ -164,22 +229,39 @@ public final class CheckMovement {
             return false;
         }
 
-        return outflank((short)1, (short)1, gameBoard, player, (short)(row-2),
-                (short)(column+2));
+        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 outflank(final short moveX, final short moveY,
-            final Square gameBoard[][], Player player,short row, short column) {
+    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 < ReversiView.NUMBER_OF_ROWS && column < ReversiView.NUMBER_OF_COLUMNS &&
+                gameBoard[column][row].getPlayer() != Player.NOPLAYER) {
 
-        do {
             if (gameBoard[column][row].getPlayer() == player) {
-                return true;
+                match = true;
+                break;
             }
-            row = (short)(row + moveX);
-            column = (short)(column + moveY);
-        }while (row > 0 && column > 0 &&
-                row < ReversiView.NUMBER_OF_ROWS && column < ReversiView.NUMBER_OF_COLUMNS);
 
-        return false;
+            flippedDiscs.add(new FlippedDisc(row, column));
+            column = (short)(column + moveX);
+            row = (short)(row + moveY);
+        }
+
+        if (!match) {
+            flippedDiscs.clear();
+        }
+
+        return flippedDiscs;
     }
 }
index 75a32df..f0bb72c 100644 (file)
@@ -1,10 +1,19 @@
 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(short row, short column) {
+    public Movement(final short row, final short column) {
         this.row = row;
         this.column = column;
     }
@@ -16,34 +25,4 @@ public class Movement {
     public short getColumn() {
         return column;
     }
-
-    @Override
-    public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + column;
-        result = prime * result + row;
-        return result;
-    }
-
-    @Override
-    public boolean equals(final Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (obj == null) {
-            return false;
-        }
-        if (!(obj instanceof Movement)) {
-            return false;
-        }
-        Movement other = (Movement) obj;
-        if (column != other.column) {
-            return false;
-        }
-        if (row != other.row) {
-            return false;
-        }
-        return true;
-    }
 }
index fce5e08..d1e65b2 100644 (file)
@@ -7,16 +7,14 @@ import android.view.Menu;
 public class ReversiActivity extends Activity {
 
     @Override
-    public void onCreate(Bundle savedInstanceState) {
+    public void onCreate(final Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_reversi);
     }
 
     @Override
-    public boolean onCreateOptionsMenu(Menu menu) {
+    public boolean onCreateOptionsMenu(final Menu menu) {
         getMenuInflater().inflate(R.menu.activity_reversi, menu);
         return true;
     }
-
-
 }
index 9bdee78..566bca3 100644 (file)
@@ -111,11 +111,11 @@ public class ReversiView extends SurfaceView {
             @Override
             public void surfaceCreated(final SurfaceHolder holder) {
                 //White
-                updateBoard(Player.PLAYER1, (short)3, (short)3);
-                updateBoard(Player.PLAYER1, (short)4, (short)4);
+                updateBoard(Player.PLAYER1, (short)3, (short)4);
+                updateBoard(Player.PLAYER1, (short)4, (short)3);
                 //Black
-                updateBoard(Player.PLAYER2, (short)4, (short)3);
-                updateBoard(Player.PLAYER2, (short)3, (short)4);
+                updateBoard(Player.PLAYER2, (short)4, (short)4);
+                updateBoard(Player.PLAYER2, (short)3, (short)3);
 
                 //AllowedMovements for Player
                 listAllowedMovements = allowedMovements(currentPlayer, gameBoard);
@@ -157,9 +157,11 @@ public class ReversiView extends SurfaceView {
             final short row = transformCoordinateYInRow(event.getY());
 
             if (row != -1 && column != -1 ) {
-                if (this.isAllowedMovement(new Movement(row, column))) {
+                Movement movement;
+                if((movement = retrieveAllowedMovement(row, column)) != null) {
                     removeSuggestionsFromBoard(gameBoard, listAllowedMovements);
                     updateBoard(this.currentPlayer, column, row);
+                    flipOpponentDiscs(gameBoard, movement, currentPlayer);
                     this.mainLoop();
                 }
             }
@@ -320,11 +322,12 @@ public class ReversiView extends SurfaceView {
 
         for (short column = 0; column < NUMBER_OF_COLUMNS; column++) {
             for (short row = 0; row < NUMBER_OF_ROWS; row++) {
+                final Movement movement = new Movement(row, column);
                 if (CheckMovement.empty(gameBoard, column, row) &&
-                        (CheckMovement.diagonal(gameBoard, column, row, player) ||
-                                CheckMovement.horizontal(gameBoard, column, row, player) ||
-                                CheckMovement.vertical(gameBoard, column, row, player))) {
-                    list.add(new Movement(row, column));
+                        (CheckMovement.diagonal(gameBoard, movement, player) ||
+                                CheckMovement.horizontal(gameBoard, movement, player) ||
+                                CheckMovement.vertical(gameBoard, movement, player))) {
+                    list.add(movement);
                 }
             }
         }
@@ -332,14 +335,14 @@ public class ReversiView extends SurfaceView {
         return list;
     }
 
-    private boolean isAllowedMovement(final Movement movement) {
-        for (final Movement iterator : listAllowedMovements) {
-            if (iterator.equals(movement)) {
-                return true;
+    private Movement retrieveAllowedMovement(final short row, final short column) {
+        for (final Movement movement : listAllowedMovements) {
+            if ((movement.getRow() == row) && (movement.getColumn() == column)) {
+                return movement;
             }
         }
 
-        return false;
+        return null;
     }
 
     private void removeSuggestionsFromBoard(final Square gameBoard[][],
@@ -350,7 +353,7 @@ public class ReversiView extends SurfaceView {
         }
     }
 
-    private Player opponent(Player currentPlayer) {
+    private Player opponent(final Player currentPlayer) {
         switch (currentPlayer){
             case PLAYER1:
                 return Player.PLAYER2;
@@ -360,4 +363,10 @@ public class ReversiView extends SurfaceView {
                 return Player.NOPLAYER;
         }
     }
+
+    private void flipOpponentDiscs(final Square gameBoard[][], final Movement movement, final Player currentPlayer) {
+        for (final FlippedDisc flippedDisc : movement.getFlippedDiscs()) {
+            updateBoard(currentPlayer, flippedDisc.getColumn(), flippedDisc.getRow());
+        }
+    }
 }