From: gumartinm Date: Tue, 4 Sep 2012 23:49:26 +0000 (+0200) Subject: No time for comments. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=934fdbb6323697dfa796a26171a92b68adef9dca;p=AndroidReversiForFun%2F.git No time for comments. --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index cc1f077..b1fec04 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -15,7 +15,7 @@ android:theme="@style/AppTheme" > diff --git a/res/values/strings.xml b/res/values/strings.xml index 1f1e715..fa5b469 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -2,6 +2,8 @@ Reversi Hello world! + YOU LOST! + I WIN! Settings ReversiActivity Black: diff --git a/src/de/android/reversi/Player.java b/src/de/android/reversi/Player.java index 17bd528..7c1a5e0 100644 --- a/src/de/android/reversi/Player.java +++ b/src/de/android/reversi/Player.java @@ -11,7 +11,7 @@ public enum Player { } }, - //Human player? + PLAYER1 { @Override public int color() { @@ -19,7 +19,7 @@ public enum Player { } }, - //IA? + PLAYER2 { @Override int color() { diff --git a/src/de/android/reversi/ReversiView.java b/src/de/android/reversi/ReversiView.java index b7d7ded..d8cff67 100644 --- a/src/de/android/reversi/ReversiView.java +++ b/src/de/android/reversi/ReversiView.java @@ -4,10 +4,15 @@ import java.lang.Thread.UncaughtExceptionHandler; import java.util.List; import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.DialogFragment; import android.content.Context; +import android.content.DialogInterface; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; +import android.os.Bundle; import android.os.Handler; import android.util.AttributeSet; import android.util.Log; @@ -27,7 +32,7 @@ public class ReversiView extends SurfaceView { private final Context context; //¿Funciona bien volatile con enum? Ver mi codigo de Singletons y enums. - private volatile Player currentPlayer = Player.PLAYER1; + private volatile Player currentPlayer = AI; private volatile boolean isEnableUserTouch; private List listAllowedPositions; @@ -90,19 +95,21 @@ public class ReversiView extends SurfaceView { @Override public void surfaceCreated(final SurfaceHolder holder) { - //White + //Black board.makeMove(Player.PLAYER1, (short)3, (short)4); board.makeMove(Player.PLAYER1, (short)4, (short)3); - //Black + //White board.makeMove(Player.PLAYER2, (short)4, (short)4); board.makeMove(Player.PLAYER2, (short)3, (short)3); //AllowedPositions for Player - listAllowedPositions = board.allowedPositions(currentPlayer); + if (currentPlayer != AI) { + listAllowedPositions = board.allowedPositions(currentPlayer); - //UpdateBoard with suggestions - for (final Position movement : listAllowedPositions) { - board.makeMove(currentPlayer, movement.getColumn(), movement.getRow(), true); + //UpdateBoard with suggestions + for (final Position movement : listAllowedPositions) { + board.makeMove(currentPlayer, movement.getColumn(), movement.getRow(), true); + } } } @@ -115,7 +122,30 @@ public class ReversiView extends SurfaceView { drawGrid(canvas); drawPositions(canvas); holder.unlockCanvasAndPost(canvas); - isEnableUserTouch = true; + 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); + } + + }); + + + AIThread.start(); } @Override @@ -245,9 +275,10 @@ public class ReversiView extends SurfaceView { //AllowedPositions for player. listAllowedPositions = board.allowedPositions(currentPlayer); - final AI prueba = new AI(this.currentPlayer); - - prueba.getBestMove(board); + if (listAllowedPositions.isEmpty()) { + final DialogFragment newFragment = ErrorDialogFragment.newInstance(R.string.Iwin); + newFragment.show(((Activity)context).getFragmentManager(), "errorDialog"); + } //UpdateBoard with suggestions for (final Position suggestedPosition : listAllowedPositions) { @@ -273,10 +304,15 @@ public class ReversiView extends SurfaceView { @Override public void run() { final AI ai = new AI(currentPlayer); + final Position move = ai.getBestMove(board); - mainLoop(ai.getBestMove(board)); - - + if (move != null) { + mainLoop(ai.getBestMove(board)); + } + else { + final DialogFragment newFragment = ErrorDialogFragment.newInstance(R.string.AIwon); + newFragment.show(((Activity)context).getFragmentManager(), "errorDialog"); + } } }, "AI-Thread"); @@ -296,4 +332,32 @@ public class ReversiView extends SurfaceView { AIThread.start(); } } + + public static class ErrorDialogFragment extends DialogFragment { + + public static ErrorDialogFragment newInstance(final int title) { + final ErrorDialogFragment frag = new ErrorDialogFragment(); + final Bundle args = new Bundle(); + + args.putInt("title", title); + frag.setArguments(args); + + return frag; + } + + @Override + public Dialog onCreateDialog(final Bundle savedInstanceState) { + final int title = getArguments().getInt("title"); + + return new AlertDialog.Builder(getActivity()) + .setIcon(android.R.drawable.ic_dialog_alert) + .setTitle(title) + .setPositiveButton("", new DialogInterface.OnClickListener() { + @Override + public void onClick(final DialogInterface dialog, final int whichButton) { + + } + }).create(); + } + } }