android 之猜拳游戏练习

2013-05-21 11:28:00

写猜拳游戏的目的主要是练习linerlayout和relativelayout;有三个页面,其中两个是linerlayout的。

在我的资源里已经将代码上传了:http://download.csdn.net/detail/yuexin2/5419533

HumanToComputer.java中:

package com.example.guesshand;

import java.util.Random;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.TextView;

import android.widget.RadioGroup.OnCheckedChangeListener;

public class HumanToComputer extends Activity {

public Button cok;

public RadioButton mstoreOne;

public RadioButton mjiandaoOne;

public RadioButton mbuOne;

public RadioGroup moneRadioGroup;

public TextView cresult;

public ImageView cimgTwo;

public ImageView cimgOne;

private int men=0;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.human_computer);

cresult = (TextView)findViewById(R.id.cresult);

cimgTwo = (ImageView)findViewById(R.id.cimgTwo);

cimgOne = (ImageView)findViewById(R.id.cimgOne);

cok = (Button)findViewById(R.id.cOk);

OnCokClicked occ = new OnCokClicked();

cok.setOnClickListener(occ);

moneRadioGroup = (RadioGroup)findViewById(R.id.moneRadioGroup);

mstoreOne = (RadioButton)findViewById(R.id.mstoreOne);

mjiandaoOne = (RadioButton)findViewById(R.id.mjiandaoOne);

mbuOne = (RadioButton)findViewById(R.id.mbuOne);

OnRadioGroupChange orgc = new OnRadioGroupChange();

moneRadioGroup.setOnCheckedChangeListener(orgc);

}

class OnRadioGroupChange implements OnCheckedChangeListener{

@Override

public void onCheckedChanged(RadioGroup arg0, int arg1) {

if(arg1==R.id.mstoreOne){

men=1;

}

else if(arg1==R.id.mjiandaoOne){

men=2;

}

else if(arg1==R.id.mbuOne){

men=3;

}

}

}

class OnCokClicked implements OnClickListener{

@Override

public void onClick(View v) {

Random rnd = new Random();

int computer = rnd.nextInt(3);

computer++;

System.out.println("电脑:"+computer);

if(men==1){

if(computer==2){

cresult.setText("玩家赢了!!!");

cimgOne.setImageDrawable(getResources().getDrawable(R.drawable.yes));

cimgTwo.setImageDrawable(getResources().getDrawable(R.drawable.no));

}

if(computer==3){

cresult.setText("电脑赢了!!!");

cimgTwo.setImageDrawable(getResources().getDrawable(R.drawable.yes));

cimgOne.setImageDrawable(getResources().getDrawable(R.drawable.no));

}

}

if(men==2){

if(computer==1){

cresult.setText("电脑赢了!!!");

cimgTwo.setImageDrawable(getResources().getDrawable(R.drawable.yes));

cimgOne.setImageDrawable(getResources().getDrawable(R.drawable.no));

}

if(computer==3){

cresult.setText("玩家赢了!!!");

cimgOne.setImageDrawable(getResources().getDrawable(R.drawable.yes));

cimgTwo.setImageDrawable(getResources().getDrawable(R.drawable.no));

}

}

if(men==3){

if(computer==2){

cresult.setText("电脑赢了!!!");

cimgTwo.setImageDrawable(getResources().getDrawable(R.drawable.yes));

cimgOne.setImageDrawable(getResources().getDrawable(R.drawable.no));

}

if(computer==1){

cresult.setText("玩家赢了!!!");

cimgOne.setImageDrawable(getResources().getDrawable(R.drawable.yes));

cimgTwo.setImageDrawable(getResources().getDrawable(R.drawable.no));

}

}

if(men == computer&&men!=0){

cresult.setText("平局!!!");

cimgOne.setImageDrawable(getResources().getDrawable(R.drawable.yes));

cimgTwo.setImageDrawable(getResources().getDrawable(R.drawable.yes));

}

if(men!=0&&computer!=0){

men = 0;

}

}

}

}

HumanToHumanActivity.java文件:

