05_打字游戏

自己版本:

package com.entity;

import java.util.Arrays;
import java.util.Random;

public class Computer {
    
    public String print(char[] base,int rank){
        char[] c;
        switch(rank){
        case 1:
            c=Arrays.copyOf(base, 10);
            return getPrint(c,2);
        case 2:
            c=Arrays.copyOf(base, 10);
            return getPrint(c,3);
        case 3:
            c=Arrays.copyOf(base, 36);
            return getPrint(c,4);
        case 4:
            c=Arrays.copyOf(base, 36);
            return getPrint(c,5);
        case 5:
            return getPrint(base,6);
        case 6:
            return getPrint(base,8);
        }
        return null;
        
    }
    
    public String getPrint(char[] score,int number){
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<number;i++){
            int x=new Random().nextInt(score.length);
            sb.append(score[x]);
        }
        System.out.println(sb);
        return sb.toString();
    }
    

}

package com.entity;

import java.util.Scanner;

public class Game {
    private static int rank=1;
    private User user;
    private Computer computer;
    public final long canUseTime[]={4000,4000,5000,5000,6000,70000};
    public final int needScore[]={80,100,100,100,200,100};
    public final char[] base={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','>','<','_','^','&','*','$','(',')','@','#','%'};
    
    public Game(){
        user=new User();
        computer=new Computer();
    }
    public void setComputer(Computer computer) {
        this.computer = computer;
    }
    public Computer getComputer() {
        return computer;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public User getUser() {
        return user;
    }
    public static void setRank(int rank) {
        Game.rank = rank;
    }

    public static int getRank() {
        return rank;
    }
    
    public void startGame(){
        Scanner sc=new Scanner(System.in);
        System.out.println("-------------------------------");
        System.out.println("欢迎来到最新科技打字游戏!");
        System.out.println("输入s开始游戏!");
        boolean flag=sc.nextLine().equals("s") ? true :false;
        if(flag){
            while(rank<=6){            
                doGame();
                if(rank>6){
                    System.out.println("恭喜您通关了!\n游戏结束!");
                    System.exit(0);
                }
            }
        }else{
            System.out.println("谢谢使用,再见!");
            System.exit(0);
        }
    }
    
    public void doGame(){
        String answer=computer.print(base, rank);
        String result=user.print();
        if(!answer.equals(result)){
            System.out.println("输入错误,游戏结束!");
            System.exit(1);
        }else if(user.getSpentTime()>canUseTime[rank-1]){
            System.out.println("输入超时,游戏结束!");
            System.exit(1);
        }else{
            user.setScore(user.getScore()+10);//输入正确一次加10积分
            if(user.getScore()>=needScore[rank-1]){
                rank=rank+1;
                user.clearScore();
                System.out.println("输入正确,您升级了,当前级别为:"+rank+",当前积分为:"+user.getScore());
            }else{
                System.out.println("输入正确,您的积分为:"+user.getScore()+"您的级别为:"+rank);
            }
        }
    }
    
    

}

package com.entity;

import java.util.Scanner;

public class User {
    private int score;
    private String name;
    private long spentTime;

    public void setScore(int score) {
        this.score = score;
    }

    public int getScore() {
        return score;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
    
    public User(){
        
    }
    public User(String name){
        this.name=name;
    }
    public void setSpentTime(long spentTime) {
        this.spentTime = spentTime;
    }

    public long getSpentTime() {
        return spentTime;
    }
    
    public String print(){//人的输入
        spentTime=0;
        Scanner sc=new Scanner(System.in);
        long startTime=System.currentTimeMillis();
        String userPrint= sc.nextLine();
        long endTime=System.currentTimeMillis();
        spentTime=endTime-startTime;
        return userPrint;
    }
    public void clearScore(){//积分清0
        this.score=0;
    }

    

}

package com.test;

import com.entity.Game;

public class TestGame {
    public static void main(String[] args) {
        Game game=new Game();
        game.startGame();
    }

}

 

ppt版本:

package com.entity;

import java.util.Scanner;

public class Player {
    private int levelNo=1;//玩家级别号
    private int currScore;//当前级别积分
    private long startTime;//当前级别开始时间
    private int elapsedTime;//当前级别已用时间

    public String play(){//玩家输入
        Scanner sc=new Scanner(System.in);
        long startTime=System.currentTimeMillis();
        String s= sc.nextLine();
        long endTime=System.currentTimeMillis();
        this.elapsedTime+=(int) ((endTime-startTime)/1000);
        return s;
    }
    public void clear(){
        this.currScore=0;
        this.elapsedTime=0;
    }

    public int getLevelNo() {
        return levelNo;
    }

    public void setLevelNo(int levelNo) {
        this.levelNo = levelNo;
    }

    public int getCurrScore() {
        return currScore;
    }

    public void setCurrScore(int currScore) {
        this.currScore = currScore;
    }

    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 com.entity;

public class LevelParam {
    
    public static Level[] levels=new Level[6]; 
    static {
        levels[0]=new Level(1,2,10,30,1);
        levels[1]=new Level(2,3,9,26,2);
        levels[2]=new Level(3,4,8,22,5);
        levels[3]=new Level(4,5,7,18,8);
        levels[4]=new Level(5,6,6,15,10);
        levels[5]=new Level(6,7,5,12,15);
    }
    
}
package com.entity;

public class Level {
    private int levelNo;//级别号
    private int StringLength;//一次输出字符串的长度
    private int strTimes;//输出字符串的次数    
    private int timeLimit;//各级别闯关的时间限制
    private int perScore;//输入正确一次的得分
    public int getStrTimes() {
        return strTimes;
    }
    public Level(int levelNo, int stringLength, int strTimes,  int timeLimit,
            int perScore) {
        super();
        this.levelNo = levelNo;
        this.strTimes = strTimes;
        StringLength = stringLength;
        this.timeLimit = timeLimit;
        this.perScore = perScore;
    }
    public void setStrTimes(int strTimes) {
        this.strTimes = strTimes;
    }
    public int getStringLength() {
        return StringLength;
    }
    public void setStringLength(int stringLength) {
        StringLength = stringLength;
    }
    public long 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 void setLevelNo(int levelNo) {
        this.levelNo = levelNo;
    }
    public int getLevelNo() {
        return levelNo;
    }
}

package com.entity;

import java.util.Random;

public class Game {
    public final char[] base=new char[94];    
    public String printStr(Level l){
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<l.getStringLength();i++){
            sb.append(base[new Random().nextInt(94)]);
        }
        System.out.println(sb);
        return sb.toString();
    }    
    public Game(){
        init();
    }
    public void init(){/*初始化final类*/
        for(int j=0,i=33;i<=126;i++,j++){
            base[j]=(char)i;
        }
//        for(char ch:base){
//            System.out.println(ch);
//        }
    }
    
}

package com.test;

import java.util.Scanner;

import com.entity.Game;
import com.entity.Level;
import com.entity.LevelParam;
import com.entity.Player;

public class TestGame {
    public Level level=LevelParam.levels[0];
    public Player player=new Player();
    public Game game=new Game();
    public static void main(String[] args) {
        TestGame tg=new TestGame();
        Scanner sc=new Scanner(System.in);
        System.out.println("-------------------------------");
        System.out.println("欢迎来到最新科技打字游戏!");
        System.out.println("输入s开始游戏!");
        boolean flag=sc.nextLine().equals("s") ? true :false;
        if(flag){
            tg.doGame();
        }else{
            System.out.println("谢谢使用,再见!");
            System.exit(0);
        }
    }
    public void doGame(){
        while(true){
            for(int i=0;i<level.getStrTimes();i++){
                String computerDo=game.printStr(level);
                String playerDo=player.play();
                printResult(computerDo,playerDo);
                if(i==level.getStrTimes()-1){
                    player.setLevelNo(player.getLevelNo()+1);
                    player.clear();
                }
                    System.out.println("输入正确,您的积分"+player.getCurrScore()+"您的级别"+player.getLevelNo()+"已用时间"+player.getElapsedTime()+"秒");
                
            }
            if(level.getLevelNo()==6){
                System.out.println("恭喜你,通关了!");
                System.exit(0);
            }else{
                level=LevelParam.levels[level.getLevelNo()+1];
            }
        }    
    }
    public void printResult(String out,String in){
        if(!out.equals(in)){
            System.out.println("输入错误,游戏结束!");
            System.exit(0);
        }else if(level.getTimeLimit()<player.getElapsedTime()){
            System.out.println("你输入太慢了,游戏结束!");
            System.exit(0);
        }else{
            player.setCurrScore(player.getCurrScore()+level.getPerScore());
        }
    }

}

 

posted @ 2016-06-09 16:36  guodaxia  阅读(190)  评论(0编辑  收藏  举报