第五章项目:QuickHit
需求概述:
根据输入速率和正确率将玩家分为不同级别,级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高。如果玩家在规定时间内完成规定次数的输入,正确率达到规定要求,则玩家升级(为了简单起见,规定用户只要错误一次,则游戏结束)。最高为6级,刚开始一律1级。
案例覆盖的技能点:
面向对象设计的思想
使用类图理解类的关系
类的封装
构造方法的使用
this和static关键字的使用
需要用到的类:
玩家(Player)类:当前级别号(levelNo),当前级别积分(currScore),当前级别开始时间(startTime),当前级别已用时间(elapsedTime)
级别(Level)类:级别编号(LevelNo),各级别一次输出的字符串长度(strLength),输出次数(strTime),闯关限制时间(timeLimit),正确输入得分(perScore)
LevelParam类:定义一个长度为6的Level数组,用来存放各级别的具体参数信息
游戏(Game)类:Player对象的属性,两个方法,printStr(用来随机输出字符串),printResult(用来比较输出参数和输入参数和计算)
Player类
package quickhit;
import java.util.Scanner;
public class Player {
//属性
public int levelNo;
public int curScore;
public long startTime;
public int elapsedTime;
//有参构造
public Player(int levelNo, int curScore, long startTime, int elapsedTime) {
this.levelNo = levelNo;
this.curScore = curScore;
this.startTime = startTime;
this.elapsedTime = elapsedTime;
}
//无参构造
public Player() {
}
public int getLevelNo() {
return levelNo;
}
public void setLevelNo(int levelNo) {
this.levelNo = levelNo;
}
public int getCurScore() {
return curScore;
}
public void setCurScore(int curScore) {
this.curScore = curScore;
}
public long getStartTime() {
return startTime;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public int getElapsedTime() {
return elapsedTime;
}
public void setElapsedTime(int elapsedTime) {
this.elapsedTime = elapsedTime;
}
//玩游戏的方法
public void play(){
//调用game的带参.this代表Player属性
Game game=new Game(this);
Scanner input=new Scanner(System.in);
for (int i = 0; i <LevelParam.level.length; i++) {
//晋级
this.levelNo+=1;
//晋级后计时清零
this.startTime=System.currentTimeMillis();
this.curScore=0;
//满级通关
if(levelNo==6){
System.out.println("通关..........");
break;
}
//内层循环,输出输入比较
for (int j = 0; j < LevelParam.level[levelNo-1].getStrTimes(); j++) {
//输出字符串
String outStr=game.printStr();
//接收用户输入字符串
String inStr=input.next();
//调用game的printResult方法,对比
game.printResult(outStr, inStr);
}
}
}
}
Level类
package quickhit;
public class Level {
public int levelNo;
public int strLength;
public int strTimes;
public int timeLimit;
public int perScore;
public int getLevelNo() {
return levelNo;
}
public void setLevelNo(int levelNo) {
this.levelNo = levelNo;
}
public int getStrLength() {
return strLength;
}
public void setStrLength(int strLength) {
this.strLength = strLength;
}
public int getStrTimes() {
return strTimes;
}
public void setStrTimes(int strTimes) {
this.strTimes = strTimes;
}
public int getTimeLimit() {
return timeLimit;
}
public void setTimeLimit(int timeLimit) {
this.timeLimit = timeLimit;
}
public int getPerScore() {
return perScore;
}
public void setPerScore(int perScore) {
this.perScore = perScore;
}
//无参
public Level(){
}
//带参
public Level(int levelNo, int strLength, int strTimes, int timeLimit,
int perScore) {
this.levelNo = levelNo;
this.strLength = strLength;
this.strTimes = strTimes;
this.timeLimit = timeLimit;
this.perScore = perScore;
}
}
LevelParam类
package quickhit;
public class LevelParam {
public final static Level level[]=new Level[6];
static{
level[0]=new Level(1,2,10,30,1);
level[1]=new Level(2,3,9,26,2);
level[2]=new Level(3,4,8,22,5);
level[3]=new Level(4,5,7,18,8);
level[4]=new Level(5,6,6,15,10);
level[5]=new Level(6,7,2,12,15);
}
}
Game类
package quickhit;
import java.util.Random;
public class Game {
public Player player;
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
public Game(Player player) {
this.player = player;
}
public Game(){
}
public String printStr(){
//定义一个int类型的对应各级编号应输出字符串的长度
int strLength=LevelParam.level[player.getLevelNo()-1].getStrLength();
StringBuffer buffer=new StringBuffer();
//生成随机
Random random=new Random();
for (int i = 0; i <strLength ; i++) {
int rand=random.nextInt(strLength);
switch(rand){
case 0:
buffer.append(">");
break;
case 1:
buffer.append("<");
break;
case 2:
buffer.append("*");
break;
case 3:
buffer.append("&");
break;
case 4:
buffer.append("%");
break;
case 5:
buffer.append("#");
break;
}
}
//输出
String str=buffer.toString();
System.out.println(str);
return str;
}
public void printResult(String out,String in){
long currentTime=System.currentTimeMillis();
//判断是否一致
if(out.equals(in)){
//判断是否超时
if((currentTime-player.getStartTime())/1000>LevelParam.level[player.getLevelNo()-1].getTimeLimit()){
System.out.println("太慢了吧!~");
System.exit(1);
}else{
//计算当前积分
player.setCurScore(player.getCurScore()+LevelParam.level[player.getLevelNo()-1].getPerScore());
//计算时间
player.setElapsedTime((int)(currentTime-player.getStartTime())/1000);
//输出级别,积分和时间
System.out.println("输入正确,您的级别"+player.getLevelNo()+"积分"+player.curScore+"已用时间"+player.getElapsedTime());
}
}else{
System.out.println("输入错误........");
System.exit(1);
}
}
}
测试类
package quickhit;
public class Test {
public static void main(String[] args) {
Player player=new Player();
player.play();
}
}
游戏效果



第五章项目:QuickHit的更多相关文章
- PMP 第五章 项目范围管理
1.范围管理主要是干什么?什么是产品范围?什么是项目范围? 项目范围管理包括确保项目做而且只做成功完成项目所需的全部工作的各过程.管理项目范围主要是在定义和控制哪些工作应该包括在项目内,哪些不应 ...
- 05章项目: QuickHit快速击键
一.项目分析 根据输入速率和正确率将玩家分为不同等级,级别越高,一次显示的字符数越多,玩家正确输入一次的得分也越高.如果玩家在规定时间内完成规定次数的输入,正确率达到规定要求,则玩家升级.玩家最高级别 ...
- 精通Web Analytics 2.0 (7) 第五章:荣耀之钥:度量成功
精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第五章:荣耀之钥:度量成功 我们的分析师常常得不到我们应得的喜欢,尊重和资金,因为我们没有充分地衡量一个黄金概念:成果.因为我们 ...
- 读《编写可维护的JavaScript》第五章总结
第五章 UI层的松耦合 5.1 什么是松耦合 在Web开发中,用户界面是由三个彼此隔离又相互作用的层定义的: HTML是用来定义页面的数据和语义 CSS用来给页面添加样式 JavaScript用来给页 ...
- python学习心得第五章
python学习心得第五章 1.冒泡排序: 冒泡是一种基础的算法,通过这算法可以将一堆值进行有效的排列,可以是从大到小,可以从小到大,条件是任意给出的. 冒泡的原理: 将需要比较的数(n个)有序的两个 ...
- 《Introduction to Tornado》中文翻译计划——第五章:异步Web服务
http://www.pythoner.com/294.html 本文为<Introduction to Tornado>中文翻译,将在https://github.com/alioth3 ...
- 《Android群英传》读书笔记 (2) 第三章 控件架构与自定义控件详解 + 第四章 ListView使用技巧 + 第五章 Scroll分析
第三章 Android控件架构与自定义控件详解 1.Android控件架构下图是UI界面架构图,每个Activity都有一个Window对象,通常是由PhoneWindow类来实现的.PhoneWin ...
- (转)iOS Wow体验 - 第五章 - 利用iOS技术特性打造最佳体验
本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第五章译文精选,其余章节将陆续放出.上一篇:Wow ...
- 第五章SignalR的实时高频通讯
第五章SignalR的实时高频通讯 概述:本例子演示了如果创建一个对象与其他浏览器共享实时状态的应用程序.我们要创建的应用程序为“MoveShape”,该MoveShape页面会显示一个Html Di ...
随机推荐
- ASP.NET多文件上传实例
在Web应用程序开发中,避免不了要用到上传文件这个功能,但以前上传文件是个很麻烦的事,现在有了.NET,文件上传变得轻而易举.下面的这个例子实现了多文件上传功能.可以动态添加输入表单,上传的文件数量没 ...
- 2034-人见人爱A-B(c++实现)
Problem Description 参加过上个月月赛的同学一定还记得其中的一个最简单的题目,就是{A}+{B},那个题目求的是两个集合的并集,今天我们这个A-B求的是两个集合的差,就是做集合的减法 ...
- Learn RxJava
Learn RxJava http://reactivex.io/documentation/operators.html https://github.com/ReactiveX/RxJava/wi ...
- umeng 渠道统计 android
1.配置AndroidManifest.xml,添加权限 <uses-permission android:name="android.permission.ACCESS_NETWOR ...
- App 即时通讯 SDK
1.网易云信 http://netease.im/ 2.环信 http://www.easemob.com/customer/im 3.融云 http://www.rongcloud.cn/ 4.极光 ...
- HTTPS学习总结
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 21.0px Verdana; color: #393939 } span.s1 { } HTTPS学习总结 ...
- (20160601)开源第三方学习之SVProgressHUD
SVProgressHUD相信在很多项目中都有运用,运用于弹出窗提示效果: 地址:https://github.com/SVProgressHUD/SVProgressHUD 一:插件的运用 1.1 ...
- Http协议与TCP协议简单理解
TCP协议对应于传输层,而HTTP协议对应于应用层,从本质上来说,二者没有可比性.Http协议是建立在TCP协议基础之上的,当浏览器需要从服务器获取网页数据的时候,会发出一次Http请求.Http会通 ...
- 【开源项目SugarSite】ASP.NET MVC+ Layui+ SqlSugar+RestSharp项目讲解
SugarSite一个前端支持移动端的企业网站,目前只支持了简单功能,后续还会加上论坛等. 源码GIT地址: https://github.com/sunkaixuan/SugarSite 技术介绍 ...
- js:插入节点appendChild insertBefore使用方法
首先 从定义来理解 这两个方法: appendChild() 方法:可向节点的子节点列表的末尾添加新的子节点.语法:appendChild(newchild) insertBefore() 方法:可在 ...