Browse Source

Initial commit

Ian Adam Naval 6 years ago
commit
160949c0e5

+ 8
- 0
.classpath View File

@@ -0,0 +1,8 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<classpath>
3
+	<classpathentry kind="src" path="src"/>
4
+	<classpathentry kind="src" path="gen"/>
5
+	<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
6
+	<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
7
+	<classpathentry kind="output" path="bin/classes"/>
8
+</classpath>

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+bin/
2
+gen/

+ 33
- 0
.project View File

@@ -0,0 +1,33 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<projectDescription>
3
+	<name>DiceUp</name>
4
+	<comment></comment>
5
+	<projects>
6
+	</projects>
7
+	<buildSpec>
8
+		<buildCommand>
9
+			<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
10
+			<arguments>
11
+			</arguments>
12
+		</buildCommand>
13
+		<buildCommand>
14
+			<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
15
+			<arguments>
16
+			</arguments>
17
+		</buildCommand>
18
+		<buildCommand>
19
+			<name>org.eclipse.jdt.core.javabuilder</name>
20
+			<arguments>
21
+			</arguments>
22
+		</buildCommand>
23
+		<buildCommand>
24
+			<name>com.android.ide.eclipse.adt.ApkBuilder</name>
25
+			<arguments>
26
+			</arguments>
27
+		</buildCommand>
28
+	</buildSpec>
29
+	<natures>
30
+		<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
31
+		<nature>org.eclipse.jdt.core.javanature</nature>
32
+	</natures>
33
+</projectDescription>

+ 23
- 0
AndroidManifest.xml View File

@@ -0,0 +1,23 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3
+    package="com.ianonavy.diceup"
4
+    android:versionCode="1"
5
+    android:versionName="1.0" >
6
+
7
+    <uses-sdk android:minSdkVersion="3" />
8
+
9
+    <application
10
+        android:icon="@drawable/ic_launcher"
11
+        android:label="@string/app_name" >
12
+        <activity
13
+            android:name=".DiceUpActivity"
14
+            android:label="@string/app_name" >
15
+            <intent-filter>
16
+                <action android:name="android.intent.action.MAIN" />
17
+
18
+                <category android:name="android.intent.category.LAUNCHER" />
19
+            </intent-filter>
20
+        </activity>
21
+    </application>
22
+
23
+</manifest>

+ 40
- 0
proguard.cfg View File

@@ -0,0 +1,40 @@
1
+-optimizationpasses 5
2
+-dontusemixedcaseclassnames
3
+-dontskipnonpubliclibraryclasses
4
+-dontpreverify
5
+-verbose
6
+-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
7
+
8
+-keep public class * extends android.app.Activity
9
+-keep public class * extends android.app.Application
10
+-keep public class * extends android.app.Service
11
+-keep public class * extends android.content.BroadcastReceiver
12
+-keep public class * extends android.content.ContentProvider
13
+-keep public class * extends android.app.backup.BackupAgentHelper
14
+-keep public class * extends android.preference.Preference
15
+-keep public class com.android.vending.licensing.ILicensingService
16
+
17
+-keepclasseswithmembernames class * {
18
+    native <methods>;
19
+}
20
+
21
+-keepclasseswithmembers class * {
22
+    public <init>(android.content.Context, android.util.AttributeSet);
23
+}
24
+
25
+-keepclasseswithmembers class * {
26
+    public <init>(android.content.Context, android.util.AttributeSet, int);
27
+}
28
+
29
+-keepclassmembers class * extends android.app.Activity {
30
+   public void *(android.view.View);
31
+}
32
+
33
+-keepclassmembers enum * {
34
+    public static **[] values();
35
+    public static ** valueOf(java.lang.String);
36
+}
37
+
38
+-keep class * implements android.os.Parcelable {
39
+  public static final android.os.Parcelable$Creator *;
40
+}

+ 11
- 0
project.properties View File

@@ -0,0 +1,11 @@
1
+# This file is automatically generated by Android Tools.
2
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3
+#
4
+# This file must be checked in Version Control Systems.
5
+#
6
+# To customize properties used by the Ant build system use,
7
+# "ant.properties", and override values to adapt the script to your
8
+# project structure.
9
+
10
+# Project target.
11
+target=android-3

BIN
res/drawable/green_sticker.png View File


BIN
res/drawable/ic_launcher.png View File


BIN
res/drawable/red_sticker.png View File


BIN
res/drawable/yellow_sticker.png View File


+ 61
- 0
res/layout/main.xml View File

