java面向对象实现两人猜拳游戏

Player类

public class Player {

/**
 * @author DCH
 */
private String name;
private int score;

/*
 * 无参构造器
 * 
 */
public Player(){
	
}

/*
 * 
 * 有参构造器
 * 
 */
public Player(String name,int score){
	this.name=name;
	this.score=score;
}

//get/set方法
public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public int getScore() {
	return score;
}

public void setScore(int score) {
	this.score = score;
}

//可以出拳   出拳 石头(1) ,剪刀(2),布(3)
	/**
	 * 出拳方法 
	 * @return
	 */
	public int getNum(){
		// 生成一个随机数  ,  1  2  3  
		int r = (int)(Math.random()*100);
		if(r<33){
			System.out.println(name+"出拳:石头");
			return 1;
		}else if(r<66){
			System.out.println(name+"出拳:剪刀");
			return 2;
		}else{
			System.out.println(name+"出拳:布");
			return 3;
		}
	 
	}

public void scoreInfo(){
	System.out.println("分数为"+getScore());
}

}

caipan类

public class CaiPan {

/**
 * @author DCH
 */
//第一个选手
private Player player1;
//第二个选手
private Player player2;

//无参构造器
public CaiPan(){
	
}


//有参构造器
public CaiPan(Player player1,Player player2){
	this.player1=player1;
	this.player2=player2;
}


//get/set方法
public Player getPlayer1() {
	return player1;
}

public void setPlayer1(Player player1) {
	this.player1 = player1;
}

public Player getPlayer2() {
	return player2;
}

public void setPlayer2(Player player2) {
	this.player2 = player2;
}

//可以出拳   出拳 石头(1) ,剪刀(2),布(3)
public void change(){
	int	a=player1.getNum();
	int b=player2.getNum();
	if(a==1){
		if(b==2){
			int s =  player1.getScore()+1;
			player1.setScore(s);
			System.out.println("赢的人为 "+player1.getName());
		}else if(b==3){
			int s =  player2.getScore()+1;
			player2.setScore(s);
			System.out.println("赢的人为 "+player2.getName());
		}else{
			System.out.println("平局");
		}
	}
	if(a==2){
		if(b==1){
			int s =  player1.getScore()+1;
			player1.setScore(s);
			System.out.println("赢的人为 "+player2.getName());
		}else if(b==3){
			int s =  player2.getScore()+1;
			player2.setScore(s);
			System.out.println("赢的人为 "+player1.getName());
		}else{
			System.out.println("平局");
		}
	}
	if(a==3){
		if(b==1){
			int s =  player1.getScore()+1;
			player1.setScore(s);
			System.out.println("赢的人为 "+player1.getName());
		}else if(b==2){
			int s =  player2.getScore()+1;
			player2.setScore(s);
			System.out.println("赢的人为  "+player2.getName());
		}else{
			System.out.println("平局");
		}
	}
}

}

CQtest类(测试类)

public class CQtest {

/**
 * @author DCH
 */
public static void main(String[] args) {
	// TODO Auto-generated method stub
	
	Player player1 = new Player();
	player1.setName("杜");
	Player player2 = new Player();
	player2.setName("李");
	CaiPan caipan1 = new CaiPan();
	//这里出现空指针异常的问题是
	//初始化了但是没有给对象player1和player2,
	//导致不能调用player类里面相关的方法与属性.
	caipan1.setPlayer1(player1);
	caipan1.setPlayer2(player2);
	for (int i = 0; i < 10; i++){
		caipan1.change();
	}
	player1.scoreInfo();
	player2.scoreInfo();
}

}

注意在初始化对象时,可以通过构造器,也可以通过set/get方法给对象赋值,
例如构造器:CaiPan caipan1 = new CaiPan(Player player1,Player player2);
例如set/get方法: CaiPan caipan1 = new CaiPan();//这个是调用无参构造器
caipan1.setPlayer1(player1);
caipan1.setPlayer2(player2);
如果没有set设置对象的话,就不能在Caipan类调用Player类的对象方法和属性.

展开阅读全文

没有更多推荐了,返回首页