From bdd27195bc292a2572509310e57319524229e644 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Thu, 30 Aug 2012 03:23:17 +0200 Subject: [PATCH] Checkmovement, empty squares detection. --- src/de/android/reversi/CheckMovement.java | 93 ++++++++++++++++++------------- src/de/android/reversi/ReversiView.java | 9 ++- 2 files changed, 60 insertions(+), 42 deletions(-) diff --git a/src/de/android/reversi/CheckMovement.java b/src/de/android/reversi/CheckMovement.java index c1dc0c0..dda95d7 100644 --- a/src/de/android/reversi/CheckMovement.java +++ b/src/de/android/reversi/CheckMovement.java @@ -1,24 +1,35 @@ package de.android.reversi; -public class CheckMovement { - public static boolean horizontal(Square gameBoard[][], short column, short row, Player player) { +public final class CheckMovement { + public static boolean horizontal(final Square gameBoard[][], short column, short row, + final Player player) { return right(gameBoard, column, row, player) || left(gameBoard, column, row, player); } - public static boolean vertical(Square gameBoard[][], short column, short row, Player player) { + public static boolean vertical(final Square gameBoard[][], short column, short row, + final Player player) { return up(gameBoard, column, row, player) || down(gameBoard, column, row, player); } - public static boolean diagonal(Square gameBoard[][], short column, short row, Player player) { + public static boolean diagonal(final Square gameBoard[][], short column, short row, + final Player player) { return diagonalLeftUp(gameBoard, column, row, player) || diagonalRightDown(gameBoard, column, row, player) || diagonalRightUp(gameBoard, column, row, player) || diagonalLeftDown(gameBoard, column, row, player); } - private static boolean up (Square gameBoard[][], short column, short row, Player player) { + 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 up (final Square gameBoard[][], short column, short row, + final Player player) { //Precondition 1. if (row <= 1) { @@ -26,14 +37,16 @@ public class CheckMovement { } //Precondition 2. - if (gameBoard[column][row-1].getPlayer() == player) { + if (gameBoard[column][row-1].getPlayer() == player || + empty(gameBoard, column, (short)(row-1))) { return false; } return outflank((short)0, (short)-1, gameBoard, player, (short)(row-2), column); } - private static boolean down (Square gameBoard[][], short column, short row, Player player) { + private static boolean down (final Square gameBoard[][], short column, short row, + final Player player) { //Precondition 1: if (row >= ReversiView.NUMBER_OF_ROWS -2) { @@ -41,14 +54,16 @@ public class CheckMovement { } //Precondition 2. - if (gameBoard[column][row+1].getPlayer() == player) { + if (gameBoard[column][row+1].getPlayer() == player || + empty(gameBoard, column, (short)(row+1))) { return false; } return outflank((short)0, (short)1, gameBoard, player, (short)(row+2), column); } - private static boolean right (Square gameBoard[][], short column, short row, Player player) { + private static boolean right (final Square gameBoard[][], short column, short row, + final Player player) { //Precondition 1: if (column >= ReversiView.NUMBER_OF_COLUMNS -2) { @@ -56,14 +71,16 @@ public class CheckMovement { } //Precondition 2. - if (gameBoard[column+1][row].getPlayer() == player) { + if (gameBoard[column+1][row].getPlayer() == player || + empty(gameBoard, (short)(column+1), row)) { return false; } return outflank((short)1, (short)0, gameBoard, player, row, (short)(column+2)); } - private static boolean left (Square gameBoard[][], short column, short row, Player player) { + private static boolean left (final Square gameBoard[][], short column, short row, + final Player player) { //Precondition 1: if (column <= 1) { @@ -71,23 +88,25 @@ public class CheckMovement { } //Precondition 2. - if (gameBoard[column-1][row].getPlayer() == player) { + if (gameBoard[column-1][row].getPlayer() == player || + empty(gameBoard, (short)(column-1), row)) { return false; } return outflank((short)-1, (short)0, gameBoard, player, row, (short)(column-2)); } - private static boolean diagonalLeftUp (Square gameBoard[][], short column, short row, - Player player) { + private static boolean diagonalLeftUp (final Square gameBoard[][], short column, + short row, final Player player) { //Precondition 1: - if (column <= 1 && row <= 1) { + if (column <= 1 || row <= 1) { return false; } //Precondition 2. - if (gameBoard[column-1][row-1].getPlayer() == player) { + if (gameBoard[column-1][row-1].getPlayer() == player || + empty(gameBoard, (short)(column-1), (short)(row-1))) { return false; } @@ -95,16 +114,17 @@ public class CheckMovement { (short)(column-2)); } - private static boolean diagonalRightDown (Square gameBoard[][], short column, short row, - Player player) { + private static boolean diagonalRightDown (final Square gameBoard[][], short column, + short row, final Player player) { //Precondition 1: - if (column >= ReversiView.NUMBER_OF_COLUMNS -2 && row >= ReversiView.NUMBER_OF_ROWS -2) { + if (column >= (ReversiView.NUMBER_OF_COLUMNS -2) || row >= (ReversiView.NUMBER_OF_ROWS -2)) { return false; } //Precondition 2. - if (gameBoard[column+1][row+1].getPlayer() == player) { + if (gameBoard[column+1][row+1].getPlayer() == player || + empty(gameBoard, (short)(column+1), (short)(row+1))) { return false; } @@ -112,16 +132,17 @@ public class CheckMovement { (short)(column+2)); } - private static boolean diagonalLeftDown (Square gameBoard[][], short column, short row, - Player player) { + private static boolean diagonalLeftDown (final Square gameBoard[][], short column, + short row, final Player player) { //Precondition 1: - if (column <= 1 && row >= ReversiView.NUMBER_OF_ROWS -2) { + if (column <= 1 || row >= (ReversiView.NUMBER_OF_ROWS -2)) { return false; } //Precondition 2. - if (gameBoard[column-1][row+1].getPlayer() == player) { + if (gameBoard[column-1][row+1].getPlayer() == player || + empty(gameBoard, (short)(column-1), (short)(row+1))) { return false; } @@ -129,16 +150,17 @@ public class CheckMovement { (short)(column-2)); } - private static boolean diagonalRightUp (Square gameBoard[][], short column, short row, - Player player) { + private static boolean diagonalRightUp (final Square gameBoard[][], short column, short row, + final Player player) { //Precondition 1: - if (row <= 1 && column >= ReversiView.NUMBER_OF_COLUMNS -2) { + if (row <= 1 || column >= (ReversiView.NUMBER_OF_COLUMNS -2)) { return false; } //Precondition 2. - if (gameBoard[column+1][row-1].getPlayer() == player) { + if (gameBoard[column+1][row-1].getPlayer() == player || + empty(gameBoard, (short)(column+1), (short)(row-1))) { return false; } @@ -146,15 +168,8 @@ public class CheckMovement { (short)(column+2)); } - public static boolean empty(Square gameBoard[][], short column, short row) { - if (gameBoard[column][row].getPlayer() == Player.NOPLAYER) { - return true; - } - return false; - } - - private static boolean outflank(short moveX, short moveY, Square gameBoard[][], Player player, - short row, short column) { + private static boolean outflank(final short moveX, final short moveY, + final Square gameBoard[][], Player player,short row, short column) { do { if (gameBoard[column][row].getPlayer() == player) { @@ -162,8 +177,8 @@ public class CheckMovement { } row = (short)(row + moveX); column = (short)(column + moveY); - }while (row > 0 && column >0 && - row <= ReversiView.NUMBER_OF_ROWS && column <= ReversiView.NUMBER_OF_COLUMNS); + }while (row > 0 && column > 0 && + row < ReversiView.NUMBER_OF_ROWS && column < ReversiView.NUMBER_OF_COLUMNS); return false; } diff --git a/src/de/android/reversi/ReversiView.java b/src/de/android/reversi/ReversiView.java index e08247b..95352a1 100644 --- a/src/de/android/reversi/ReversiView.java +++ b/src/de/android/reversi/ReversiView.java @@ -204,14 +204,14 @@ public class ReversiView extends SurfaceView { } private void drawCircle(Canvas canvas, Player player, int cx, int cy, int radius, - boolean isSolid) { + boolean alphaChannel) { Paint paint = new Paint(); paint.setAntiAlias(true); - //If not solid it is a suggestion. - if (!isSolid) { + //Set alpha channel when drawing suggestion discs. + if (alphaChannel) { paint.setAlpha(77); } @@ -219,6 +219,9 @@ public class ReversiView extends SurfaceView { case PLAYER1: paint.setColor(player.getColor()); //paint.setAntiAlias(true); + if (alphaChannel) { + paint.setColor(Color.BLUE); + } canvas.drawCircle(cx, cy, radius, paint); break; case PLAYER2: -- 2.1.4