@@ -0,0 +1,61 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<LinearLayout android:layout_width="fill_parent"
3
+    android:layout_height="fill_parent"
4
+    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
5
+
6
+    <TextView
7
+        android:id="@+id/score"
8
+        android:layout_width="wrap_content"
9
+        android:layout_height="wrap_content"
10
+        android:layout_gravity="center_horizontal"
11
+        android:text="@string/default_score"
12
+        android:textSize="72pt" />
13
+    
14
+    <TableLayout android:id="@+id/dice" android:layout_width="fill_parent" android:layout_height="wrap_content" >
15
+
16
+        <TableRow android:id="@+id/row1" android:layout_width="wrap_content" android:layout_height="wrap_content" >
17
+
18
+            <ImageView android:id="@+id/die1" style="@style/Die" />
19
+            <ImageView android:id="@+id/die2" style="@style/Die" />
20
+            <ImageView android:id="@+id/die3" style="@style/Die" />
21
+            <ImageView android:id="@+id/die4" style="@style/Die" />
22
+            <ImageView android:id="@+id/die5" style="@style/Die" />
23
+            
24
+        </TableRow>
25
+
26
+        <TableRow android:id="@+id/row2" android:layout_width="wrap_content" android:layout_height="wrap_content" >
27
+            
28
+            <ImageView android:id="@+id/die6" style="@style/Die" />
29
+            <ImageView android:id="@+id/die7" style="@style/Die" />
30
+            <ImageView android:id="@+id/die8" style="@style/Die" />
31
+            <ImageView android:id="@+id/die9" style="@style/Die" />
32
+            <ImageView android:id="@+id/die10" style="@style/Die" />
33
+            
34
+        </TableRow>
35
+    </TableLayout>
36
+    
37
+    
38
+        
39
+        <TableRow
40
+            android:id="@+id/row3" 
41
+        	android:layout_gravity="center_horizontal"
42
+        	android:layout_width="wrap_content"
43
+        	android:layout_height="wrap_content" >
44
+
45
+            <Button
46
+                android:id="@+id/roll"
47
+                android:layout_width="wrap_content"
48
+                android:layout_height="wrap_content"
49
+                android:layout_marginTop="4dip"
50
+                android:text="@string/roll" />
51
+            
52
+            <Button
53
+                android:id="@+id/reset"
54
+                android:layout_width="wrap_content"
55
+                android:layout_height="wrap_content"
56
+                android:layout_marginTop="4dip"
57
+                android:text="@string/reset" />
58
+            
59
+        </TableRow>
60
+
61
+</LinearLayout>

+ 11
- 0
res/values/strings.xml View File

@@ -0,0 +1,11 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<resources>
3
+
4
+    <string name="hello">Hello World, DiceUpActivity!</string>
5
+    <string name="app_name">DiceUp</string>
6
+    <string name="default_score">0</string>
7
+    <string name="roll">Dice Up</string>
8
+    <string name="hold">Hold</string>
9
+    <string name="reset">Reset</string>
10
+
11
+</resources>

+ 9
- 0
res/values/styles.xml View File

@@ -0,0 +1,9 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<resources>
3
+    <style name="Die">
4
+        <item name="android:layout_gravity">center_horizontal</item>
5
+        <item name="android:layout_width">fill_parent</item>
6
+        <item name="android:layout_height">wrap_content</item>
7
+        <item name="android:src">@drawable/green_sticker</item>
8
+    </style>
9
+</resources>

+ 79
- 0
src/com/ianonavy/diceup/DiceUpActivity.java View File

@@ -0,0 +1,79 @@
1
+package com.ianonavy.diceup;
2
+
3
+import android.app.Activity;
4
+import android.os.Bundle;
5
+import android.util.Log;
6
+import android.view.View;
7
+import android.view.View.OnClickListener;
8
+import android.widget.Button;
9
+import android.widget.TextView;
10
+
11
+public class DiceUpActivity extends Activity {
12
+    
13
+	Die[] dice;
14
+	Button roll, hold, reset;
15
+	TextView score;
16
+	
17
+	/** Called when the activity is first created. */
18
+    @Override
19
+    public void onCreate(Bundle savedInstanceState) {
20
+        super.onCreate(savedInstanceState);
21
+        setContentView(R.layout.main);
22
+        
23
+        dice = new Die[10];
24
+        for (int i = 0; i < 10; i ++) {
25
+        	dice[i] = new Die(this, i);
26
+        }
27
+        roll = (Button) findViewById(R.id.roll);
28
+        reset = (Button) findViewById(R.id.reset);
29
+        
30
+        score = (TextView) findViewById(R.id.score);
31
+        
32
+        roll.setOnClickListener(new OnClickListener() {
33
+			@Override
34
+			public void onClick(View view) {
35
+				int numGreenLast = 0;
36
+				int numGreen = 0;
37
+				int numRed = 0;
38
+				
39
+				for (Die die : dice) {
40
+					if (die.isGreen()) {
41
+						numGreenLast++;
42
+						die.roll();
43
+						
44
+						if (die.isRed()) {
45
+							numRed++;
46
+						}
47
+					}
48
+					
49
+					if (die.isGreen()) {
50
+						numGreen++;
51
+					}
52
+				}
53
+				
54
+				score.setText("" + (Integer.parseInt(score.getText().toString()) + numGreen));
55
+				
56
+				if (numGreenLast == 0) {
57
+					for (Die die : dice) {
58
+						die.reset();
59
+					}
60
+				}
61
+				
62
+				Log.d("numRed", "" + numRed);
63
+				Log.d("numGreenLast", "" + numGreenLast);
64
+				if (numRed == numGreenLast && numRed != 0) {
65
+					score.setText(getText(R.string.default_score));
66
+				}
67
+			}
68
+        });
69
+        reset.setOnClickListener(new OnClickListener() {
70
+        	@Override
71
+			public void onClick(View view) {
72
+				score.setText(getString(R.string.default_score));
73
+				for (Die die : dice) {
74
+					die.reset();
75
+				}
76
+			}
77
+        });
78
+    }    
79
+}

