现在的位置: 首页 > 编程语言 > 正文

Java入门案列之猜拳小游戏

2020年02月13日 编程语言 ⁄ 共 2905字 ⁄ 字号 评论关闭

最近正在学习Java基础知识,终于完成了第一个小demo,记录下来,算是一个小总结与整理,也希望可以帮助到别人。

先看看我写了哪些类:

Player:玩家类;

ComputerPlayer:机器人玩家类,主要用来实现机器人随机出拳;

Game:游戏类,主要实现游戏规则的逻辑,以及正式游戏的逻辑;

TestGuessBox:代码测试类;

Player类:

//玩家类public class Player { private String name; //玩家昵称 private int score; //玩家积分 private String box; //玩家出的 //玩家构造函数,传入玩家昵称与玩家初始积分 Player(String name,int score){ this.name=name; this.score=score; } 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 String getBox() { return box; } public void setBox(String box) { this.box = box; } }

ComputerPlayer类

public class ComputerPlayer extends Player { //机器人玩家构造函数,传入机器人昵称与初始积分 ComputerPlayer(String name, int score) { super("机器人"+name, score); // TODO Auto-generated constructor stub } /** * 实现机器人玩家随机出拳的逻辑 */ void punch() { String[] box = {"剪刀","石头","布"}; int index =(int) (Math.random()*3); this.setBox(box[index]); }}

Game类

import java.util.Scanner; public class Game { Player p; //玩家 ComputerPlayer cp; //机器人玩家 //构造函数,得到一个玩家和一个机器人玩家 Game(Player p, ComputerPlayer cp) { this.p = p; this.cp = cp; } //开始游戏 void start() { System.out.println("玩家"+p.getName()+ "和"+cp.getName()+"开始游戏咯"); System.out.println("您的初始积分为:"+p.getScore()+ "\n"+cp.getName()+"的积分是:"+cp.getScore()); Scanner sc=new Scanner(System.in); while(true) { System.out.println("请出拳(剪刀石头布,exit退出游戏):"); String pbox=sc.next(); if(filter(pbox)) { //过滤器 if(pbox.equals("exit")) { //退出游戏 break; }else { p.setBox(pbox); cp.punch(); System.out.println("您出了:"+p.getBox()); System.out.println(cp.getName()+"出了:"+cp.getBox()); int result = ruler(p,cp); if(result>0) { System.out.println("您赢了,赢得10积分"); p.setScore(p.getScore()+10); cp.setScore(cp.getScore()-10); } else if(result<0) { System.out.println("您输了,扣除10积分"); p.setScore(p.getScore()-10); cp.setScore(cp.getScore()+10); } else { System.out.println("您和机器人打平了!"); } } } else { System.out.println("输入了无法识别的关键字,请重新输入:"); continue; //退出本次循环,进入下一次循环 } } System.out.println("本轮结束,积分情况如下:"); System.out.println("您的当前积分:"+p.getScore()); System.out.println(cp.getName()+"的当前积分:" +cp.getScore()); } /** * 游戏规则 * * @param p1 玩家1 * @param cp2 机器玩家2 * @return 0为打平,1为玩家赢,-1为机器玩家赢 */ int ruler(Player p1, Player cp2) { if (p1.getBox().equals("剪刀")) { if (cp2.getBox().equals("石头")) return -1; else if (cp2.getBox().equals("布")) return 1; } else if (p1.getBox().equals("石头")) { if (cp2.getBox().equals("剪刀")) return 1; else if (cp2.getBox().equals("布")) return -1; } else if (p1.getBox().equals("布")) { if (cp2.getBox().equals("剪刀")) return -1; else if (cp2.getBox().equals("石头")) return 1; } return 0; } /** * 过滤器 * @param s 需要过滤的文字 * @return */ boolean filter(String s) { if (s.equals("剪刀") || s.equals("石头") || s.equals("布") || s.equals("exit")) { return true; } else return false; }}

TestGuessBox类

public class TestGuessBox { public static void main(String[] args) { // TODO Auto-generated method stub Player p =new Player("小七月",100); ComputerPlayer cp=new ComputerPlayer("小丑八怪",100); Game game=new Game(p,cp); game.start(); } }

很简单,理清了思路就很容易写了。如果有错误请指出。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: Java入门案列之猜拳小游戏

以上就上有关Java入门案列之猜拳小游戏的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.