猜拳小游戏
2013-04-06 21:21
302 查看
一.任务描述
模拟剪刀,石头,布游戏,实现人机互动二.编写过程
1.阶段一:进行页面布局
布局代码如下:仍然使用LinearLayout的嵌套布局
<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:background="@drawable/pg" android:orientation="vertical" 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" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/chosetext" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioGroup android:id="@+id/Radiogroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/Button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/shitou" /> <RadioButton android:id="@+id/Button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/jiandao" /> <RadioButton android:id="@+id/Button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bu" /> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/click" /> </LinearLayout> </LinearLayout>
布局效果如下:
2.阶段二:编写电脑类和玩家类
实现代码如下:public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub switch(checkedId){ case R.id.Button1: Pchose=1; personchoose=button1.getText().toString(); break; case R.id.Button2: Pchose=2; personchoose=button2.getText().toString(); break; case R.id.Button3: Pchose=3; personchoose=button3.getText().toString(); break; default: break; }
Cchose=(int)Math.random()*10%3+1;//电脑随机产生数字1-3 switch(Cchose){ case 1: computerchoose=button1.getText().toString(); break; case 2: computerchoose=button2.getText().toString(); break; case 3: computerchoose=button3.getText().toString(); break; } } });
3.阶段三:编写猜拳游戏类,判定输赢
实现代码如下:if(computerchoose.equals(personchoose)) { score="平局"; } else if((personchoose.equals("剪刀") && computerchoose.equals("石头"))||(personchoose.equals("石头") && computerchoose.equals("布"))||(personchoose.equals("布") &&computerchoose.equals("剪刀"))) { score="玩家赢"; } else { score="电脑赢"; } Toast.makeText(this, score, Toast.LENGTH_LONG).show(); showResult.setText("玩家:"+personchoose+"VS电脑:"+computerchoose+"\n"+score);
4.阶段四:页面跳转传值
public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("computerchoose", computerchoose); bundle.putString("personchoose", personchoose); intent.putExtras(bundle); intent.setClass(MainActivity.this, SecondActivity.class); startActivity(intent); }
接收数据:
intent=getIntent(); Bundle bundle=intent.getExtras(); personchoose=bundle.getString("personchoose"); computerchoose=bundle.getString("computerchoose");