人机猜拳游戏

本文介绍了一个简单的猜拳游戏应用程序的开发过程,包括了主活动界面的创建、用户选择的处理逻辑、电脑随机出拳的实现及游戏结果展示。通过Android平台的Activity交互和Java编程实现了人机对战的猜拳游戏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【程序截屏】


一、


package cn.edu.bzu.caiquan.activity;



import cn.edu.bzu.test.R;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.view.View.OnClickListener;
import android.widget.*;

public class MainActivity extends Activity {
	
	public static final int MENU_ABOUT=1;  //关于菜单
	public static final int MENU_EXIT =2;  //关于菜单
	
	RadioButton shitou;
	RadioButton jianzi;
	RadioButton bu;
	Button chuquan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
             
    }
    
    
    public void init(){
    	shitou = (RadioButton)findViewById(R.id.shitou);
    	jianzi = (RadioButton)findViewById(R.id.jianzi);
    	bu = (RadioButton)findViewById(R.id.bu);
    	
    	chuquan = (Button)findViewById(R.id.dongzuo);
    	chuquan.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				
				int i;
				
				if(shitou.isChecked()) {
					i = 1;
					
				}else if(jianzi.isChecked()){
					i = 2;
					
				}else{
					i = 3;
					
				}

				Intent intent = new Intent();
				intent.putExtra("people", i);
				intent.setClass(MainActivity.this,SecondAcitity.class );
				intent.putExtras(intent);
				startActivity(intent);
			
			}
		});
    }
    
    
   /**
    * 使用自定义对话框
    */
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		
		case MENU_ABOUT:
			LayoutInflater layoutInflater = getLayoutInflater();
			View view = layoutInflater.inflate(R.layout.dialog_item, null);
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("关于")
			.setView(view)
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					
				}
			}).show();
			
			break;
		case MENU_EXIT:
			MainActivity.this.finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

    /**
     * 代码实现菜单
     */
	@Override
    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);
    	menu.add(0, MENU_ABOUT, 1, "关于");
    	menu.add(0, MENU_EXIT, 2, "退出");
        return true;
    }
    
}



二、


package cn.edu.bzu.caiquan.activity;

import cn.edu.bzu.caiquan.lei.Computer;
import cn.edu.bzu.caiquan.lei.People;
import cn.edu.bzu.test.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class SecondAcitity extends Activity{
	
	TextView textView;
	
	 @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.second_acitity);
	        
	        textView = (TextView)findViewById(R.id.TV1);
	        
	        Bundle bundle = this.getIntent().getExtras(); //接受传来的值
	        int chuquan = bundle.getInt("people");
	        
	        People people = new People(chuquan);
	        Computer computer = new Computer();
	        computer.setChuQuFanfShi( computer.chuquan());
	        
	       /**
	        * 为使代码更简洁,可以将下列代码抽出。
	        */
	        
	        int peo = people.getChuQuFanfShi();
	        int com = computer.getChuQuFanfShi();
	        System.out.println("peo="+peo+"  com="+com);
	    	if(peo==1&&com==2||peo==2&&com==3||peo==3&&com==1){
	    		if(peo==1){
	    			textView.setText("玩家:石头"+"VS"+"电脑:剪子");
	    		}
	    		if(peo==2){
	    			textView.setText("玩家:剪子"+"VS"+"电脑:布");
	    		}
	    		if(peo==3){
	    			textView.setText("玩家:布"+"VS"+"电脑:石头");
	    		}
	    		textView.append("\n赢");
			}else if(com==1&&peo==2||com==2&&peo==3||com==3&&peo==1){
				if(peo==1){
	    			textView.setText("玩家:石头"+"VS"+"电脑:布");
	    		}
	    		if(peo==2){
	    			textView.setText("玩家:剪子"+"VS"+"电脑:石头");
	    		}
	    		if(peo==3){
	    			textView.setText("玩家:布"+"VS"+"电脑:剪子");
	    		}
	    		textView.append("\n输");
				
				
			}else if(com==peo||com==peo||com==peo){
				if(peo==1){
	    			textView.setText("玩家:石头"+"VS"+"电脑:石头");
	    		}
	    		if(peo==2){
	    			textView.setText("玩家:剪子"+"VS"+"电脑:剪子");
	    		}
	    		if(peo==3){
	    			textView.setText("玩家:布"+"VS"+"电脑:布");
	    		}
	    		textView.append("\n平");
				
			}
       
	        
	    }

}



三、电脑猜拳


package cn.edu.bzu.caiquan.lei;

import java.util.Random;

public class Computer {
	
	private int chuQuFanfShi;
	
	public int getChuQuFanfShi() {
		return chuQuFanfShi;
	}

	public void setChuQuFanfShi(int chuQuFanfShi) {
		this.chuQuFanfShi = chuQuFanfShi;
	}

	public int chuquan(){
		
		Random random = new Random();	
		return  random.nextInt(3)+1;
	}

}


四、人猜拳


package cn.edu.bzu.caiquan.lei;


public class People {
	
	private int chuQuFanfShi;
	
	public People(int chuQuFanfShi){
		this.chuQuFanfShi = chuQuFanfShi;
		
	}
	
	
	public int getChuQuFanfShi() {
		return chuQuFanfShi;
	}

	public void setChuQuFanfShi(int chuQuFanfShi) {
		this.chuQuFanfShi = chuQuFanfShi;
	}
	
	
	

}


五、


<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/moshi" />
    <RadioGroup
        android:id="@+id/chuquan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_marginTop="30dp"
        android:orientation="horizontal">
        <RadioButton 
            android:id="@+id/shitou"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/shitou"
            android:typeface="serif"
            android:textSize="18sp"
            android:textColor="#001122"/>
        <RadioButton 
            android:id = "@+id/jianzi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:typeface="sans"
            android:text="@string/jianzi"
            android:textSize="18sp"/>
        <RadioButton 
            android:id="@+id/bu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/bu"
            android:textSize="18sp"/>
    </RadioGroup>
    <Button 
        android:id="@+id/dongzuo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="120dp"
        android:layout_centerHorizontal="true"
        android:text="@string/chuquan"
        android:textSize="30sp"
        android:textColor="#00EECC"/>
</RelativeLayout>


六、


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ImageView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/tupian"/>
    
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:textSize="30sp"
        android:text="作者:XX"/>
       <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="版本:1.0.0.10"/>
  

</LinearLayout>


七、


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    
    <TextView 
        android:id="@+id/TV1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>



内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核和精密轨到轨输出放大器。该器件仅体积小巧、功耗低,而且具有广泛的输入电压范围和快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项和误差源分析,旨在帮助工程师更好地理解和应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师和技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间和转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收和泄漏电流等因素对测量准确性的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值