当前位置:网站首页>猜拳游戏专题训练
猜拳游戏专题训练
2022-06-27 19:20:00 【continueLR】
目录
今天的任务是通过控制台方式实现一个人机对战的猜拳游戏,用户通过输入(1.剪刀 2.石头 3.布),机器随机生成(1.剪刀 2.石头 3.布),胜者积分,n 局以后通过积分的多少判定胜负。
1. 定义机器类,以及拳头属性(此属性只有三个值:剪刀,石头,布。这里的值可以使用数值代替)
2. 定义生成随机数的方法(让机器生成剪刀,石头,布的值),赋值给第一步的拳头属性
3. 定义测试类,获取用户输入的剪头石头布的值,和随机生成的值比较
4. 测试中,定义变量保存胜者积分
public static void main(String[] args) {
PerComGame game = new PerComGame();
game.menu();
}
}
class Person{
private int score;//人获得的积分
public Person(){
}
public Person(int score){
this.score = score;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int inputShow(){//人出拳的方法
Scanner input = new Scanner(System.in);
while (true){
System.out.println("请输入您猜拳的结果:(1.剪刀 2.石头 3.布)");
int num = input.nextInt();
switch (num){
case 1:
System.out.println("用户出拳:剪刀");
break;
case 2:
System.out.println("用户出拳:石头");
break;
case 3:
System.out.println("用户出拳:布");
break;
default:
System.out.println("您输入有误,游戏重新开始!");
continue;
}
return num;
}
}
}
class Computer{
private int score;//电脑得的积分
public Computer() {
}
public Computer(int score) {
this.score = score;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int inputShow(){
int num = (int)(Math.random()*3+1);
switch(num){
case 1:
System.out.println("机器出拳:剪刀");
break;
case 2:
System.out.println("机器出拳:石头");
break;
case 3:
System.out.println("机器出拳:布");
break;
}
return num;
}
}
class PerComGame{
private int js;
private Person person;
private Computer computer;
public PerComGame(){
this.js = 0;
this.person = new Person();
this.computer = new Computer();
}
public PerComGame(int js,Person person,Computer computer){
this.js = 0;
this.person = new Person();
this.computer = new Computer();
}
public int getJs() {
return js;
}
public void setJs(int play) {
this.js = js;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public Computer getComputer() {
return computer;
}
public void setComputer(Computer computer) {
this.computer = computer;
}
//输出菜单方法
public void menu(){
Scanner input = new Scanner(System.in);
System.out.println("------欢迎开始猜拳游戏------");
System.out.println("开始游戏,用户先开始");
loop:while(true){
startGame();
js++;
System.out.println("是否继续游戏(y/n)");
String c = input.next();
if(c.equals ("n") || c.equals("否")){//结束游戏
break loop;
}
}
System.out.println("本次游戏已结束!");
System.out.println("最终对战结果如下:");
System.out.println("用户\tVS\t机器");
System.out.println("总共游戏局数:" + js);
System.out.println("用户的积分:" + person.getScore());
System.out.println("机器的积分:" + computer.getScore());
System.out.println("由以上积分得最终赢家为 " + winner());
}
//游戏运行方法
public void startGame(){
int pNum = person.inputShow();
int cNum = computer.inputShow();
//比较结果
if((pNum == 1 && cNum == 3) || (pNum == 2 && cNum == 1) || (pNum == 3 && cNum == 2)){//用户赢
System.out.println("用户赢!");
person.setScore(person.getScore() + 1);
}else if(pNum == cNum){//平局
System.out.println("二者平局!");
}else{
System.out.println("机器赢!");
computer.setScore(computer.getScore() + 1);
}
}
//判断赢家方法
public String winner(){
if(person.getScore() > computer.getScore()){
return "用户";
}else if (person.getScore() == computer.getScore()){
return "用户和机器打平";
}else{
return "机器赢";
}
}踩坑原因:空指针异常

在创建person对象时,并没有用new对象的形式,而是写了一个空的无参构造方法。
修改结果
边栏推荐
- 于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
- 爱数课实验 | 第六期-金融反欺诈案例研究
- Codeforces Round #722 (Div. 2)
- SQL必需掌握的100个重要知识点:过滤数据
- College graduation thesis management system based on wechat applet graduation design
- 爱数课实验 | 第七期-基于随机森林的金融危机分析
- 数据平台调度升级改造 | 从Azkaban 平滑过度到Apache DolphinScheduler 的操作实践
- Industry case | see the operation of bank digital transformation from the king of retail
- Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler
- 分享下我是如何做笔记的
猜你喜欢

Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen

MySQL usage notes 1

“好声音“连唱10年,星空华文如何唱响港交所?

分享|智慧环保-生态文明信息化解决方案(附PDF)

Industry case | see the operation of bank digital transformation from the king of retail

SQL Server for循环用法

麒麟V10安装字体

于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日

GoLand永久激活

DO280OpenShift访问控制--security policy和章节实验
随机推荐
Kirin V10 installation font
Day8 ---- 云资讯项目介绍与创建
Open a new ecological posture | use the wrodpress remote attachment to store it in COS
Galaxy Kirin system LAN file sharing tutorial
Codeforces Round #722 (Div. 2)
Share how I take notes
GFS分布式文件系统
How to do a good job of gateway high availability protection in the big promotion scenario
TypeScript学习
VMware vSphere ESXi 7.0安装教程
Navicat premium connection problem --- host 'XXXXXXXX' is not allowed to connect to this MySQL server
一套系统,减轻人流集中地10倍的通行压力
麒麟V10安装字体
抗洪救灾,共克时艰,城联优品驰援英德捐赠爱心物资
Golang 使用正则来匹配出子字符串函数
OpenSSL 编程 二:搭建 CA
银河麒麟系统局域网文件共享教程
How to participate in openharmony code contribution
Prospects for enterprise digitalization (38/100)
分享|智慧环保-生态文明信息化解决方案(附PDF)