当前位置:网站首页>猜拳游戏专题训练
猜拳游戏专题训练
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对象的形式,而是写了一个空的无参构造方法。
修改结果
边栏推荐
- 行业案例|从零售之王看银行数字化转型的运营之道
- 体验Navicat Premium 16,无限重置试用14天方法(附源码)
- Navicat premium connection problem --- host 'XXXXXXXX' is not allowed to connect to this MySQL server
- 教程|fNIRS数据处理工具包Homer2下载与安装
- 实际工作中用到的shell命令 - sed
- Leetcode 1381. Design a stack that supports incremental operations
- Acwing周赛57-最长连续子序列-(二分or树状数组)
- Golang 使用正则来匹配出子字符串函数
- Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
- DO280OpenShift访问控制--security policy和章节实验
猜你喜欢

富文本 考试 填空题

让马化腾失望了!Web3.0,毫无希望

squid代理服務器

Educational Codeforces Round 108 (Rated for Div. 2)

华为伙伴暨开发者大会2022开源时刻全纪录

AI 绘画极简教程

行业案例|从零售之王看银行数字化转型的运营之道

麒麟V10安装字体

Show the comprehensive strength of strong products, and make the first show of 2022 Lincoln aviator in Southwest China

Codeforces Round #719 (Div. 3)
随机推荐
抗洪救灾,共克时艰,城联优品驰援英德捐赠爱心物资
Full record of 2022 open source moment at Huawei partners and Developers Conference
Goldfish rhca memoirs: do447 managing projects and carrying out operations -- creating job templates and starting jobs
白嫖红队goby&POC,叫你如何白嫖?
如何将队列里面的内容没秒钟执行一次优先级
使用storcli工具配置RAID,收藏这一篇就够了
VMware vSphere esxi 7.0 installation tutorial
分享一次自己定位 + 解决问题的经历
SQL必需掌握的100个重要知识点:检索数据
SQL必需掌握的100个重要知识点:用通配符进行过滤
How to do a good job of gateway high availability protection in the big promotion scenario
GFS分布式文件系统
“好声音“连唱10年,星空华文如何唱响港交所?
# Leetcode 821. Minimum distance of characters (simple)
Tutorial | fNIRS data processing toolkit homer2 download and installation
Acwing周赛57-最长连续子序列-(二分or树状数组)
农产品期货怎么做怎么开户,期货开户手续费多少,找谁能优惠手续费?
GoLand permanently activated
Data platform scheduling upgrade and transformation | operation practice from Azkaban smooth transition to Apache dolphin scheduler
Galaxy Kirin system LAN file sharing tutorial