猜拳游戏Java

拿到这道问题,大值浏览下需要的类,既然是游戏,Game类中包含了init()与begin()两个方法,可以猜测,Game类作为了一个“控制器”的角色,在里面进行操作,创建Person与Computer类的对象,并调用各showQuan()方法,对结果进行比较,得出是否获胜的结论。

思路清晰后,开始编写相关的类,首先是前两个类Person与Computer类,两个类具有相同的属性和方法,所以分装了一个抽象类IPeople,提取出公共的属性和方法:

public abstract class IPeople {

	protected String name;
	protected int score;
	
	public abstract int showQuan();
	
	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;
	}
	
	
}

接下来是两个类继承了上述抽象类,并实现了抽象方法:

public class Person extends IPeople {

	@Override
	public int showQuan() {
		//接收用户的选择
		Scanner input =new Scanner(System.in);
		System.out.print("\n请出拳:1.剪刀 2.石头 3.布 (输入相应数字):");
		int show=input.nextInt();
		//输出出拳结果,并返回
		switch(show){
		case 1:
		System.out.println("你出拳:剪刀");
		break;
		case 2:
		System.out.println("你出拳:石头");
		break;
		case 3:
		System.out.println("你出拳:布");
		break;
	}
		return show;
	}
}

public class Computer extends IPeople {

	public Computer() {
		name = "电脑";
		score = 0;
	}

	@Override
	public int showQuan() {
		int random = (int) (Math.random() * 100 % 2);
		switch (random) {
		case 0:
			System.out.println(name + "你出拳:剪刀");
			break;
		case 1:
			System.out.println(name + "你出拳:石头");
			break;
		case 2:
			System.out.println(name + "你出拳:布");
			break;
		default:
			break;
		}
		return random + 1; //因为从0开始,所以加一
	}

}

接下来开始写Game类,将其方法都作为一个规范抽取出来,形成一个接口

public interface IGame {

	public abstract void init(); 
	public abstract void begin(); 
	public abstract void showMessage(); 
}

写一个类,实现以上接口:

public class Game implements IGame {

	Person person; // 甲方
	Computer computer; // 乙方
	int count;// 对战次数
	private int userInput;

	@Override
	public void init() {
		// TODO Auto-generated method stub
		person = new Person();
		computer = new Computer();
		count = 0;
	}

	@Override
	public void begin() {
		System.out.println("输入猜拳次数");
		Scanner input = new Scanner(System.in);
		userInput = input.nextInt();
		for (int i = 0; i < userInput; i++) {
			System.out.println("随机输入一个1-3的数");
			int n = input.nextInt();
			int result1 = computer.showQuan();// 计算机出结果
			if (n == result1 && n > 0 && n < 4) {
				System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!\n"); // 平局
			} else if ((n == 1 && result1 == 3) || (n == 2 && result1 == 1)
					|| (n == 3 && result1 == 2)) {
				System.out.println("结果:恭喜,你赢了!"); // 用户赢
				person.score++;
			} else {
				System.out.println("结果说:^_^,你输了,真笨!\n"); // 计算机赢
				computer.score++;
			}

		}
		System.out.println("您的得分:" + person.score);
		System.out.println("电脑得分:" + computer.score);
		if (person.score < computer.score) {
			System.out.println("你输了");
		} else if (person.score > computer.score) {
			System.out.println("你赢了");
		} else {
			System.out.println("平局");
		}

	}

	@Override
	public void showMessage() {
		// TODO Auto-generated method stub
		//偷个懒,就不显示了
	}

}

在init方法中初始化了两个对象,以及初始化了count值,begin()方法首先接收一个值,游戏次数,用着for循环次数的控制上,然后在每次游戏中,用户输入和电脑随机数进行比较,并记录相应的score结果,结束循环后,比较两者成绩,决出高低。

嗯,大概就是这个思路。O(∩_∩)O

源码下载地址:http://download.csdn.net/detail/changhuiyuanh/8421231

umm,加油哦哦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值