Anonymous avatar Anonymous committed 8188b0e

first commit and its ver 1.0

Comments (0)

Files changed (13)

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="src" path="gen"/>
+	<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
+glob:bin/*
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>tbsg</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>com.android.ide.eclipse.adt.ApkBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

AndroidManifest.xml

+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+      package="demo.tbsg"
+      android:versionCode="1"
+      android:versionName="1.0">
+    <application android:icon="@drawable/icon" android:label="@string/app_name">
+        <activity android:name=".TBSG"
+                  android:label="@string/app_name">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+
+    </application>
+
+
+</manifest> 

default.properties

+# 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 use,
+# "build.properties", and override values to adapt the script to your
+# project structure.
+
+# Project target.
+target=android-4

gen/demo/tbsg/R.java

+/* AUTO-GENERATED FILE.  DO NOT MODIFY.
+ *
+ * This class was automatically generated by the
+ * aapt tool from the resource data it found.  It
+ * should not be modified by hand.
+ */
+
+package demo.tbsg;
+
+public final class R {
+    public static final class attr {
+    }
+    public static final class drawable {
+        public static final int icon=0x7f020000;
+    }
+    public static final class id {
+        public static final int Button=0x7f050002;
+        public static final int EditTextPlayer1=0x7f050000;
+        public static final int EditTextPlayer2=0x7f050001;
+        public static final int LinearLayout01=0x7f050003;
+        public static final int TextViewPlayer1Status=0x7f050004;
+        public static final int TextViewPlayer2Status=0x7f050005;
+        public static final int TextViewResult=0x7f050006;
+    }
+    public static final class layout {
+        public static final int main=0x7f030000;
+    }
+    public static final class string {
+        public static final int app_name=0x7f040001;
+        public static final int end=0x7f040004;
+        public static final int loser=0x7f040006;
+        public static final int player1=0x7f040000;
+        public static final int player2=0x7f040002;
+        public static final int start=0x7f040003;
+        public static final int stop=0x7f040007;
+        public static final int winner=0x7f040005;
+    }
+}
Add a comment to this file

res/drawable-hdpi/icon.png

Added
New image
Add a comment to this file

res/drawable-ldpi/icon.png

Added
New image
Add a comment to this file

res/drawable-mdpi/icon.png

Added
New image

res/layout/main.xml

+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent"
+    >
+<TextView  
+    android:layout_width="fill_parent" 
+    android:text="@string/player1" android:layout_gravity="center_horizontal" android:layout_height="20px"/>
+
+<EditText android:layout_width="fill_parent" android:id="@+id/EditTextPlayer1" android:hint="請輸入玩家1名稱" android:layout_gravity="center_horizontal" android:layout_height="50px"></EditText>
+<TextView android:layout_width="fill_parent" android:text="@string/player2" android:layout_gravity="center_horizontal" android:layout_height="20px"></TextView>
+<EditText android:layout_width="fill_parent" android:id="@+id/EditTextPlayer2" android:hint="請輸入玩家2名稱" android:layout_gravity="center_horizontal" android:layout_height="50px"></EditText>
+
+<Button android:id="@+id/Button" android:text="@string/start" android:layout_width="fill_parent" android:layout_height="50px"></Button><LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"><TextView android:layout_height="100px" android:layout_width="160px" android:id="@+id/TextViewPlayer1Status"></TextView>
+<TextView android:layout_width="160px" android:layout_height="100px" android:id="@+id/TextViewPlayer2Status"></TextView>
+</LinearLayout><TextView android:layout_width="fill_parent" android:layout_weight="8" android:id="@+id/TextViewResult" android:scrollbars="vertical" android:layout_height="290px" android:scrollHorizontally="true"></TextView>
+
+
+</LinearLayout>

res/values/strings.xml

+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <string name="player1">玩家1</string>
+    <string name="app_name">turn-based strategy</string>
+<string name="player2">玩家2</string>
+<string name="start">開始戰鬥</string>
+<string name="end">戰鬥結束</string>
+<string name="winner">勝利者</string>
+<string name="loser">失敗者</string>
+<string name="stop">戰鬥暫停</string>
+</resources>

src/demo/tbsg/Player.java

+package demo.tbsg;
+
+public class Player {
+	private String player;
+	private int hp; //血量
+	private int agi; //敏捷
+	private int atk; //攻擊
+	private int def; //防禦
+	private int flee; //閃躲
+
+	private Player() {};
+	
+	public Player(String p) {
+		player = p;
+		initialize();
+	}
+	
+	private void initialize() {
+		int tototal = 0;
+		char buffer[] = new char[player.length()];
+		player.getChars(0, player.length()-1, buffer, 0);
+		for (int i :buffer) {
+			tototal += i;
+		}
+		hp = tototal * 99 % 500;
+		agi = tototal * 99 % 100;
+		atk = tototal * 59 % 89;
+		def = tototal * 89 % 79;
+		flee = tototal * 9 % 100;
+	}
+
+	public String getPlayerStatus() {
+		return "玩家:" + player + "\n" + "敏捷:" + agi + "\n" + "血量:" +
+				hp + "\n" + "攻擊力:" + atk + "\n" + "防禦力:" + def +
+				"\n"+ "閃避率:" + flee + "\n";
+	}
+	
+	public String getPlayer() {
+		return player;
+	}
+
+	public void setPlayer(String player) {
+		this.player = player;
+	}
+	
+	public void setHp(int hp) {
+		this.hp = hp;
+	}
+
+	public int getHp() {
+		return hp;
+	}
+
+	public void setAgi(int agi) {
+		this.agi = agi;
+	}
+
+	public int getAgi() {
+		return agi;
+	}
+
+	public void setAtk(int atk) {
+		this.atk = atk;
+	}
+
+	public int getAtk() {
+		return atk;
+	}
+
+	public void setDef(int def) {
+		this.def = def;
+	}
+
+	public int getDef() {
+		return def;
+	}
+
+	public void setFlee(int flee) {
+		this.flee = flee;
+	}
+
+	public int getFlee() {
+		return flee;
+	}
+}