+ 108
- 0
src/com/ianonavy/diceup/Die.java View File

@@ -0,0 +1,108 @@
1
+package com.ianonavy.diceup;
2
+
3
+import android.app.Activity;
4
+import android.widget.ImageView;
5
+
6
+public class Die {
7
+	ImageView image;
8
+	State state;
9
+	
10
+	public enum State {
11
+		RED,
12
+		YELLOW,
13
+		GREEN
14
+	}
15
+	
16
+	int nRed, nYellow, nGreen;
17
+	float numFaces, pRed, pYellow, pGreen;
18
+	boolean reset;
19
+	
20
+	public Die(Activity activity, int imageID) {
21
+		this(activity, 1, 2, 3, imageID);
22
+	}
23
+	
24
+	public Die(Activity activity, int nRed, int nYellow, int nGreen, int imageID) {
25
+		this.nRed = nRed;
26
+		this.nYellow = nYellow;
27
+		this.nGreen = nGreen;
28
+		
29
+		numFaces = nRed + nYellow + nGreen;
30
+		pRed = nRed / numFaces;
31
+		pYellow = nYellow / numFaces;
32
+		pGreen = nGreen / numFaces;
33
+		
34
+		image = (ImageView) activity.findViewById(getDieID(imageID));
35
+		state = State.GREEN;
36
+		
37
+		reset = true;
38
+	}
39
+	
40
+	public void roll() {
41
+		reset = false;
42
+		double random = Math.random();
43
+		if (random < pRed) {
44
+			setRed();
45
+		} else if (random < pRed + pYellow) {
46
+			setYellow();
47
+		} else {
48
+			setGreen();
49
+		}
50
+	}
51
+	
52
+	public void setRed() {
53
+		this.state = State.RED;
54
+		image.setImageResource(R.drawable.red_sticker);
55
+	}
56
+	
57
+	public void setYellow() {
58
+		this.state = State.YELLOW;
59
+		image.setImageResource(R.drawable.yellow_sticker);
60
+	}
61
+	
62
+	public void setGreen() {
63
+		this.state = State.GREEN;
64
+		image.setImageResource(R.drawable.green_sticker);
65
+	}
66
+	
67
+	public void reset() {
68
+		reset = true;
69
+		setGreen();
70
+	}
71
+	
72
+	public State getState() {
73
+		return this.state;
74
+	}
75
+	
76
+	public int getDieID(int i) {
77
+    	int id = 0;
78
+    	switch (i) {
79
+    		case 0: id = R.id.die1; break;
80
+    		case 1: id = R.id.die2; break;
81
+    		case 2: id = R.id.die3; break;
82
+    		case 3: id = R.id.die4; break;
83
+    		case 4: id = R.id.die5; break;
84
+    		case 5: id = R.id.die6; break;
85
+    		case 6: id = R.id.die7; break;
86
+    		case 7: id = R.id.die8; break;
87
+    		case 8: id = R.id.die9; break;
88
+    		case 9: id = R.id.die10; break;
89
+    	}
90
+    	return id;
91
+    }
92
+
93
+	public boolean isRed() {
94
+		return this.state == State.RED;
95
+	}
96
+	
97
+	public boolean isYellow() {
98
+		return this.state == State.YELLOW;
99
+	}
100
+	
101
+	public boolean isGreen() {
102
+		return this.state == State.GREEN;
103
+	}
104
+	
105
+	public boolean isReset() {
106
+		return reset;
107
+	}
108
+}

Loading…
Cancel
Save