From 9d3d399c249de84628ca6209a315ef1936f23531 Mon Sep 17 00:00:00 2001 From: gumartinm Date: Sat, 1 Sep 2012 22:35:14 +0200 Subject: [PATCH] No time for comments :( --- src/de/android/reversi/AIThread.java | 17 +++++++++++------ src/de/android/reversi/Board.java | 9 ++++----- src/de/android/reversi/ReversiView.java | 14 ++++++-------- src/de/android/reversi/logic/ReversiLogic.java | 8 ++++---- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/de/android/reversi/AIThread.java b/src/de/android/reversi/AIThread.java index bc89927..31873af 100644 --- a/src/de/android/reversi/AIThread.java +++ b/src/de/android/reversi/AIThread.java @@ -1,20 +1,25 @@ package de.android.reversi; -import java.util.List; +import android.util.Log; public class AIThread extends Thread { - private List 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 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); + } } } diff --git a/src/de/android/reversi/Board.java b/src/de/android/reversi/Board.java index c784a4e..0697485 100644 --- a/src/de/android/reversi/Board.java +++ b/src/de/android/reversi/Board.java @@ -34,15 +34,14 @@ public class Board { gameBoard[column][row].setSuggestion(suggestion); } - public void removeSuggestionsFromBoard(final List listAllowedMovements) { - - for (final Movement iterator : listAllowedMovements) { - this.updateBoard(Player.NOPLAYER, iterator.getColumn(), iterator.getRow()); + public void removeSuggestionsFromBoard(final List 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()); } diff --git a/src/de/android/reversi/ReversiView.java b/src/de/android/reversi/ReversiView.java index 47b0587..900cf8f 100644 --- a/src/de/android/reversi/ReversiView.java +++ b/src/de/android/reversi/ReversiView.java @@ -17,7 +17,6 @@ import de.android.reversi.logic.ReversiLogic; 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; @@ -94,7 +93,7 @@ public class ReversiView extends SurfaceView { 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) { @@ -134,7 +133,7 @@ public class ReversiView extends SurfaceView { 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); @@ -216,15 +215,15 @@ public class ReversiView extends SurfaceView { 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) { @@ -243,8 +242,7 @@ public class ReversiView extends SurfaceView { } 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(); diff --git a/src/de/android/reversi/logic/ReversiLogic.java b/src/de/android/reversi/logic/ReversiLogic.java index 4abc08a..2ad5f9c 100644 --- a/src/de/android/reversi/logic/ReversiLogic.java +++ b/src/de/android/reversi/logic/ReversiLogic.java @@ -9,9 +9,9 @@ import de.android.reversi.Movement; import de.android.reversi.Player; import de.android.reversi.Square; -public class ReversiLogic { +public final class ReversiLogic { - public List allowedMovements(final Player player, final Square gameBoard[][]) { + public static List allowedMovements(final Player player, final Square gameBoard[][]) { final List list = new ArrayList(); for (short column = 0; column < Board.NUMBER_OF_COLUMNS; column++) { @@ -32,7 +32,7 @@ public class ReversiLogic { return list; } - public Movement retrieveAllowedMovement(final short row, final short column, + public static Movement retrieveAllowedMovement(final short row, final short column, final List listAllowedMovements) { for (final Movement movement : listAllowedMovements) { if ((movement.getRow() == row) && (movement.getColumn() == column)) { @@ -43,7 +43,7 @@ public class ReversiLogic { return null; } - public Player opponent(final Player currentPlayer) { + public static Player opponent(final Player currentPlayer) { switch (currentPlayer){ case PLAYER1: return Player.PLAYER2; -- 2.1.4