android 简易的猜拳小游戏

news/2024/10/25 14:25:45/

    我先把图片贴上来吧。。

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

   

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

下面是代码部分。

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();@Overridepublic 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() {@Overridepublic 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);
好了,就这么多了。。


    

 

 


http://www.ppmy.cn/news/966545.html

相关文章

模拟两人猜拳游戏

模拟两个人猜拳&#xff0c;出石头剪刀布。第一轮大家随机出拳&#xff0c;存储模拟结果&#xff0c;并计算玩家 1 的胜率。改进玩家 1 的出拳策略之后&#xff0c;看对其胜率是否有影响。 主要思路&#xff1a;设计一个策略集合&#xff0c;放置石头剪刀布三种策略。再分别设…

猜拳游戏!

#include <stdio.h> #include <stdlib.h> #include <time.h> void liu(int a[2][3],int i,int j); void sun(int a[2][3],int i,int j); void cc(int a[2][3],int i,int j); //#define A 刘备 //#define B 孙权 //#define C 曹操 int main() {int c;int num[…

Python实现猜拳游戏

在日常休闲时间中&#xff0c;我们经常玩着许多游戏来寻找生活中的乐趣&#xff0c;猜拳游戏是童年记忆中的游戏&#xff0c;也是我们从小学会的第一个游戏。 1 问题 编写代码来完成人工机器猜拳系统 2 方法 首先引入随机数系统&#xff0c;然后设置一个参数代表为我方猜拳&…

PHP--猜拳游戏

此段代码能使计算机随机生成0、1、2三个数中的一个, 恰好能表示计算机的出拳。请编写小游戏,让用户与计算机猜拳。 example9_0.php:<html> <head> <title>猜拳游戏</title> <meta http-equiv"content-type" content"text/html;chars…

【AI理论学习】深入理解Prompt Learning和Prompt Tuning

深入理解Prompt Learning和Prompt Tuning 背景Prompt Learning简介1. Prompt是什么&#xff1f;2. 为什么要使用Prompt&#xff1f;3. Prompt Learning的形式&#xff08;举例&#xff09;4. 有哪些Pre-training language model&#xff1f;5. 常见的Prompt Learning的方法 Pro…

灵活利用ChatAI,帮助你编写文章/故事,提高创作

前言 ChatAI 对于编写文章/故事提供了广泛且有益的帮助。它可以激发灵感、提供背景知识、检查错误并提供实时反馈等功能&#xff0c;在创作过程中起到了重要辅助作用。然而&#xff0c;在使用过程中需要保持适度&#xff0c;并将其视为一个有益工具而非完全取代人类创作的替代…

可以说,今天是中国民营企业家信心暴涨的一天 —— 马云回来了

马云&#xff0c;回国了。 3月27日&#xff0c;据《科创板日报》报道&#xff0c;近日有网友在杭州文一路隧道中目击阿里巴巴集团创始人马云。视频中&#xff0c;马云乘坐一辆丰田考斯特&#xff0c;不时与身边及前面的人交谈。 据《科创板日报》从相关人士处确认&#xff0c;该…

26岁财富自由,30岁已经历成功人士的一生,OpenAI掌门人的36条人生经验

Altman30岁就经历了成功人士的一生:程序员、创始人、投资人、CEO、慈善家。 编辑丨沃特敦 OpenAI掌门人Sam Altman是美国现在最炙手可热的企业家。上周,Altman刚刚度过38岁生日。今天分享的是他30岁时总结的人生经验。那时,Altman已经是硅谷最具人气的创投明星,年纪轻轻就能…