zoukankan      html  css  js  c++  java
  • 关于java的QuickHit打字游戏小项目

    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++的编程思路和技巧掌握不到位,以至于我现在还没有完成。我会尽量
    完成它以提高自己的理解。
  • 相关阅读:
    9. Palindrome Number
    7. Reverse Integer
    6. ZigZag Conversion
    1. Two Sum
    [leetcode]Binary Tree Zigzag Level Order Traversal
    [leetcode]Scramble String
    [leetcode]Convert Sorted Array to Binary Search Tree
    [leetcode]Sum Root to Leaf Numbers
    [leetcode]Longest Consecutive Sequence
    [leetcode]Reverse Linked List II
  • 原文地址:https://www.cnblogs.com/deemohans/p/11706938.html
Copyright © 2011-2022 走看看