List<Position> allowedPositions;
if (depth == 0 || (allowedPositions = board.allowedPositions(player)).isEmpty()) {
- return 10; //the heuristic value of node
+ return this.heuristic(board, player); //the heuristic value of node
}
if (max) {
for (final Position child : allowedPositions) {
final Board newBoard = board.clone();
newBoard.makeMove(this.max, child.getColumn(), child.getRow());
newBoard.flipOpponentDiscs(child, this.max);
- final int val = this.minimaxAB(newBoard, child, depth, -10, 10, true);
+ final int val = this.minimaxAB(newBoard, child, depth, 0, 0, true);
if (val > alpha) {
alpha = val;
bestMove = child;
return bestMove;
}
+
+
+ private int getMobilityForPlayer(final Board board, final Player player) {
+
+ return board.allowedPositions(player).size();
+
+ }
+
+ private int heuristic (final Board board, final Player player) {
+ return this.getMobilityForPlayer(board, player);
+ }
}
+++ /dev/null
-package de.android.reversi;
-
-import android.util.Log;
-
-public class AIThread extends Thread {
- private static final String TAG = "AIThread";
- private final Board board;
- private final Player player;
-
-
- public AIThread(final Board board, final Player player) {
- super("AI-Thread");
- this.setUncaughtExceptionHandler(new UnExpectedException());
- this.board = board;
- this.player = player;
- }
-
- private class UnExpectedException implements UncaughtExceptionHandler {
-
- @Override
- public void uncaughtException(final Thread thread, final Throwable ex) {
- Log.e(TAG, "Unexpected exception. Thread: " + thread.getName(), ex);
- }
- }
-}
package de.android.reversi;
+import java.lang.Thread.UncaughtExceptionHandler;
import java.util.List;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
+import android.os.Handler;
import android.util.AttributeSet;
+import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class ReversiView extends SurfaceView {
private final Board board = new Board();
- private final Player AI = Player.NOPLAYER;
+ private final Player AI = Player.PLAYER1;
+ int player1Score;
+ int player2Score;
private final Context context;
if((movement = ReversiLogic.retrieveAllowedPosition(row, column,
listAllowedPositions)) != null) {
board.removeSuggestionsFromBoard(listAllowedPositions);
- this.mainLoop(column, row, movement);
+ this.mainLoop(movement);
}
}
}
}
private void drawPositions(final Canvas canvas) {
- int player1Score = 0;
- int player2Score = 0;
+ player1Score = 0;
+ player2Score = 0;
for (short column = 0; column < Board.NUMBER_OF_COLUMNS; column++) {
for (short row = 0; row < Board.NUMBER_OF_ROWS; row++) {
}
}
- ((TextView)((Activity)this.context).findViewById(R.id.txtPlayer1Score)).setText(String.format(" %d %s", player1Score, "discs"));
- ((TextView)((Activity)this.context).findViewById(R.id.txtPlayer2Score)).setText(String.format(" %d %s", player2Score, "discs"));
+ //TODO Really? I must redesign all of this :(
+ final Handler updateUI = this.getHandler();
+ updateUI.post(new Runnable() {
+
+ @Override
+ public void run() {
+ ((TextView)((Activity)context).findViewById(R.id.txtPlayer1Score)).setText(String.format(" %d %s", player1Score, "discs"));
+ ((TextView)((Activity)context).findViewById(R.id.txtPlayer2Score)).setText(String.format(" %d %s", player2Score, "discs"));
+ }
+
+ });
+
}
- private void mainLoop(final short column, final short row, final Position movement) {
+ private void mainLoop(final Position position) {
+
+ board.makeMove(this.currentPlayer, position.getColumn(), position.getRow());
+ board.flipOpponentDiscs(position, currentPlayer);
- board.makeMove(this.currentPlayer, column, row);
- board.flipOpponentDiscs(movement, currentPlayer);
+ Canvas canvas = getHolder().lockCanvas();
+ drawGrid(canvas);
+ drawPositions(canvas);
+ getHolder().unlockCanvasAndPost(canvas);
//Switch player.
this.currentPlayer = ReversiLogic.opponent(this.currentPlayer);
}
- final Canvas canvas = getHolder().lockCanvas();
+ canvas = getHolder().lockCanvas();
drawGrid(canvas);
drawPositions(canvas);
getHolder().unlockCanvasAndPost(canvas);
}
else {
- final AIThread AI = new AIThread(board, currentPlayer);
+ final Thread AIThread = new Thread(new Runnable(){
+
+ @Override
+ public void run() {
+ final AI ai = new AI(currentPlayer);
+
+ mainLoop(ai.getBestMove(board));
+
+ }
+
+ }, "AI-Thread");
+
+ AIThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
+
+ @Override
+ public void uncaughtException(final Thread thread, final Throwable ex) {
+ Log.e("AIThread", "Unexpected exception. Thread: " + thread.getName(), ex);
+ }
+
+ });
this.isEnableUserTouch = false;
- AI.start();
+
+
+
+ AIThread.start();
}
}
}