猜拳小游戏
来源:互联网 发布:苹果公司bcg矩阵分析 编辑:程序博客网 时间:2024/04/28 10:52
阶段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" android:background="@drawable/ba" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/padding_medium" android:text="@string/choose" tools:context=".MainActivity" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:text="石头" android:id="@+id/stone" android:checked="true"/> <RadioButton android:text="剪刀" android:id="@+id/scissors"/> <RadioButton android:text="布" android:id="@+id/cloth"/> </RadioGroup> <Button android:id="@+id/click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="出拳" /></LinearLayout>
阶段2:编写电脑类和玩家类
电脑类:
package com.lks.model;public class Computer {public int show(){int quan;quan=(int) (Math.random()*10);if(quan<=2){quan=1;}else if(quan<=5){quan=2;}else{quan=3;}switch(quan){case 1:System.out.println("电脑出拳:石头");break;case 2:System.out.println("电脑出拳:剪刀");break;case 3:System.out.println("电脑出拳:布");break;}return quan;}}
玩家类:
package com.lks.model;public class Player {public int guess(String quan){int num=0;if(quan=="石头"){num=1;}else if(quan=="剪刀"){num=2;}else if(quan=="布"){num=3;}return num;}}
阶段3:编写猜拳游戏类,判定输赢
package com.lks.littlegame;import com.lks.model.Computer;import com.lks.model.Player;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.RadioButton;public class MainActivity extends Activity {private RadioButton stone;private RadioButton scissors;private RadioButton cloth;private Button clickButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); stone=(RadioButton) findViewById(R.id.stone); scissors=(RadioButton) findViewById(R.id.scissors); cloth= (RadioButton) findViewById(R.id.cloth); clickButton=(Button) findViewById(R.id.click); clickButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String choose="";if(stone.isChecked()){choose="石头";}if(scissors.isChecked()){choose="剪刀";}if(cloth.isChecked()){choose="布";}Computer computer=new Computer();Player player=new Player();int a=computer.show();int b=player.guess(choose);String show="";if(a==1&&b==3||a==2&&b==1||a==3&&b==2){show="玩家 赢";}else if(a==1&&b==1||a==2&&b==2||a==3&&b==3){show="平局";}else if(a==3&&b==1||a==1&&b==2||a==2&&b==3){show="电脑 赢";}}}); }
阶段4:页面跳转传值
新建一个Activity,命名为GuessActivity.在layout中自动生成activity_guess.xml.其代码为:
<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:background="@drawable/ba"> <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/padding_medium" tools:context=".GuessActivity" /></RelativeLayout>
在MainActivity中嵌入代码:
Intent intent=new Intent();intent.setClass(MainActivity.this, GuessActivity.class);Bundle bundle=new Bundle();bundle.putString("choose", choose);bundle.putInt("a",a);bundle.putString("show", show);intent.putExtras(bundle);startActivity(intent);
在GuessActivity中接收MainActivity所传的值:
package com.lks.littlegame;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView;public class GuessActivity extends Activity { private TextView result; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_guess); result=(TextView) findViewById(R.id.result); Bundle bundle = this.getIntent().getExtras(); String choose = bundle.getString("choose"); int a=bundle.getInt("a"); String show=bundle.getString("show"); String fist=""; if(a==1){fist="石头";}else if(a==2){fist="剪刀";}else if(a==3){fist="布";} result.setText("玩家:"+choose+"\tVS\t"+"电脑:"+fist+"\n"+show); }
结果显示:
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 猜拳小游戏
- 三角形对象的封装
- 计算机技术课实验报告(5)
- 计算机技术课实验报告(6)
- 黑马程序员__IO流笔记二
- jquery实现文字显隐并改变点击时候的字
- 猜拳小游戏
- iPhone比小米贵3000 贵在哪里?
- 【第11天】得饶人处且饶人,不咄咄逼人
- MySQL Tuner- Author Major Hayden
- 软件驱动工程师面试题
- 23个经典JDK设计模式
- 线程安全
- 设置XP默认登录用户
- IAR Error[Pe141]: unnamed prototyped parameters not allowed when body is present