android 简易的猜拳小游戏

    我先把图片贴上来吧。。

    选好你要出的,之后结果显示为

   

电脑出什么是随机的。。。。

下面是代码部分。

1.布局:

<LinearLayout 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:orientation="vertical"
    >

    <RadioGroup 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        
        <RadioButton 
            android:id="@+id/shitou"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="石头"
            android:checked="true"
            />
        
        <RadioButton 
            android:id="@+id/jiandao"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="剪刀"
            />
        <RadioButton 
            android:id="@+id/bu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="布"
            />
    </RadioGroup>
    <Button 
        android:id="@+id/chuquan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="出拳"
        />
</LinearLayout>

2.Activity 层

	RadioButton shitou,jiandao,bu;
	Button chuquan;
	int user,diannao;
	String [] xianshi = new String[3];
	Random random = new Random();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        shitou = (RadioButton) findViewById(R.id.shitou);
        jiandao = (RadioButton) findViewById(R.id.jiandao);
        bu = (RadioButton) findViewById(R.id.bu);
        chuquan = (Button) findViewById(R.id.chuquan);
        chuquan.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				diannao = Math.abs(random.nextInt() % 3);
				user = 1;
				String userxinxi = null;
				if(shitou.isChecked()){  
					xianshi[0] = "剪刀";
					xianshi[1] = "石头";
					xianshi[2] = "布";
					userxinxi = "石头";
				}else if (jiandao.isChecked()) {
					xianshi[0] = "布";
					xianshi[1] = "剪刀";
					xianshi[2] = "石头";
					userxinxi = "剪刀";
				}else if (bu.isChecked()) {
					xianshi[0] = "石头";
					xianshi[1] = "布";
					xianshi[2] = "剪刀";
					userxinxi = "布";
				}
				
				Intent intent = new Intent(MainActivity.this,Result.class);
				intent.setFlags(RESULT_CANCELED);
				if(diannao > user){
					intent.putExtra("result", "玩家:"+userxinxi+"	vs	"+"电脑:"+xianshi[diannao] + "\n电脑赢");
				}else if(diannao == user){
					intent.putExtra("result", "玩家:"+userxinxi+"	vs	"+"电脑:"+xianshi[diannao] + "\n双方是平手");
				}else if(diannao < user){
					intent.putExtra("result", "玩家:"+userxinxi+"	vs	"+"电脑:"+xianshi[diannao] + "\n玩家赢");
				}
				startActivity(intent);
			}
		});
    }

3.显示结果:

Intent intent = getIntent();
        String result = intent.getStringExtra("result");
        TextView jieguo = (TextView) findViewById(R.id.jieguo);
        jieguo.setText(result);
好了,就这么多了。。


    

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值