“石頭剪刀布”誰都玩過,趕快做個小遊戲和電腦玩吧!
一:佈局:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="@dimen/padding_medium"
android:text="請您選擇要出的拳"
tools:context=".MainActivity" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="剪刀" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石頭" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布" />
</RadioGroup>
<Button
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/radioGroup1"
android:layout_marginTop="26dp"
android:text="出拳" />
</RelativeLayout>
二:實現類:
1)電腦類:
public class Computer {
public int show(){
int quan;
quan=(int) (Math.random()%0.3);
if(quan==0){
quan=1;
}
if(quan==1){
quan=2;
}
if(quan==2){
quan=3;
}
switch(quan){
case 1:System.out.println("電腦出拳:石頭");break;
case 2:System.out.println("電腦出拳:剪刀");break;
case 3:System.out.println("電腦出拳:布");break;
}
return quan;
}
}
2)玩家類:
package w3.dyp.model; public class Player { public int play(String choose) { int number; if (choose == "剪刀") { number = 1; } else if (choose == "石頭") { number = 2; } else { number = 3; } return number; } }
三:主要編輯類:
MainActivity:
public class MainActivity extends Activity {
private TextView textview;
private RadioButton radio0;
private RadioButton radio1;
private RadioButton radio2;
private Button click;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radio0 = (RadioButton) findViewById(R.id.radio0);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio2 = (RadioButton) findViewById(R.id.radio2);
click =(Button)findViewById(R.id.click);
click.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String choose="";
if (radio0.isChecked()) {
choose = "剪刀";
}
if(radio1.isChecked()){
choose="石頭";
}
if(radio2.isChecked()){
choose="布";
}
Player player=new Player();
Computer computer=new Computer();
int a=player.play(choose);
int b=computer.show();
String result="";
if(a>b){
result="玩家贏";
}else if(a==b){
result="平局";
}else {
result="電腦贏";
}
Toast.makeText(MainActivity.this,"玩家出"+choose+""+result,Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
劃線部分,能讓結果顯示在主頁面上而不用跳轉傳值。
跳轉傳值時,需要把劃線地方刪了,再做以下修改:
1)新建一個Activity類,修改其自動生成的佈局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_medium"
tools:context=".ShowActivity" />
</RelativeLayout>
2)在MainActivity中添加:
<SPAN style="FONT-SIZE: 12px">Intent intent=new Intent();
intent.setClass(MainActivity.this, ShowActivity.class);
Bundle bundle=new Bundle();
bundle.putString("choose", choose);
bundle.putInt("a",a);
bundle.putString("reslt", result);
intent.putExtras(bundle);
startActivity(intent);</SPAN>
3)ShowActivity類:
package w3.dyp.xiaogame;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class ShowActivity extends Activity {
private TextView result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
result = (TextView) findViewById(R.id.result);
Bundle bundle = this.getIntent().getExtras();
String choose = bundle.getString("choose");
int a = bundle.getInt("a");
String show = bundle.getString("show");
String fist = "";
if (a == 1) {
fist = "剪刀";
} else if (a == 2) {
fist = "石頭";
} else if (a == 3) {
fist = "布";
}
result.setText("玩家:" + choose + "\tVS\t" + "電腦:" + fist + "\n" + show);
}
}
結果顯示:
剛發現沒截圖,截上圖就舒服多了,也好看了!