java学习之路——类与对象案例之打字游戏
程序员文章站
2022-09-14 09:41:00
类与对象案例-童年回忆之打字游戏一、玩家类二、等级类三、游戏类四、等级地图五、测试类这次要做的案例是一个打字游戏的案例,相信大家小时候都玩过金山打字通的警察抓小偷和飞机大战,这次的案例是类似的简易版。首先对于这个案例,我们要解决的是如何生成随机的字符串,如何判断生成的字符串和输入的字符串是否相等。一、玩家类package com.yc.oop6.hc0705;public class Player {private int score;//积分private long startT...
这次要做的案例是一个打字游戏的案例,相信大家小时候都玩过金山打字通的警察抓小偷和飞机大战,这次的案例是类似的简易版。
首先对于这个案例,我们要解决的是如何生成随机的字符串,如何判断生成的字符串和输入的字符串是否相等。
一、玩家类
package com.yc.oop6.hc0705;
public class Player {
private int score; //积分
private long startTime; //各级别的开始时间
private long dis; //每次闯关剩余时间
private int levelNo; //级别号码
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public int getLevelNo() {
return levelNo;
}
public void setLevelNo(int levelNo) {
this.levelNo = levelNo;
}
public Player(int score, long startTime, int levelNo) {
super();
this.score = score;
this.startTime = startTime;
this.levelNo = levelNo;
}
public Player() {
super();
}
public long getDis() {
return dis;
}
public void setDis(long dis) {
this.dis = dis;
}
}
二、等级类
package com.yc.oop6.hc0705;
public class Level {
private int levelNo; //第几关
private int strLength; //字符串长度
private int strTime; //需要输入的次数
private int timeLimit; //时间限制
private int score; //答对一次获得的积分
public int getLevelNo() {
return levelNo;
}
public int getStrLength() {
return strLength;
}
public int getStrTime() {
return strTime;
}
public int getTimeLimit() {
return timeLimit;
}
public int getScore() {
return score;
}
public Level(int levelNo, int strLength, int strTime, int timeLimit, int score) {
super();
this.levelNo = levelNo;
this.strLength = strLength;
this.strTime = strTime;
this.timeLimit = timeLimit;
this.score = score;
}
public Level() {
super();
}
}
三、游戏类
package com.yc.oop6.hc0705;
import java.util.Random;
import java.util.Scanner;
public class Game {
private Player player;
private Random r = new Random();
private Scanner sc = new Scanner(System.in) ;
public Game(Player player) {
this.player = player;
}
//开始游戏
public void startGame() {
System.out.println("游戏开始");
//开始闯关
for(int i = 0; i < Levels.ls.length; i++) {
System.out.println("开始进入第" + (i+1) + "关");
//每一关的开始在这里
player.setLevelNo(player.getLevelNo() + 1);
player.setStartTime(System.currentTimeMillis());
//循环,这一关要输入多少次
for(int j = 0 ; j < Levels.ls[player.getLevelNo() - 1].getStrTime() ; j++ ) {
String out = printStr();
System.out.println(out);
String in = sc.nextLine();
boolean flag = result(in, out);
if(flag == false) {
System.exit(1);;
}
}
//每剩下1s 增加20分
player.setScore(player.getScore() + (int)player.getDis() * 20);
System.out.println("完成第" + (i+1) + "关,您现在的积分为:" + player.getScore());
System.out.println("进入下一关");
}
}
//随机字符串
public String printStr( ) {
String str = "";
double rNum = 0;
for(int i = 0; i < Levels.ls[player.getLevelNo()-1].getStrLength(); i++ ) {
rNum = r.nextDouble();
if(rNum < 0.3333) {
str += (r.nextInt(9)+1);
}else if(rNum < 0.6667){
str += (char)(r.nextInt(26)+65);
}else {
str += (char)(r.nextInt(26)+97);
}
}
return str;
}
//字符串对比
public boolean result(String in, String out) {
//字符串判断
if(out.equals(in)) {
//先获取一下当前的系统时间
long endTimes = System.currentTimeMillis();
long startTimes = player.getStartTime();
//获得下一关的时间
long dis = Levels.ls[player.getLevelNo() -1 ].getTimeLimit();
if((endTimes - startTimes)/1000 > dis) {
System.out.println("游戏超时,游戏结束,您的最终得分为:" + player.getScore());
return false;
}else {
//输入正确,且没有超时
//加积分
int score = Levels.ls[player.getLevelNo()].getScore();
player.setDis(dis - (endTimes - startTimes)/1000);
player.setScore(player.getScore() + score );
System.out.println("输入正确,您现在的积分为:"+ player.getScore() +" ,这一关您还有: " + player.getDis() + " 秒钟");
return true;
}
}else {
System.out.println("输入错误,游戏结束,您的最终得分为:" + player.getScore());
return false;//输入错误
}
}
}
四、等级地图
package com.yc.oop6.hc0705;
public class Levels {
//定义一个静态的对象数组
public static Level ls[] = new Level[7];
static {
ls[0] = new Level(1,2,5,20,10);
ls[1] = new Level(2,3,5,18,20);
ls[2] = new Level(3,4,4,16,30);
ls[3] = new Level(4,5,4,15,40);
ls[4] = new Level(5,6,4,15,50);
ls[5] = new Level(6,7,3,15,60);
ls[6] = new Level(7,8,3,15,70);
}
}
五、测试类
package com.yc.oop6.hc0705;
public class Test {
public static void main(String[] args) {
Player p = new Player();
Game g = new Game(p);
g.startGame();
}
}
详细的解释都在代码的注释里了,大家细品~
本文地址:https://blog.csdn.net/weixin_44420328/article/details/107145062
下一篇: Python基础数据类型及常用方法