人机猜拳游戏

【程序截屏】


一、


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>



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,我可以帮你实现一个简单的 Python 人机猜拳游戏。以下是代码示例: ```python import random print("欢迎来到人机猜拳游戏!") print("请输入你要出的手势:1-石头,2-剪刀,3-布") # 用户出拳 player = input() while player not in ['1', '2', '3']: print("输入有误,请重新输入!") player = input() player = int(player) # 电脑出拳 computer = random.randint(1, 3) # 判断胜负 if player == computer: print("平局!") elif (player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1): print("恭喜你,你赢了!") else: print("很遗憾,你输了!") ``` 运行程序后,用户需要输入要出的手势,程序会随机生成电脑出的手势,并判断胜负,最后输出结果。你可以根据需要对代码进行修改和优化。 ### 回答2: Python人机猜拳游戏一个简单而有趣的项目。在这个游戏中,用户可以与计算机进行猜拳,通过Python代码实现游戏的进行和结果的显示。 首先,我们需要定义玩家和计算机的选择。石头、剪刀和布分别用数字1、2、3表示。然后,我们需要生成计算机的随机选择,使用random库中的randint函数。用户可以通过输入选择:1代表石头,2代表剪刀,3代表布。 接下来,我们需要比较玩家和计算机的选择。根据猜拳的规则,石头战胜剪刀,剪刀战胜布,布战胜石头。根据这些规则以及用户和计算机的选择,我们可以判断出胜负关系并显示结果。 最后,我们可以给用户一个选择继续游戏还是退出游戏的选项。如果用户选择继续游戏,我们将再次执行游戏的步骤,让用户和计算机继续猜拳。如果用户选择退出游戏,我们将结束游戏。 通过以上的步骤,我们可以实现一个完整的Python人机猜拳游戏。这个游戏不仅可以帮助用户学习代码实现,还能够提供一个娱乐的方式,让用户和计算机进行互动。希望这个简要的回答能够对你有所帮助。 ### 回答3: 人机猜拳游戏是一种简单而有趣的游戏,可以用Python编写实现。下面是一个基本的人机猜拳游戏的代码示例,具体规则如下: 1. 玩家通过在控制台输入1,2或3来选择出拳,其中1代表剪刀,2代表石头,3代表布。 2. 电脑会随机选择1,2或3作为出拳。 3. 根据游戏规则,剪刀胜布,布胜石头,石头胜剪刀,如果两者一样,则为平局。 4. 最后会输出游戏结果,显示玩家和电脑的出拳以及胜负结果。 以下是代码示例: ```python import random def play_game(player_choice): computer_choice = random.randint(1, 3) if player_choice == computer_choice: result = "平局" elif (player_choice == 1 and computer_choice == 3) or \ (player_choice == 2 and computer_choice == 1) or \ (player_choice == 3 and computer_choice == 2): result = "你赢了!" else: result = "电脑赢了!" choices = ["剪刀", "石头", "布"] print("你的选择:", choices[player_choice - 1]) print("电脑的选择:", choices[computer_choice - 1]) print("结果:", result) print("欢迎来到人机猜拳游戏!") print("请按下面方式选择:") print("1.剪刀") print("2.石头") print("3.布") player_choice = int(input("请选择(输入相应数字):")) if player_choice < 1 or player_choice > 3: print("请输入有效的选择!") else: play_game(player_choice) ``` 通过运行上述代码,就可以开始玩人机猜拳游戏了。玩家输入1、2或3来选择出拳,程序会显示出拳结果和胜负情况。游戏的随机性和简单规则使得整个过程既有趣又富有挑战性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值