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 an