类与对象案例-童年回忆之打字游戏
- 一、玩家类
- 二、等级类
- 三、游戏类
- 四、等级地图
- 五、测试类
这次要做的案例是一个打字游戏的案例,相信大家小时候都玩过金山打字通的警察抓小偷和飞机大战,这次的案例是类似的简易版。
首先对于这个案例,我们要解决的是如何生成随机的字符串,如何判断生成的字符串和输入的字符串是否相等。
一、玩家类
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);;
}
}
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();
}
}
详细的解释都在代码的注释里了,大家细品~