猜拳小游戏

本程序通过控制单选按钮来表示认出的拳,点击出拳,将在另一个界面,显示比赛结果。

 

程序的主界面

 

结果显示界面:

 

 

 

主界面activity_main.xml布局代码

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/you" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RadioGroup
            android:id="@+id/select "
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/knife"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/jiandao" />
            <RadioButton
                android:id="@+id/stone"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/shitou" />
            <RadioButton
                android:id="@+id/cloth"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/bu" />
        </RadioGroup>
    </LinearLayout>
    <Button android:id="@+id/quan" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/quan"/>

</LinearLayout>


结果显示界面activity_show.xml布局代码:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/people"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/people" />

        <TextView
            android:id="@+id/speople"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="" />
         <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="VS" />

        <TextView
            android:id="@+id/computer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/computer" />

        <TextView
            android:id="@+id/scomputer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="" />
    </LinearLayout>

    <TextView
        android:id="@+id/end"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" />

    <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/back" />

</LinearLayout>


程序主要代码:

model层的主要代码:

 

compute类的主要代码:

 

package bzu.example.model;

public class Compute {
	String name="电脑";
	int score=0;
	public static int showFist(){
		int show=(int)(Math.random()*10)%3+1;
		return show;
	}
}


Text类的主要代码:

 

package bzu.example.model;

public class Text {
    public static String start(int perShow,int comShow){

	if(perShow==comShow){
		
        return "平局";
	}else if(perShow==1&&comShow==2||perShow==2&&comShow==3||perShow==3&&comShow==1){
		
		return "电脑赢";
	}else if(perShow==2&&comShow==1||perShow==3&&comShow==2||perShow==1&&comShow==3){
		
		return "玩家赢";
		
	}
	return null;
    }
}


activity层的主要代码:

 

mainactivity类的主要代码 :

 

package bzu.example.activity;

import bzu.example.model.Compute;
import bzu.example.model.Text;
//import bzu.example.mybidy.MainActivity;
//import bzu.example.mybidy.Sactivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {
	
	private RadioGroup group;
	private RadioButton stone;
	private RadioButton knife;
	private RadioButton cloth;
	private Button quan;
	int choose=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initial();
		Intent intent=getIntent();
		String man1=intent.getStringExtra("man");
		if(man1!=null&&man1.equals("剪刀")){
			knife.setChecked(true);
		}else if(man1!=null&&man1.equals("石头")){
			stone.setChecked(true);
		}else if(man1!=null&&man1.equals("布")){
			cloth.setChecked(true);
		}
		group.setOnCheckedChangeListener(new select());
		quan.setOnClickListener(new click());			
		
	}
	public void initial(){
		group=(RadioGroup) findViewById(R.id.select);
		stone=(RadioButton) findViewById(R.id.stone);
		knife=(RadioButton) findViewById(R.id.knife);
		cloth=(RadioButton) findViewById(R.id.cloth);
		quan=(Button) findViewById(R.id.quan);
	}
    public class select implements OnCheckedChangeListener{
    	public void onCheckedChanged(RadioGroup group, int checkedId) {
    		// TODO Auto-generated method stub
    		if(checkedId==knife.getId())
			{
				choose=1;
				
			}else if(checkedId==stone.getId())
			{
				choose=2;
				
			}else if(checkedId==cloth.getId())
			{
				choose=3;	
			}
    	}
    }
    
    public class click implements android.view.View.OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String pc="";
			String man="";
			String cop="";
		    int show=Compute.showFist();
			Intent intent=new Intent();
			intent.setClass(MainActivity.this,Show.class);
			//根据不同的值,传递不同的参数
			if(choose==1){
			pc=	Text.start(choose,show);
			man=knife.getText().toString();
			}else if(choose==2){
			pc=	Text.start(choose,show);
			man=stone.getText().toString();
			}else if(choose==3){
			pc=	Text.start(choose,show);
			man=cloth.getText().toString();
			}
			//电脑返回值代表的拳
		    if(show==1)
		    {
		    	cop="剪刀";
		    }else if(show==2){
		    	cop="石头";
		    }else if(show==3){
		    	cop="布";
		    }
			intent.putExtra("pe",man);
			intent.putExtra("co",cop);
			intent.putExtra("pc",pc);
			startActivity(intent);
		}

    }
 
    
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	 }
	
}


显示界面show类的主要代码:

 

package bzu.example.activity;

//import bzu.example.mybidy.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Show extends Activity {
    
	
	private TextView text1;
	private TextView text2;
	private TextView text3;
	private Button back;
	String man;
	String com;
	String end;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show);
		text1=(TextView) findViewById(R.id.speople);
        text2=(TextView) findViewById(R.id.scomputer);
        text3=(TextView) findViewById(R.id.end);
        back=(Button) findViewById(R.id.back);
        Intent intent=getIntent();
	    man=intent.getStringExtra("pe");
		com=intent.getStringExtra("co");
	    end=intent.getStringExtra("pc");
	    text1.setText(man);
		text2.setText(com);
		text3.setText(end);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				intent.setClass(Show.this,MainActivity.class);
				intent.putExtra("man", man);
				startActivity(intent);
			}
		});
	}
    
//	public class click implements OnClickListener{
//		public void onClick(View v) {
//			// TODO Auto-generated method stub
//			Intent intent=new Intent();
//			intent.setClass(Show.this,MainActivity.class);
//			intent.putExtra("man", man);
//			startActivity(intent);
//		}
//	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.show, menu);
		return true;
	}
	

}


又做了一个小程序,感觉好开心,相信自己通过以后的学习会学到更多,加油吧,继续努力!!

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值