package de.android.reversi;
-import java.util.List;
+import android.util.Log;
public class AIThread extends Thread {
- private List<Movement> listAllowedMovements;
+ private static final String TAG = "AIThread";
private final Board board;
private final Player player;
- public AIThread(final Board board, final Player player, final String nameThread) {
- super(nameThread);
+ public AIThread(final Board board, final Player player) {
+ super("AI-Thread");
+ this.setUncaughtExceptionHandler(new UnExpectedException());
this.board = board;
this.player = player;
}
- public void setListAllowedMovements(final List<Movement> listAllowedMovements) {
- this.listAllowedMovements = listAllowedMovements;
+ private class UnExpectedException implements UncaughtExceptionHandler {
+
+ @Override
+ public void uncaughtException(final Thread thread, final Throwable ex) {
+ Log.e(TAG, "Unexpected exception. Thread: " + thread.getName(), ex);
+ }
}
}
gameBoard[column][row].setSuggestion(suggestion);
}
- public void removeSuggestionsFromBoard(final List<Movement> listAllowedMovements) {
-
- for (final Movement iterator : listAllowedMovements) {
- this.updateBoard(Player.NOPLAYER, iterator.getColumn(), iterator.getRow());
+ public void removeSuggestionsFromBoard(final List<Movement> list) {
+ for (final Movement movement : list) {
+ this.updateBoard(Player.NOPLAYER, movement.getColumn(), movement.getRow());
}
}
- public void flipOpponentDiscs(final Square gameBoard[][], final Movement movement, final Player currentPlayer) {
+ public void flipOpponentDiscs(final Movement movement, final Player currentPlayer) {
for (final FlippedDisc flippedDisc : movement.getFlippedDiscs()) {
this.updateBoard(currentPlayer, flippedDisc.getColumn(), flippedDisc.getRow());
}
public class ReversiView extends SurfaceView {
private final Board board = new Board();
- private final ReversiLogic reversiLogic = new ReversiLogic();
private final Player AI = Player.NOPLAYER;
private final Context context;
board.updateBoard(Player.PLAYER2, (short)3, (short)3);
//AllowedMovements for Player
- listAllowedMovements = reversiLogic.allowedMovements(currentPlayer, board.getGameBoard());
+ listAllowedMovements = ReversiLogic.allowedMovements(currentPlayer, board.getGameBoard());
//UpdateBoard with suggestions
for (final Movement movement : listAllowedMovements) {
if (row != -1 && column != -1 ) {
Movement movement;
- if((movement = reversiLogic.retrieveAllowedMovement(row, column,
+ if((movement = ReversiLogic.retrieveAllowedMovement(row, column,
listAllowedMovements)) != null) {
board.removeSuggestionsFromBoard(listAllowedMovements);
this.mainLoop(column, row, movement);
private void mainLoop(final short column, final short row, final Movement movement) {
board.updateBoard(this.currentPlayer, column, row);
- board.flipOpponentDiscs(board.getGameBoard(), movement, currentPlayer);
+ board.flipOpponentDiscs(movement, currentPlayer);
//Switch player.
- this.currentPlayer = reversiLogic.opponent(this.currentPlayer);
+ this.currentPlayer = ReversiLogic.opponent(this.currentPlayer);
if (this.currentPlayer != this.AI) {
//AllowedMovements for player.
- listAllowedMovements = reversiLogic.allowedMovements(currentPlayer, board.getGameBoard());
+ listAllowedMovements = ReversiLogic.allowedMovements(currentPlayer, board.getGameBoard());
//UpdateBoard with suggestions
for (final Movement suggestedMovement : listAllowedMovements) {
}
else {
- final AIThread AI = new AIThread(board, currentPlayer, "AI-Thread");
- AI.setListAllowedMovements(listAllowedMovements);
+ final AIThread AI = new AIThread(board, currentPlayer);
this.isEnableUserTouch = false;
AI.start();
import de.android.reversi.Player;
import de.android.reversi.Square;
-public class ReversiLogic {
+public final class ReversiLogic {
- public List<Movement> allowedMovements(final Player player, final Square gameBoard[][]) {
+ 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++) {
return list;
}
- public Movement retrieveAllowedMovement(final short row, final short column,
+ public static Movement retrieveAllowedMovement(final short row, final short column,
final List<Movement> listAllowedMovements) {
for (final Movement movement : listAllowedMovements) {
if ((movement.getRow() == row) && (movement.getColumn() == column)) {
return null;
}
- public Player opponent(final Player currentPlayer) {
+ public static Player opponent(final Player currentPlayer) {
switch (currentPlayer){
case PLAYER1:
return Player.PLAYER2;