src/demo/tbsg/TBSG.java

+package demo.tbsg;
+
+import java.util.Random;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.TextView;
+
+public class TBSG extends Activity {
+    /** Called when the activity is first created. */
+	EditText editTextPlayer1;
+	EditText editTextPlayer2;
+	TextView textViewPlayer1Status;
+	TextView textViewPlayer2Status;
+	Button button;
+    TextView textViewResult;
+	private Boolean state;
+	private Player player1;
+	private Player player2;
+	private int turn;
+	
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.main);
+        initialize();
+    }
+    
+    private void initialize() {
+    	editTextPlayer1 = (EditText) findViewById(R.id.EditTextPlayer1);
+    	editTextPlayer2 = (EditText) findViewById(R.id.EditTextPlayer2);
+    	
+    	textViewPlayer1Status = 
+    		(TextView) findViewById(R.id.TextViewPlayer1Status);
+    	textViewPlayer2Status = 
+    		(TextView) findViewById(R.id.TextViewPlayer2Status);
+    	textViewResult = (TextView) findViewById(R.id.TextViewResult);
+    	turn = 0;
+    	button = (Button) findViewById(R.id.Button);
+    	button.setOnClickListener(new View.OnClickListener() {
+
+			@Override
+			public void onClick(View v) {
+				// TODO Auto-generated method stub
+				String p1 = editTextPlayer1.getText().toString();
+				String p2 = editTextPlayer2.getText().toString();
+				if (p1.equalsIgnoreCase("") || p2.equalsIgnoreCase("")) {
+					return;
+				}
+				turn++;
+				button.setText("第" + turn + "回合");
+				if (turn == 1) {
+					textViewResult.setText("");
+					player1 = new Player(editTextPlayer1.getText().toString());
+					player2 = new Player(editTextPlayer2.getText().toString());
+					textViewPlayer1Status.setText(player1.getPlayerStatus());
+					textViewPlayer2Status.setText(player2.getPlayerStatus());
+					if (player1.getAgi() > player2.getAgi()) {
+			    		// p1 first
+			    		state = false;
+			    		
+			    	} else {
+			    		// p2 first
+			    		state = true;
+			    	}					
+				} 
+				attack();
+			}
+    		
+    	});
+    }
+    
+    public void attack() {
+    	int loss;
+    	int randNum = new Random().nextInt(101);
+    	if (state) {
+    		loss = (player2.getAtk() - player1.getDef())>0? 
+    				player2.getAtk() - player1.getDef():1;
+    		if (randNum >= player1.getFlee()) {    			
+    			textViewResult.append(player2.getPlayer() + "攻擊" + 
+    					player1.getPlayer() + "照成傷害" + loss + "\n");
+    			player1.setHp(player1.getHp() - loss);
+    		} else {
+    			textViewResult.append(player1.getPlayer() + "避開" + 
+    					player2.getPlayer() + "攻擊碧照成其傷害" + loss / 2 + "\n");
+    			player2.setHp(player2.getHp() - loss);   			
+    		}
+    	} else {
+    		loss = (player1.getAtk() - player2.getDef())>0? 
+    				player1.getAtk() - player2.getDef():1;
+    		if (randNum >= player2.getFlee()) {    			
+    			textViewResult.append(player1.getPlayer() + "攻擊" + 
+    					player2.getPlayer() + "照成傷害" + loss + "\n");
+    			player2.setHp(player2.getHp() - loss);
+    		} else {
+    			textViewResult.append(player2.getPlayer() + "避開" + 
+    					player1.getPlayer() + "攻擊碧照成其傷害" + loss / 2 + "\n");
+    			player1.setHp(player1.getHp() - loss);
+    		}
+    	}
+    	state = !state;
+    	textViewPlayer1Status.setText(player1.getPlayerStatus());
+		textViewPlayer2Status.setText(player2.getPlayerStatus());
+    	// check end
+    	if (player1.getHp() <= 0 || player2.getHp() <= 0) {
+    		// alert
+    		String winner = player1.getHp() <= 0?
+    				player2.getPlayer():player1.getPlayer();
+    		new AlertDialog.Builder(TBSG.this)
+    		.setTitle("訊息")
+    		.setPositiveButton("確定", null)
+    		.setMessage(getString(R.string.winner) + 
+    				winner + "在第"+turn+"回合獲勝")
+    		.show();
+    		button.setText(getString(R.string.start));
+    		textViewPlayer1Status.setText("");
+    		textViewPlayer2Status.setText("");
+    		textViewResult.setText("");
+    		editTextPlayer1.setText("");
+    		editTextPlayer2.setText("");
+    		turn = 0;
+    	}
+    	
+    }
+    
+    
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.