package com.example.guesshand;

import android.app.Activity;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.RadioGroup.OnCheckedChangeListener;

import android.widget.TextView;

public class HumanToHumanActivity extends Activity {

public Button okButton;

public RadioGroup oneRadioGroup;

public RadioGroup twoRadioGroup;

public RadioButton storeTwo;

public RadioButton storeOne;

public RadioButton jiandaoOne;

public RadioButton jiandaoTwo;

public RadioButton buOne;

public RadioButton buTwo;

public TextView result;

public ImageView imgTwo;

public ImageView imgOne;

private int one=0;

private int two=0;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.human_human);

imgTwo = (ImageView)findViewById(R.id.imgTwo);

imgOne = (ImageView)findViewById(R.id.imgOne);

okButton = (Button)findViewById(R.id.ok);

oneRadioGroup = (RadioGroup)findViewById(R.id.oneRadioGroup);

twoRadioGroup = (RadioGroup)findViewById(R.id.twoRadioGroup);

storeTwo = (RadioButton)findViewById(R.id.storeTwo);

jiandaoOne = (RadioButton)findViewById(R.id.jiandaoOne);

storeOne = (RadioButton)findViewById(R.id.storeOne);

jiandaoTwo = (RadioButton)findViewById(R.id.jiandaoTwo);

buOne = (RadioButton)findViewById(R.id.buOne);

buTwo = (RadioButton)findViewById(R.id.buTwo);

result = (TextView)findViewById(R.id.result);

OnOkButtonClick oobc = new OnOkButtonClick();

OnRadioGroupChange orgc = new OnRadioGroupChange();

oneRadioGroup.setOnCheckedChangeListener(orgc);

twoRadioGroup.setOnCheckedChangeListener(orgc);

okButton.setOnClickListener(oobc);

}

class OnOkButtonClick implements OnClickListener{

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

storeTwo.setChecked(false);

storeOne.setChecked(false);

jiandaoOne.setChecked(false);

jiandaoTwo.setChecked(false);

buOne.setChecked(false);

buTwo.setChecked(false);

if(one==1){

if(two==2){

result.setText("用户一赢了!!!");

imgOne.setImageDrawable(getResources().getDrawable(R.drawable.yes));

imgTwo.setImageDrawable(getResources().getDrawable(R.drawable.no));

}

if(two==3){

result.setText("用户二赢了!!!");

imgTwo.setImageDrawable(getResources().getDrawable(R.drawable.yes));

imgOne.setImageDrawable(getResources().getDrawable(R.drawable.no));

}

}

if(one==2){

if(two==1){

result.setText("用户二赢了!!!");

imgTwo.setImageDrawable(getResources().getDrawable(R.drawable.yes));

imgOne.setImageDrawable(getResources().getDrawable(R.drawable.no));

}

if(two==3){

result.setText("用户一赢了!!!");

imgOne.setImageDrawable(getResources().getDrawable(R.drawable.yes));

imgTwo.setImageDrawable(getResources().getDrawable(R.drawable.no));

}

}

if(one==3){

if(two==2){

result.setText("用户二赢了!!!");

imgTwo.setImageDrawable(getResources().getDrawable(R.drawable.yes));

imgOne.setImageDrawable(getResources().getDrawable(R.drawable.no));

}

if(two==1){

result.setText("用户一赢了!!!");

imgOne.setImageDrawable(getResources().getDrawable(R.drawable.yes));

imgTwo.setImageDrawable(getResources().getDrawable(R.drawable.no));

}

}

if(one == two&&one!=0){

result.setText("平局!!!");

imgOne.setImageDrawable(getResources().getDrawable(R.drawable.yes));

imgTwo.setImageDrawable(getResources().getDrawable(R.drawable.yes));

}

if(one!=0&&two!=0){

one = 0;

two = 0;

}

}

}

