Commits

a761007 committed d41179d

clear code

Comments (0)

Files changed (10)

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;
-    }
-}

res/drawable-hdpi/icon.png

Removed
Old image

res/drawable-ldpi/icon.png

Removed
Old image

res/drawable-mdpi/icon.png

Removed
Old 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" android:background="#EEEEEE">
-<TextView  
-    android:layout_width="fill_parent" 
-    android:text="@string/player1" android:layout_gravity="center_horizontal" android:layout_height="20px" android:background="#ffff00" android:textColor="#000000"/>
-
-<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" android:background="#00ffff" android:textColor="#000000"></TextView>
-<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" android:textColor="#000000" android:background="#ff0000"></TextView>
-<TextView android:layout_width="160px" android:layout_height="100px" android:id="@+id/TextViewPlayer2Status" android:background="#0fff0f" android:textColor="#000000"></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" android:background="#f0f0f0" android:textColor="#000000"></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; //��q
-	private int agi; //�ӱ�
-	private int atk; //����
-	private int def; //���m
-	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 "���a1�G" + player + "\n" + "�ӱ��G" + agi + "\n" + "��q�G" +
-				hp + "\n" + "�����G" + atk + "\n" + "���m�G" + def +
-				"\n"+ "�{�סG" + 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 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;
-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() {
-    	turn = 0;
-    	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);
-    	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++;
-				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 * randNum / 2;
-    		if (randNum >= player1.getFlee()) {    			
-    			textViewResult.setText(player2.getPlayer() + "攻擊" + 
-    					player1.getPlayer() + "造成傷害" + loss + "\n" + text);
-    			player1.setHp(player1.getHp() - loss);
-    		} else {
-    			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 * randNum / 2;
-    		if (randNum >= player2.getFlee()) {    			
-    			textViewResult.setText(player1.getPlayer() + "攻擊" + 
-    					player2.getPlayer() + "造成傷害" + loss + "\n" + text);
-    			player2.setHp(player2.getHp() - loss);
-    		} else {
-    			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());
-		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); 
-    }
-}