第五章项目: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 ...
随机推荐
- Dotfuscator混淆加密
混淆加密 1. 需要安装Dotfuscator软件 2. 安装好后打开软件,找到编译好的DLL文件 3. 打开[setting]设置属性,如下图: 把 Disable String Encryptio ...
- 【C语言】C语言标识符
目录: [定义] [作用] [命名规则] [命名规范] 1.定义 标识符就是我们给函数或变量定义的名称.方便查阅增强可读性.减少沟通成本. 2.作用 · 增强可读性. · 减少沟通成本. ...
- HTTP协议基本知识
Xcode7.0以上版本必须操作:https http 在Info.plist中添加NSAppTransportSecurity类型Dictionary. 在NSAppTransportSecurit ...
- Android微信登陆
前言 分享到微信朋友圈的功能早已经有了,但微信登录推出并不久,文档写的也并不是很清楚,这里记录分享一下. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.co ...
- struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
- char(10)和VARCHAR(10)主要的区别是什么?
区别: 1.CHAR的长度是固定的,而VARCHAR2的长度是可以变化的, 如: 存储字符串“abc", 对于CHAR (10),表示你存储的字符将占10个字节(包括7个空字符), 而同样的 ...
- Tomcat:基于HTTP协议的Connector配置
Tomcat Connector 是请求接收环节与请求处理环节的连接器,具体点说,就是将接收到的请求传递给Tomcat WEB容器进行处理. Tomcat可以处理的不同协议的请求,例如HTTP协议.A ...
- MySQL运行状态show status中文详解(转)
要查看MySQL运行状态,要优化MySQL运行效率都少不了要运行show status查看各种状态,下面是参考官方文档及网上资料整理出来的中文详细解释: 状态名 作用域 详细解释 Aborted_cl ...
- Drupal7网站+IIS7.0+PHP+MySql
.服务器系统环境 Windows Server R2 Enterprise 64位操作系统 .所需软件 IIS7 PHPManager http://phpmanager.codeplex.com/r ...
- iOS TabbarController 设置底部Toolbar图片和文字颜色选中样式
提取公共方法: -(void)createChildVcWithVc:(UIViewController *)vc Title:(NSString *)title image:(NSString *) ...