class OnRadioGroupChange implements OnCheckedChangeListener{

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

// TODO Auto-generated method stub

if(checkedId==R.id.storeOne){

one=1;

}

else if(checkedId==R.id.storeTwo){

two=1;

}

else if(checkedId==R.id.jiandaoTwo){

two=2;

}

else if(checkedId==R.id.jiandaoOne){

one=2;

}

else if(checkedId==R.id.buTwo){

two=3;

}

else if(checkedId==R.id.buOne){

one=3;

}

}

}

}

主文件(MainActivity.java):

package com.example.guesshand;

import android.os.Bundle;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RadioGroup;

import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

public MainActivity mainActivity = this;

public Button buttonGo;

private RadioGroup radioGroup;

public int manOrComputer=-1;

public Bundle savedInstanceState1;

@Override

protected void onCreate(Bundle savedInstanceState) {

savedInstanceState1 = savedInstanceState;

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

buttonGo = (Button)findViewById(R.id.go);

radioGroup = (RadioGroup)findViewById(R.id.checkEnemy);

RadioGroupListener rgl = new RadioGroupListener();

ButtonGoListener bgl = new ButtonGoListener();

buttonGo.setOnClickListener(bgl);

radioGroup.setOnCheckedChangeListener(rgl);

}

//监听rideo

class RadioGroupListener implements OnCheckedChangeListener{

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

// TODO Auto-generated method stub

if(checkedId==R.id.computer){

manOrComputer = 2;

System.out.println(manOrComputer);

}

if(checkedId==R.id.human){

manOrComputer = 1;

System.out.println(manOrComputer);

}

}

}

//监听开始按钮

class ButtonGoListener implements OnClickListener{

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

if(manOrComputer==1){

Intent intent = new Intent();

intent.setClass(mainActivity, HumanToHumanActivity.class);

startActivity(intent);

MainActivity.this.finish();

}

else if(manOrComputer==2){

Intent intent = new Intent();

intent.setClass(mainActivity, HumanToComputer.class);

startActivity(intent);

MainActivity.this.finish();

}else{

System.out.println("没选择");

new AlertDialog.Builder(mainActivity)

.setTitle("错误")

.setMessage("未选择对战对手!!!")

.setPositiveButton("确定", null)

.show();

}

}

}

@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);

return true;

}

}

下面是xml文件:

activity_main.xml:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity" >

android:layout_marginTop="30dp"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="30dp"

android:text="猜拳游戏,岳鑫***" />

android:layout_marginTop="30dp"

android:textSize="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="请选择对手:" />

android:id="@+id/checkEnemy"

android:layout_marginTop="30dp"

android:layout_marginLeft="50dp"

android:textSize="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

android:id="@+id/computer"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="电脑"

/>

android:id="@+id/human"

android:layout_marginLeft="50dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="人"

/>

android:id="@+id/go"

android:layout_marginLeft="100dp"

android:layout_marginTop="30dp"

android:textSize="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="开始游戏" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="30dp"

android:textSize="20dp"

android:text="广告位:

qq:794529075

火热预定中。。。

"

/>

human_computer.xml:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_margin="20dp"

android:orientation="vertical" >

android:id="@+id/cimgOne"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/yes"

/>

android:id="@+id/cimgTwo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/no"

android:layout_toRightOf="@id/cimgOne"

android:layout_marginLeft="50dp"

/>

android:text="玩家"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/man"

android:layout_alignRight="@id/cimgOne"

android:layout_alignLeft="@id/cimgOne"

android:gravity="center"

android:layout_below="@id/cimgOne"

android:textSize="30sp"

/>

android:text="电脑"

android:textSize="30sp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/computer"

android:layout_below="@id/cimgTwo"

android:layout_alignRight="@id/cimgTwo"

android:layout_alignLeft="@id/cimgTwo"

android:gravity="center"

android:layout_toRightOf="@id/man"

/>

android:id="@+id/moneRadioGroup"

android:layout_marginTop="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/man"

>

android:id="@+id/mstoreOne"

android:textSize="30sp"

android:layout_marginLeft="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="石头"

/>

android:id="@+id/mjiandaoOne"

android:textSize="30sp"

