ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

关于java的QuickHit打字游戏小项目

2019-10-20 11:02:05  阅读:222  来源: 互联网

标签:QuickHit java Level int void level 打字 System public


public class Player {
Scanner input = new Scanner(System.in);
//玩家属性
private int level = 0;//等级
private int curScore = 0;//积分
private long stratTime = 0;//开始时间
private int elapsdTime = 0;//所用时间

//封装
public int getLevel() {
return level;
}

public void setLevel(int level) {
this.level = level;
}

public int getCurScore() {
return curScore;
}

public void setCurScore(int curScore) {
this.curScore = curScore;
}

public long getStartTime() {
return stratTime;
}

public void setStartTime(int strTime) {
this.stratTime = strTime;
}

public int getElapsdTime() {
return elapsdTime;
}

public void setElapsdTime(int elapsdTime) {
this.elapsdTime = elapsdTime;
}

//封装
Game game = new Game(this);//创建游戏对象,并装入玩家
public void play ()throws Exception//玩游戏
{
for (int i = 0; i <LevelParam.levels.length; i++) {//游玩次数
//一次通关
Level level = LevelParam.levels[this.level];
this.stratTime = System.currentTimeMillis();
for (int j = 1; j <= level.getStrTime(); j++) {
System.out.println("第" + j + "次");
String out = game.pringStr();//接收字符串
String in = input.next();//接收玩家输入的字符串
game.printResult(out, in);//比较并打印结果
}
this.curScore += level.getPerSore();
System.out.println("通过第" + (this.level + 1) + "关,积分:" + curScore);
this.level++;//等级增加
//一次通关
}
}
}
public class Game {
private Player player;//玩家
public Game(){}//构造
public Game(Player player){this.player=player;}//构造
String pringStr()//输出字符串方法
{
Level level = LevelParam.levels[this.player.getLevel()];
Random random = new Random();
StringBuffer sb=new StringBuffer();
for (int i = 0; i < level.getStrLength(); i++) {
int rand = random.nextInt(6);
switch(rand)
{
case 0:
sb.append("a");
break;
case 1:
sb.append("b");
break;
case 2:
sb.append("c");
break;
case 3:
sb.append("d");
break;
case 4:
sb.append("e");
break;
case 5:
sb.append("f");
break;
}
}
System.out.println(sb.toString());
return sb.toString();
}

public void printResult(String out,String in)//比较结果
{
if(out.equals(in))
{
System.out.println("输入成功");
long endtime=System.currentTimeMillis();
double timecha=(endtime-this.player.getStartTime())*1.0/1000;
if(timecha>(LevelParam.levels[this.player.getLevel()].getTimeLimit()))
{
System.out.println("输入超时游戏结束");
System.exit(0);
}
}
else{
System.out.println("输入失败,退出游戏");
System.exit(0);
}
}
}
public class Level {
public int getLevelON() {
return levelON;
}

public void setLevelON(int levelON) {
this.levelON = levelON;
}

public int getStrLength() {
return strLength;
}

public void setStrLength(int strLength) {
this.strLength = strLength;
}

public int getStrTime() {
return strTime;
}

public void setStrTime(int strTime) {
this.strTime = strTime;
}

public int getTimeLimit() {
return timeLimit;
}

public void setTimeLimit(int timeLimit) {
this.timeLimit = timeLimit;
}

public int getPerSore() {
return perSore;
}

public void setPerSore(int perSore) {
this.perSore = perSore;
}

private int levelON=0;//等级
private int strLength=0;//字符串长度
private int strTime=0;//输入次数
private int timeLimit=0;//时间限制
private int perSore=0;//该等级得分
//构造
public Level(){}
public Level(int levelON, int strLength, int strTime, int timeLimit, int perSore) {
this.levelON = levelON;
this.strLength = strLength;
this.strTime = strTime;
this.timeLimit = timeLimit;
this.perSore = perSore;
}
//构造
}
public class LevelParam {
public static final Level levels[]=new Level[6];//常量数组属性
static{//设置并初始化等级难度
//等级,输入字数,输入次数,时间限制,得分
levels[0]=new Level(1,2,2,30,1);
levels[1]=new Level(1,3,2,30,2);
levels[2]=new Level(1,4,2,30,3);
levels[3]=new Level(1,5,2,30,4);
levels[4]=new Level(1,6,2,30,5);
levels[5]=new Level(1,7,2,30,6);
}
}
这个项目中,切实体会的是项目的面向对象设计,以及编程思想的重要。
其中使用构造以及static来初始化参数和传参,对于初学的我来说
的确是很巧妙的运用方法,
在我试图使用C++来写这个程序的时候,也切实体会到了编程语言上的实际差异
很多东西使用c++实现起来没有java方便,当然主要是我的理解还不够以及
对于C++的编程思路和技巧掌握不到位,以至于我现在还没有完成。我会尽量
完成它以提高自己的理解。

标签:QuickHit,java,Level,int,void,level,打字,System,public
来源: https://www.cnblogs.com/deemohans/p/11706938.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有