First commit ReversiForFun
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 12 Aug 2012 20:33:22 +0000 (22:33 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 12 Aug 2012 20:33:22 +0000 (22:33 +0200)
24 files changed:
.gitignore
AndroidManifest.xml [new file with mode: 0644]
ic_launcher-web.png [new file with mode: 0644]
libs/android-support-v4.jar [new file with mode: 0644]
proguard-project.txt [new file with mode: 0644]
project.properties [new file with mode: 0644]
res/drawable-hdpi/ic_action_search.png [new file with mode: 0644]
res/drawable-hdpi/ic_launcher.png [new file with mode: 0644]
res/drawable-ldpi/ic_launcher.png [new file with mode: 0644]
res/drawable-mdpi/ic_action_search.png [new file with mode: 0644]
res/drawable-mdpi/ic_launcher.png [new file with mode: 0644]
res/drawable-xhdpi/ic_action_search.png [new file with mode: 0644]
res/drawable-xhdpi/ic_launcher.png [new file with mode: 0644]
res/layout/activity_reversi.xml [new file with mode: 0644]
res/menu/activity_reversi.xml [new file with mode: 0644]
res/values-large/dimens.xml [new file with mode: 0644]
res/values-v11/styles.xml [new file with mode: 0644]
res/values-v14/styles.xml [new file with mode: 0644]
res/values/dimens.xml [new file with mode: 0644]
res/values/strings.xml [new file with mode: 0644]
res/values/styles.xml [new file with mode: 0644]
src/de/android/reversi/Player.java [new file with mode: 0644]
src/de/android/reversi/ReversiActivity.java [new file with mode: 0644]
src/de/android/reversi/ReversiView.java [new file with mode: 0644]

index d3f5d3e..5a220c7 100644 (file)
@@ -14,3 +14,9 @@ gen/
 
 # Local configuration file (sdk path, etc)
 local.properties
+
+# Eclipse generated files and directories
+.classpath
+.project
+.settings
+target
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
new file mode 100644 (file)
index 0000000..be12a58
--- /dev/null
@@ -0,0 +1,25 @@
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="de.android.reversi"
+    android:versionCode="1"
+    android:versionName="1.0" >
+
+    <uses-sdk
+        android:minSdkVersion="15"
+        android:targetSdkVersion="15" android:maxSdkVersion="16"/>
+
+    <application
+        android:icon="@drawable/ic_launcher"
+        android:label="@string/app_name"
+        android:theme="@style/AppTheme" >
+        <activity
+            android:name=".ReversiActivity"
+            android:label="@string/title_activity_reversi" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>
\ No newline at end of file
diff --git a/ic_launcher-web.png b/ic_launcher-web.png
new file mode 100644 (file)
index 0000000..fe8ebb2
Binary files /dev/null and b/ic_launcher-web.png differ
diff --git a/libs/android-support-v4.jar b/libs/android-support-v4.jar
new file mode 100644 (file)
index 0000000..018c127
Binary files /dev/null and b/libs/android-support-v4.jar differ
diff --git a/proguard-project.txt b/proguard-project.txt
new file mode 100644 (file)
index 0000000..f2fe155
--- /dev/null
@@ -0,0 +1,20 @@
+# To enable ProGuard in your project, edit project.properties
+# to define the proguard.config property as described in that file.
+#
+# Add project specific ProGuard rules here.
+# By default, the flags in this file are appended to flags specified
+# in ${sdk.dir}/tools/proguard/proguard-android.txt
+# You can edit the include path and order by changing the ProGuard
+# include property in project.properties.
+#
+# For more details, see
+#   http://developer.android.com/guide/developing/tools/proguard.html
+
+# Add any project specific keep options here:
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+#   public *;
+#}
diff --git a/project.properties b/project.properties
new file mode 100644 (file)
index 0000000..e484b3c
--- /dev/null
@@ -0,0 +1,14 @@
+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system edit
+# "ant.properties", and override values to adapt the script to your
+# project structure.
+#
+# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
+#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
+
+# Project target.
+target=Google Inc.:Google APIs:16
diff --git a/res/drawable-hdpi/ic_action_search.png b/res/drawable-hdpi/ic_action_search.png
new file mode 100644 (file)
index 0000000..67de12d
Binary files /dev/null and b/res/drawable-hdpi/ic_action_search.png differ
diff --git a/res/drawable-hdpi/ic_launcher.png b/res/drawable-hdpi/ic_launcher.png
new file mode 100644 (file)
index 0000000..5b37499
Binary files /dev/null and b/res/drawable-hdpi/ic_launcher.png differ
diff --git a/res/drawable-ldpi/ic_launcher.png b/res/drawable-ldpi/ic_launcher.png
new file mode 100644 (file)
index 0000000..5fb5527
Binary files /dev/null and b/res/drawable-ldpi/ic_launcher.png differ
diff --git a/res/drawable-mdpi/ic_action_search.png b/res/drawable-mdpi/ic_action_search.png
new file mode 100644 (file)
index 0000000..134d549
Binary files /dev/null and b/res/drawable-mdpi/ic_action_search.png differ
diff --git a/res/drawable-mdpi/ic_launcher.png b/res/drawable-mdpi/ic_launcher.png
new file mode 100644 (file)
index 0000000..18170e9
Binary files /dev/null and b/res/drawable-mdpi/ic_launcher.png differ
diff --git a/res/drawable-xhdpi/ic_action_search.png b/res/drawable-xhdpi/ic_action_search.png
new file mode 100644 (file)
index 0000000..d699c6b
Binary files /dev/null and b/res/drawable-xhdpi/ic_action_search.png differ
diff --git a/res/drawable-xhdpi/ic_launcher.png b/res/drawable-xhdpi/ic_launcher.png
new file mode 100644 (file)
index 0000000..30e1e58
Binary files /dev/null and b/res/drawable-xhdpi/ic_launcher.png differ
diff --git a/res/layout/activity_reversi.xml b/res/layout/activity_reversi.xml
new file mode 100644 (file)
index 0000000..2996699
--- /dev/null
@@ -0,0 +1,46 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical" 
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent"
+    android:background="#ffffffff"
+    android:padding="25dip">
+
+    <LinearLayout 
+        android:orientation="horizontal"
+        android:layout_width="wrap_content" 
+        android:layout_height="wrap_content"
+        android:paddingRight="25dip">
+        <TextView 
+            android:text="@string/player1" 
+            android:id="@+id/txtPlayer1"
+            android:layout_width="wrap_content" 
+            android:layout_height="wrap_content"/>
+        <TextView android:text=" 2" 
+            android:id="@+id/txtPlayer1Counter"
+            android:minWidth="30dp" 
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content">    
+        </TextView>
+        <TextView 
+            android:text="@string/player2"
+             android:id="@+id/txtPlayer2"
+            android:layout_width="wrap_content" 
+            android:layout_height="wrap_content">
+        </TextView>
+        <TextView 
+            android:text=" 2" 
+            android:id="@+id/txtPlayer2Counter"
+            android:minWidth="30dp" 
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content">
+        </TextView>
+    </LinearLayout>
+    
+    <de.android.reversi.ReversiView
+        android:id="@+id/gameBoard" 
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="top"
+        android:layout_weight="0"/>
+    
+</LinearLayout>
diff --git a/res/menu/activity_reversi.xml b/res/menu/activity_reversi.xml
new file mode 100644 (file)
index 0000000..cfc10fd
--- /dev/null
@@ -0,0 +1,6 @@
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:id="@+id/menu_settings"
+        android:title="@string/menu_settings"
+        android:orderInCategory="100"
+        android:showAsAction="never" />
+</menu>
diff --git a/res/values-large/dimens.xml b/res/values-large/dimens.xml
new file mode 100644 (file)
index 0000000..5c4a502
--- /dev/null
@@ -0,0 +1,7 @@
+<resources>
+
+    <dimen name="padding_small">8dp</dimen>
+    <dimen name="padding_medium">16dp</dimen>
+    <dimen name="padding_large">16dp</dimen>
+
+</resources>
\ No newline at end of file
diff --git a/res/values-v11/styles.xml b/res/values-v11/styles.xml
new file mode 100644 (file)
index 0000000..d408cbc
--- /dev/null
@@ -0,0 +1,5 @@
+<resources>
+
+    <style name="AppTheme" parent="android:Theme.Holo.Light" />
+
+</resources>
\ No newline at end of file
diff --git a/res/values-v14/styles.xml b/res/values-v14/styles.xml
new file mode 100644 (file)
index 0000000..1c089a7
--- /dev/null
@@ -0,0 +1,5 @@
+<resources>
+
+    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" />
+
+</resources>
\ No newline at end of file
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
new file mode 100644 (file)
index 0000000..ec96646
--- /dev/null
@@ -0,0 +1,7 @@
+<resources>
+
+    <dimen name="padding_small">8dp</dimen>
+    <dimen name="padding_medium">8dp</dimen>
+    <dimen name="padding_large">16dp</dimen>
+
+</resources>
\ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
new file mode 100644 (file)
index 0000000..1b88f05
--- /dev/null
@@ -0,0 +1,10 @@
+<resources>
+
+    <string name="app_name">Reversi</string>
+    <string name="hello_world">Hello world!</string>
+    <string name="menu_settings">Settings</string>
+    <string name="title_activity_reversi">ReversiActivity</string>
+    <string name="player1">Blue</string>
+    <string name="player2">Red</string>
+
+</resources>
\ No newline at end of file
diff --git a/res/values/styles.xml b/res/values/styles.xml
new file mode 100644 (file)
index 0000000..4dba0d0
--- /dev/null
@@ -0,0 +1,5 @@
+<resources>
+
+    <style name="AppTheme" parent="android:Theme.Light" />
+
+</resources>
\ No newline at end of file
diff --git a/src/de/android/reversi/Player.java b/src/de/android/reversi/Player.java
new file mode 100644 (file)
index 0000000..904a380
--- /dev/null
@@ -0,0 +1,54 @@
+package de.android.reversi;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import android.graphics.Color;
+
+public enum Player {
+    PLAYER1(1) {
+        @Override
+        public int color() {
+            return Color.BLACK;
+
+        }
+    },
+    PLAYER2(2) {
+        @Override
+        int color() {
+            return Color.WHITE;
+        }
+    };
+
+    private final int playerNumber;
+    private final int color;
+    private static final Map<Integer, Player> playerMap = new HashMap<Integer, Player>();
+
+
+    static {
+        for (Player player : Player.values())
+        {
+            playerMap.put(player.playerNumber, player);
+        }
+    }
+
+    private Player (final int playerNumber) {
+        this.playerNumber = playerNumber;
+        this.color = color();
+    }
+
+    public int getPlayerNumber() {
+        return this.playerNumber;
+    }
+
+    public int getColor() {
+        return this.color;
+    }
+
+    public static final Player getPlayer (final int playerNumber)
+    {
+        return playerMap.get(playerNumber);
+    }
+
+    abstract int color();
+}
diff --git a/src/de/android/reversi/ReversiActivity.java b/src/de/android/reversi/ReversiActivity.java
new file mode 100644 (file)
index 0000000..fce5e08
--- /dev/null
@@ -0,0 +1,22 @@
+package de.android.reversi;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.view.Menu;
+
+public class ReversiActivity extends Activity {
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_reversi);
+    }
+
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        getMenuInflater().inflate(R.menu.activity_reversi, menu);
+        return true;
+    }
+
+
+}
diff --git a/src/de/android/reversi/ReversiView.java b/src/de/android/reversi/ReversiView.java
new file mode 100644 (file)
index 0000000..3bea3dc
--- /dev/null
@@ -0,0 +1,232 @@
+package de.android.reversi;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.util.AttributeSet;
+import android.view.MotionEvent;
+import android.view.SurfaceHolder;
+import android.view.SurfaceView;
+
+
+public class ReversiView extends SurfaceView {
+    private static final String TAG = "GameBoard";
+
+    /**
+     * The number of columns of this board
+     */
+    private static int NUMBER_OF_COLUMNS = 8;
+
+    /**
+     * The number of rows of this board
+     */
+    private static int NUMBER_OF_ROWS = 8;
+
+    /**
+     * The top margin
+     */
+    private static int TOP_MARGIN = 0;
+
+    /**
+     * Vertical margin
+     */
+    private static int LEFT_MARGIN = 0;
+
+    private int squareWidth;
+    private int squareHeight;
+    private int canvasHeight;
+    private int canvasWidth;
+    private final int gameBoard[][] = new int[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
+
+
+    public ReversiView(Context context) {
+        super(context);
+        this.initialize();
+    }
+
+    public ReversiView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        this.initialize();
+    }
+
+    public ReversiView(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+        this.initialize();
+    }
+
+    /**
+     * Occurs when drawing the board
+     * 
+     * @param canvas
+     */
+    @Override
+    public void onDraw(Canvas canvas) {
+        this.setBackgroundColor(Color.RED);
+    }
+
+    private void drawGrid(Canvas canvas) {
+        //Canvas background
+        Paint paint = new Paint();
+        paint.setColor(Color.WHITE);
+        canvas.drawPaint(paint);
+
+
+        //Square (green)
+        paint.setColor(Color.rgb(0, 158, 11));
+        canvas.drawRect(LEFT_MARGIN, TOP_MARGIN,
+                NUMBER_OF_COLUMNS * squareWidth + LEFT_MARGIN,
+                NUMBER_OF_ROWS * squareHeight + TOP_MARGIN, paint);
+
+
+        //Lines between squares
+        paint.setColor(Color.BLACK);
+        paint.setStrokeWidth(4);
+        for (int col = 0; col <= NUMBER_OF_COLUMNS; col++) {
+            // vertical lines
+            int x = col * squareWidth + LEFT_MARGIN;
+            canvas.drawLine(x, TOP_MARGIN, x, canvasHeight - TOP_MARGIN * 1, paint);
+        }
+        for (int row = 0; row < NUMBER_OF_ROWS + 1; row++) {
+            int y = row * squareHeight + TOP_MARGIN;
+            // horizontal lines
+            canvas.drawLine(LEFT_MARGIN, y, canvasWidth - (LEFT_MARGIN * 1), y, paint);
+        }
+
+    }
+
+    private void calculateGraphicParameters(Canvas canvas, int width, int height) {
+        canvasWidth = width;
+        canvasHeight = height;
+
+        // getting the minor (the board is a square)
+        if (canvasHeight > canvasWidth) {
+            canvasHeight = canvasWidth;
+        } else {
+            canvasWidth = canvasHeight;
+        }
+
+        // converting the dimensions to get them divisible by 8
+        while (canvasWidth % 8 != 0) {
+            canvasWidth--;
+            canvasHeight--;
+        }
+
+        squareWidth = (canvasWidth - LEFT_MARGIN * 2) / NUMBER_OF_COLUMNS;
+        squareHeight = (canvasHeight - TOP_MARGIN * 2) / NUMBER_OF_ROWS;
+    }
+
+    private void initialize() {
+
+        getHolder().addCallback(new SurfaceHolder.Callback() {
+
+            @Override
+            public void surfaceCreated(SurfaceHolder holder) {
+                //White
+                updateGrid(Player.PLAYER1.getPlayerNumber(), 3, 3);
+                updateGrid(Player.PLAYER1.getPlayerNumber(), 4, 4);
+                //Black
+                updateGrid(Player.PLAYER2.getPlayerNumber(), 4, 3);
+                updateGrid(Player.PLAYER2.getPlayerNumber(), 3, 4);
+
+            }
+
+            @Override
+            public void surfaceChanged(SurfaceHolder holder, int format,
+                    int width, int height) {
+                Canvas canvas = holder.lockCanvas();
+                calculateGraphicParameters(canvas, width, height);
+                drawGrid(canvas);
+                drawPositions(canvas);
+                holder.unlockCanvasAndPost(canvas);
+            }
+
+            @Override
+            public void surfaceDestroyed(SurfaceHolder holder) {
+                // TODO Auto-generated method stub
+            }
+        });
+    }
+
+    @Override
+    public boolean onTouchEvent(MotionEvent event) {
+        if (event.getAction() == MotionEvent.ACTION_DOWN) {
+            // Hidden pointer
+            int column = transformCoordinateXInColumn(event.getX());
+            int row = transformCoordinateYInRow(event.getY());
+            if (row != -1 && column != -1 ) {
+                updateGrid(Player.PLAYER2.getPlayerNumber(), column, row);
+                Canvas canvas = getHolder().lockCanvas();
+                drawGrid(canvas);
+                drawPositions(canvas);
+                getHolder().unlockCanvasAndPost(canvas);
+            }
+            return true;
+        }
+        else {
+            return false;
+        }
+    }
+
+    private int transformCoordinateYInRow(float y) {
+
+        int row = (int) ((y - TOP_MARGIN) / this.squareWidth);
+
+        // if tapped outside the board
+        if (row < 0 || row >= NUMBER_OF_ROWS) {
+            row = -1;
+        }
+
+        return row;
+    }
+
+    private int transformCoordinateXInColumn(float x) {
+
+        int column = (int) ((x - LEFT_MARGIN) / this.squareWidth);
+
+        // if tapped outside the board
+        if (column < 0 || column >= NUMBER_OF_COLUMNS) {
+            column = -1;
+        }
+
+        return column;
+    }
+
+    private void drawChip(Canvas canvas, int player, int column, int row) {
+        if (player != 0) {
+            // calculating the center of the cell
+            int cellMediumX = (column * this.squareWidth + (column + 1) * this.squareWidth) / 2;
+            int cellMediumY = (row * this.squareHeight + (row + 1) * this.squareHeight) / 2;
+
+            // applying the margins
+            int cx = cellMediumX + LEFT_MARGIN;
+            int cy = cellMediumY + TOP_MARGIN;
+            // now the radius
+            int radius = (this.squareWidth - 2) / 2 - 2;
+
+            this.drawCircle(canvas, player, cx, cy, radius);
+        }
+    }
+
+    private void drawCircle(Canvas canvas, int player, int cx, int cy, int radius) {
+
+        Paint paint = new Paint();
+
+        paint.setColor(Player.getPlayer(player).getColor());
+        //paint.setAntiAlias(true);
+
+        canvas.drawCircle(cx, cy, radius, paint);
+    }
+
+    private void updateGrid(int player, int column, int row) {
+        this.gameBoard[column][row] = player;
+    }
+
+    private void drawPositions(Canvas canvas) {
+        for (int column = 0; column < NUMBER_OF_COLUMNS; column++) {
+            for (int row = 0; row < NUMBER_OF_ROWS; row++) {
+                drawChip(canvas, this.gameBoard[column][row], column, row);
+            }
+        }
+    }
+}