No time for comments.
authorgumartinm <gustavo@gumartinm.name>
Tue, 4 Sep 2012 23:49:26 +0000 (01:49 +0200)
committergumartinm <gustavo@gumartinm.name>
Tue, 4 Sep 2012 23:49:26 +0000 (01:49 +0200)
AndroidManifest.xml
res/values/strings.xml
src/de/android/reversi/Player.java
src/de/android/reversi/ReversiView.java

index cc1f077..b1fec04 100644 (file)
@@ -15,7 +15,7 @@
         android:theme="@style/AppTheme" >
         <activity
             android:name=".ReversiActivity"
-            android:label="@string/title_activity_reversi
+            android:label="@string/app_name
             android:screenOrientation="portrait">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
index 1f1e715..fa5b469 100644 (file)
@@ -2,6 +2,8 @@
 
     <string name="app_name">Reversi</string>
     <string name="hello_world">Hello world!</string>
+    <string name="AIwon">YOU LOST!</string>
+    <string name="Iwin">I WIN!</string>
     <string name="menu_settings">Settings</string>
     <string name="title_activity_reversi">ReversiActivity</string>
     <string name="player1">Black: </string>
index 17bd528..7c1a5e0 100644 (file)
@@ -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() {
index b7d7ded..d8cff67 100644 (file)
@@ -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<Position> 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();
+        }
+    }
 }