Commits

a761007 committed 521f054

add autoattack v1.5

Comments (0)

Files changed (6)

-<?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>
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="src" path="gen"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

.settings/org.eclipse.core.resources.prefs

+#Fri Oct 22 10:39:34 CST 2010
+eclipse.preferences.version=1
+encoding//src/demo/tbsg/TBSG.java=UTF-8

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;
-    }
-}
+/* 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;
+    }
+}

res/layout/main.xml

     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>
+<EditText android:layout_width="fill_parent" android:id="@+id/EditTextPlayer1" android:hint="請輸入玩家1名稱" android:layout_gravity="center_horizontal" android:layout_height="50px" android:singleLine="true"></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>
+<EditText android:layout_width="fill_parent" android:id="@+id/EditTextPlayer2" android:hint="請輸入玩家2名稱" android:layout_gravity="center_horizontal" android:layout_height="50px" android:singleLine="true"></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>

src/demo/tbsg/Player.java

 
 public class Player {
 	private String player;
-	private int hp; //血量
-	private int agi; //敏捷
-	private int atk; //攻擊
-	private int def; //防禦
-	private int flee; //閃躲
+	private int hp; //��q
+	private int agi; //�ӱ�
+	private int atk; //����
+	private int def; //���m
+	private int flee; //�{��
 
 	private Player() {};
 	
 	}
 
 	public String getPlayerStatus() {
-		return "玩家:" + player + "\n" + "敏捷:" + agi + "\n" + "血量:" +
-				hp + "\n" + "攻擊力:" + atk + "\n" + "防禦力:" + def +
-				"\n"+ "閃避率:" + flee + "\n";
+		return "���a1�G" + player + "\n" + "�ӱ��G" + agi + "\n" + "��q�G" +
+				hp + "\n" + "�����G" + atk + "\n" + "���m�G" + def +
+				"\n"+ "�{�סG" + flee + "\n";
 	}
 	
 	public String getPlayer() {

src/demo/tbsg/TBSG.java

 package demo.tbsg;
 
 import java.util.Random;
+import java.util.Timer;
+import java.util.TimerTask;
 
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.os.Bundle;
+import android.os.Handler;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 					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;
-			    	}					
-				} 
+				textViewResult.setText("");
+				player1 = new Player(editTextPlayer1.getText().toString());
+				player2 = new Player(editTextPlayer2.getText().toString());
+				textViewPlayer1Status.setText(player1.getPlayerStatus());
+				textViewPlayer2Status.setText(player2.getPlayerStatus());
+				editTextPlayer1.setEnabled(false);
+				editTextPlayer2.setEnabled(false);
+				if (player1.getAgi() > player2.getAgi()) {
+		    		// p1 first
+		    		state = false;
+		    		
+		    	} else {
+		    		// p2 first
+		    		state = true;
+		    	}					
+				button.setEnabled(false);
 				attack();
 			}
     		
     public void attack() {
     	int loss;
     	int randNum = new Random().nextInt(101);
+    	String text = textViewResult.getText().toString();
     	if (state) {
     		loss = (player2.getAtk() - player1.getDef())>0? 
-    				player2.getAtk() - player1.getDef():1;
+    				player2.getAtk() - player1.getDef():1 * randNum / 2;
     		if (randNum >= player1.getFlee()) {    			
-    			textViewResult.append(player2.getPlayer() + "攻擊" + 
-    					player1.getPlayer() + "照成傷害" + loss + "\n");
+    			textViewResult.setText(player2.getPlayer() + "攻擊" + 
+    					player1.getPlayer() + "造成傷害" + loss + "\n" + text);
     			player1.setHp(player1.getHp() - loss);
     		} else {
-    			textViewResult.append(player1.getPlayer() + "避開" + 
-    					player2.getPlayer() + "攻擊碧照成其傷害" + loss / 2 + "\n");
-    			player2.setHp(player2.getHp() - loss);   			
+    			textViewResult.setText(player1.getPlayer() + "躲開" + 
+    					player2.getPlayer() + "攻擊並造成其傷害" + loss / 2 + 
+    					"\n" + text);
+    			player2.setHp(player2.getHp() - loss / 2);   			
     		}
     	} else {
     		loss = (player1.getAtk() - player2.getDef())>0? 
-    				player1.getAtk() - player2.getDef():1;
+    				player1.getAtk() - player2.getDef():1 * randNum / 2;
     		if (randNum >= player2.getFlee()) {    			
-    			textViewResult.append(player1.getPlayer() + "攻擊" + 
-    					player2.getPlayer() + "照成傷害" + loss + "\n");
+    			textViewResult.setText(player1.getPlayer() + "攻擊" + 
+    					player2.getPlayer() + "造成傷害" + loss + "\n" + text);
     			player2.setHp(player2.getHp() - loss);
     		} else {
-    			textViewResult.append(player2.getPlayer() + "避開" + 
-    					player1.getPlayer() + "攻擊碧照成其傷害" + loss / 2 + "\n");
-    			player1.setHp(player1.getHp() - loss);
+    			textViewResult.setText(player2.getPlayer() + "躲開" + 
+    					player1.getPlayer() + "攻擊並造成其傷害" + 
+    					loss / 2 + "\n" + text);
+    			player1.setHp(player1.getHp() - loss / 2);
     		}
     	}
     	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;
-    	}
-    	
+		if (player1.getHp() <= 0 || player2.getHp() <= 0) {
+			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));
+			editTextPlayer1.setEnabled(true);
+			editTextPlayer2.setEnabled(true);
+			button.setEnabled(true);
+			turn = 0;
+			return;
+		}
+		final Handler handler = new Handler(); 
+        Timer t = new Timer(); 
+        t.schedule(new TimerTask() { 
+                public void run() { 
+                        handler.post(new Runnable() { 
+                                public void run() { 
+                                	attack();
+                            		turn++; 
+                            		button.setText("第" + turn + "回合");
+                                } 
+                        }); 
+                } 
+        }, 200); 
     }
-    
-    
 }