A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© boycechan 中级黑马   /  2014-11-29 11:57  /  1657 人查看  /  9 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 boycechan 于 2014-11-29 14:14 编辑

package project.quickhit.review2;
/**
* 玩家级别类
* @author 陈向波
* @version 1.0 2014-11-18
*/
public class Level {
private int levelNo;   //各级别
private int strLength; //各级别输入字符串长度
private int strTimes;  //各级别输入字符串次数
private int timeLimit; //各级别输入字符串的时间限制
private int 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;
}
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;
}


}
---------------------------------------------------------------------------
package project.quickhit.review2;

import java.util.Scanner;

/**
* 玩家类
* @author Chen.Xiangbo
* @version 1.0 2014-11-18
*/
public class Player {
        private int levelNo;   //当前玩家级别
        private int curScore;  //当前级别玩家得分
        private long startTime; //当前级别玩家游戏开始时间
        private int elapsedTime;//当前玩家已用时间
        
        public Player(){
               
        }
        public Player(int levelNo,int curScore,long startTime,int elapsedTime){
                this.levelNo=levelNo;
                this.curScore=curScore;
                this.startTime=startTime;
                this.elapsedTime=elapsedTime;
        }
        /**
         * 定义play()方法
         */
        public String play(){
                Scanner input = new Scanner(System.in);  //定义输入器
                String in=input.next();
                return in;               
        }
        
        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;
        }

        
        
}
----------------------------------------------------------------------
package project.quickhit.review2;
/**
* 级别参数类
* @author Chen.Xiangbo
* @version 1.0 2014-11-18
*/
public class LevelParam {
        public final static Level levels[]=new Level[6];  //对应6个级别:对应数组
        
        /**
         * 静态代码块
         */
        static{
                levels[0]= new Level(1,2,6,45,5);
                levels[1]= new Level(2,3,5,40,10);
                levels[2]= new Level(3,4,4,35,15);
                levels[3]= new Level(4,5,3,30,25);
                levels[4]= new Level(5,6,2,25,40);
                levels[5]= new Level(6,7,1,20,100);
        }
}
-------------------------------------------------------------------
package project.quickhit.review2;
/**
* 游戏类
* @author Chen.Xiangbo
* @version 1.0 2014-11-18
*/
public class Game {
        private Player player;
        
        public Game(){
                 player = new Player(1,0,0,0);
        }
        
        public void start(){
                for(int i=1;i<=LevelParam.levels.length;i++){
                        player.setStartTime(System.currentTimeMillis());   //定义开始时间
                        for(int j=0;j<LevelParam.levels[player.getLevelNo()-1].getStrTimes();j++){
                                String out=printStr();//随机产生字符串
                                String in=player.play();//输入字符串
                                printResult(out,in);  //比较字符串
                        }
                        if(i<player.getLevelNo()){
                                System.out.println("恭喜您普级为"+(i+1)+"级!");
                        }
                        player.setLevelNo(i);  //更新等级
                }
                System.out.println("真牛逼,已全部通关!");
        }
        public String printStr(){
                String out="";   //定义输出字符串的变量out
                int length =LevelParam.levels[player.getLevelNo()-1].getStrLength();
                for(int i=0;i<length;i++){
                out+=(char)((Math.random()*(126-33)+33));  //随机生成字符串
               
                }
                System.out.println(out);   //输出字符串
                return out;
        }
        public void printResult(String out,String in){
                //计算已用时间
                player.setElapsedTime((int)((System.currentTimeMillis()-player.getStartTime())/1000));
                //计算积分
                player.setCurScore(player.getCurScore()+LevelParam.levels[player.getLevelNo()-1].getPerScore());
                //判断输入是否正确
                if(!out.equals(in)){
                        System.out.println("输入错误,游戏退出!");
                        System.exit(0);
                }if(player.getElapsedTime()>LevelParam.levels[player.getLevelNo()-1].getTimeLimit()){
                        System.out.println("输入超时,游戏退出!");
                        System.exit(0);
                }else{
                        System.out.println("您的积分是"+player.getCurScore()+",等级为"+player.getLevelNo()+",已用时间"+player.getElapsedTime()+"秒");
                }
        }
}
-----------------------------------------------------------------------------
package project.quickhit.review2;
/**
* 测试测试类
* @author Chen.Xiangbo
*
*/
public class TestQuickhit {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Game gm = new Game();
                gm.start();
        }

}
-----------------------------------------------------------------------
总共五个类都是以虚线隔开

评分

参与人数 1技术分 +1 收起 理由
杨佳名 + 1 赞一个!

查看全部评分

9 个回复

倒序浏览
抢沙发,抢沙发,坐看
回复 使用道具 举报
貌似  很腻害的样子 给力
回复 使用道具 举报
不习惯看代码,有图就更好了
回复 使用道具 举报
收藏了
回复 使用道具 举报
全部自己写得吗,思路真明确
回复 使用道具 举报
貌似好厉害。。。
回复 使用道具 举报
好厉害的
回复 使用道具 举报
厉害。。。。。。。。。。。
回复 使用道具 举报
厉害,赞一个!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马