当前位置:网站首页>猜拳游戏专题训练
猜拳游戏专题训练
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对象的形式,而是写了一个空的无参构造方法。
修改结果
边栏推荐
- Shell command used in actual work - sed
- Graduation design of police report convenience service platform based on wechat applet
- Icml2022 | scalable depth Gaussian Markov random field
- Serveur mandataire SQUID
- 爱数课实验 | 第九期-利用机器学习方法进行健康智能诊断
- 教程|fNIRS数据处理工具包Homer2下载与安装
- Navicat Premium连接问题--- Host ‘xxxxxxxx‘ is not allowed to connect to this MySQL server
- OpenSSL 编程 一:基本概念
- 体验Navicat Premium 16,无限重置试用14天方法(附源码)
- squid代理服务器
猜你喜欢
Unity3d button adapts the size according to the text content
Codeforces Global Round 14
让马化腾失望了!Web3.0,毫无希望
麒麟V10安装字体
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
Full record of 2022 open source moment at Huawei partners and Developers Conference
Model reasoning acceleration based on tensorrt
富文本 考试 填空题
Graduation design of police report convenience service platform based on wechat applet
GoLand永久激活
随机推荐
Love math experiment | Issue 8 - building of Singapore house price prediction model
非常全面的DolphinScheduler(海豚调度)安装使用文档
shell脚本控制服务的启动和关闭 - 具备详细案例
SQL必需掌握的100个重要知识点:使用函数处理数据
# Leetcode 821. Minimum distance of characters (simple)
oss上传调用的是哪个方法
送你12个常用函数公式,用过的都说好
Original translation | comparison of machine learning model service tools: kserve, Seldon core and bentoml
Industry case | see the operation of bank digital transformation from the king of retail
MySQL速成——第一天--基础入门
White whoring red team goby & POC, how do you call white whoring?
强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”
100 important knowledge points that SQL must master: using functions to process data
ARCS模型介绍
Day8 ---- 云资讯项目介绍与创建
行业案例|从零售之王看银行数字化转型的运营之道
农产品期货怎么做怎么开户,期货开户手续费多少,找谁能优惠手续费?
互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?
Codeforces Round #716 (Div. 2)
Oracle的CTAS能不能将约束等属性带到新表?