android:layout_marginLeft="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="剪刀"

/>

android:id="@+id/mbuOne"

android:textSize="30sp"

android:layout_marginLeft="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="布"

/>

android:id="@+id/twoRadioGroup"

android:layout_marginTop="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/moneRadioGroup"

android:layout_below="@id/computer"

android:layout_marginLeft="30dp"

>

android:id="@+id/storeTwo"

android:textSize="30sp"

android:layout_marginLeft="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="石头"

/>

android:id="@+id/jiandaoTwo"

android:textSize="30sp"

android:layout_marginLeft="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="剪刀"

/>

android:id="@+id/buTwo"

android:textSize="30sp"

android:layout_marginLeft="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="布"

/>

android:id="@+id/cOk"

android:layout_below="@id/moneRadioGroup"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="确定"

android:textSize="30sp"

/>

android:id="@+id/temptext"

android:layout_marginTop="20dp"

android:textSize="15dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="结果:"

android:layout_below="@id/cOk"

/>

android:id="@+id/cresult"

android:textSize="20dp"

android:layout_marginLeft="50dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/temptext"

/>

human_human.xml:

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:orientation="vertical">

android:id="@+id/imgOne"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/yes"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="用户一:"

android:textSize="30dp"

/>

android:id="@+id/oneRadioGroup"

android:layout_marginTop="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

android:id="@+id/storeOne"

android:textSize="30dp"

android:layout_marginLeft="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="石头"

/>

android:id="@+id/jiandaoOne"

android:textSize="30dp"

android:layout_marginLeft="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="剪刀"

/>

android:id="@+id/buOne"

android:textSize="30dp"

android:layout_marginLeft="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="布"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:orientation="vertical">

android:id="@+id/imgTwo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/yes"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="用户二:"

android:textSize="30dp"

/>

android:id="@+id/twoRadioGroup"

android:layout_marginTop="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

android:id="@+id/storeTwo"

android:textSize="30dp"

android:layout_marginLeft="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="石头"

/>

android:id="@+id/jiandaoTwo"

android:textSize="30dp"

android:layout_marginLeft="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="剪刀"

/>

android:id="@+id/buTwo"

android:textSize="30dp"

android:layout_marginLeft="20dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="布"

/>

android:id="@+id/ok"

android:layout_marginTop="30dp"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20dp"

android:text="确定"

/>

android:layout_marginTop="20dp"

android:textSize="15dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="结果:"

/>

android:id="@+id/result"

android:textSize="20dp"

android:layout_marginLeft="50dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

