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) {
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) {
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) {
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) {
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) {
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) {
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)) {
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)) {
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)) {
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;
}
}
@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);
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();
}
}
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);
}
}
}
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[][],
}
}
- private Player opponent(Player currentPlayer) {
+ private Player opponent(final Player currentPlayer) {
switch (currentPlayer){
case PLAYER1:
return Player.PLAYER2;
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());
+ }
+ }
}