猜拳游戏android报告,android 之猜拳游戏练习相关推荐

  1. java猜数字游戏实验报告_java猜数游戏实验报告.doc

    java猜数游戏实验报告 课 程 设 计 报 告 课程设计名称 Java程序设计-猜数游戏 指导教师 钟世刚 专业 班级 信息安全 学 号 姓 名 成 绩 一.设计任务与要求1 1.1 设计任务与要求 ...

  2. c语言打字游戏程序设计报告,打字游戏程序设计报告.doc

    打字游戏程序设计报告 第二章 打字游戏程序设计内容要求和设计思想 2.1 设计内容要求 要求设计的程序需包括:在游戏开始后,按回车键进入打字游戏:按ESC键返回主界面:按空格暂停:按大写字母E程序结束 ...

  3. 国内首份功能游戏产业报告(中娱智库发布)

    文章主要对中娱智库发布的<2020功能游戏产业报告>做一个个人笔记. 原报告的全文和下载链接见下面(全文要7000多字,阅读时间10分钟即可): https://zhuimeng.qq.c ...

  4. 核心游戏大作销量超预期,游戏巨头Take-Two已经回春?

    8月6日,游戏厂商巨头Take-Two对外发布了它在新一季度的新财报.根据财报来看,它在营收跟净利润这两方面都保持同比增长,尤其是核心游戏大作NBA 2K.<侠盗飞车5>和<荒野大镖 ...

  5. Android初学者综合应用——猜拳游戏

    我是一个技术爱好者,不光是移动开发,还是画简单的插画,都与专业人士不相上下.这次我专门利用业余时间,借鉴参考书上的代码,自己编写了电脑猜拳游戏,以飨Android移动开发初学者. 猜拳游戏的界面如下图 ...

  6. Android五子棋开发实验报告,Android五子棋游戏实验报告.doc

    Android开发总结报告 _____扫雷小游戏 2011年1月 一.背景 扫雷是一个简单的单人游戏.游戏的目的是在没有触碰任何一个地雷的情形下清空一个雷区.扫雷不仅有Windows版本,也有其他平台 ...

  7. android 手机九宫格解锁实验报告,Android数独游戏实验分析报告.pdf

    本科生实验报告 实验课程 Android 课程设计数独游戏 学院名称 信息科学与技术学院 专业名称 物联网工程 学生姓名 学生学号 指导教师 实验地点 实验成绩 二〇一五 年 十 月 二〇一五 年 十 ...

  8. android2048项目报告,Android项目开发实战-2048游戏

    <2048>是一款比较流行的数字游戏,最早于2014年3月20日发行.原版2048首先在GitHub上发布,原作者是Gabriele Cirulli,后被移植到各个平台.这款游戏是基于&l ...

  9. python猜拳小游戏实验报告_Java猜拳小游戏程序设计实验报告

    实验题目:猜拳小游戏 实验要求: 用 java 编写一个人机对战的猜拳小游戏.人选择性出拳,电脑随机出拳,判断 输赢,记录输赢情况.有简单的操作界面. 实验内容: 1 .问题分析过程: ( 1 )首先 ...

  10. android源码大全 IOS游戏源代码打包下载 小游戏|视频教程 微信小程序源码带后台全套|公众号平台

    不断更新中,下面是2017-12-22更新部分 IOS_源码及视频一小部分: 开发环境:Xcode 基于cocos2d的tweejump跳跃游戏ios经典游戏源码.rar  https://pan.b ...

最新文章

  1. Windows服务器上Mysql为设置允许远程连接提示:not allowed to connect to this MySQL server
  2. linux 日志行数,如何实时查看日志文件新增的行数
  3. 通过连接池无法连接mysql_连接池无法链接数据库
  4. 云安全并非神话 五个源头严控把关
  5. 【毕业设计】基于PHP的网上书店的设计(论文)
  6. TDL信道模型和CDL信道模型
  7. 软件测试的工作内容主要是干什么?
  8. 腾讯qq群推广“一键加群”的一个细节
  9. 正则表达式 -验证身份证号
  10. Python自动化办公 | 用Python自动生成数据日报
  11. Altium designer (AD)中如何设置区域规则和器件规则
  12. matlab中taylor公式源代码,matlab实现两步taylor-galerkin算法
  13. SOM-TL437x是基于TI Sitara系列AM4376/AM4379 ARM Cortex-A9高性能低功耗处理器设计的工业级核心板
  14. PON、EPON、GPON的区别
  15. Allegro贴片元件封装制作
  16. 思科设备VLAN配置命令
  17. autojs root权限命令
  18. 一文搞懂BN、LN、IN、GN的区别
  19. [Bartender]C#调用BartenderSDK使用场景-客户标签案列
  20. 博途中位值平均滤波算法(附SCL完整代码)

热门文章

  1. wuauclt.exe是什么进程?为什么运行?wuauclt.exe进程介绍
  2. 为什么你还是离不开微软的Office软件?
  3. 透过西安未来人工智能计算中心,看到AI不一样的未来
  4. 侯捷C++八部曲笔记(三、设计模式)
  5. 【思想感悟】站在巨人的肩膀上
  6. [ZZ] Maxwell 架构
  7. 槑图秀秀 (初学JAVA第三篇)
  8. 全国计算机大学排名2019最新排名,2019全国大学最新排名 中国最好大学排行榜
  9. 985硕士,入职八个月被通知裁员,领导哭着谈话,同事疯狂帮忙,但还是走了!...
  10. Apple应用证书申请过程