QuickHit Java编程小游戏

import java.util.Random;
import java.util.Scanner;

public class Game {
    StringBuffer buffer = new StringBuffer();
    Random random = new Random();
    public int Score=0;
    public int time=0;
    public long timing=0;//时差
    public int tr=1;
    public long currentTime=0;
    public int j=0;
    
    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, 5, 12, 15);
    }
    
    public void star() {
        Scanner input=new Scanner(System.in);
        StringBuffer out=null;
        String in=null;
    loop:for( j=0;j<=5;j++) {
        time=0;
        currentTime = System.currentTimeMillis();
        while(time<level[j].strTimes) {
            out=printStr(level[j].strLength);
            System.out.println(out);
            in=input.next();
            printResult(out,in,j,currentTime);
            buffer.delete(0,level[j].strLength); 
            if(tr==0) {
                System.out.println("输入错误");
                break loop;
            }else if(tr==-1) {
                System.out.println("输入超时,退出程序");
                break loop;
            }
        }
        }
}
    public void printResult(StringBuffer out, String in,int j,long currentTime) {// 比较游戏输出out和玩家输入in,根据比较结果输出相应信息
        String out1 = out.toString();
        if(out1.equals(in)) {//输入正确
            Score+=level[j].perScore;
            long currentTime1 = System.currentTimeMillis();
            timing=(currentTime1-currentTime)/1000;
            if(timing>level[j].timeLimit) {//是否超时
                tr=-1;
            }else {
            System.out.println("输入正确,您的积分"+Score+",您的等级"+level[j].levelNo+",已用时间"+timing+"秒");
            time++;
            }
        }else
        {
        tr=0;
        }
    }
    public StringBuffer printStr(int strLength) {// 输出字符串,返回字符串用于和玩家输入比较
        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;
            case 6:
                buffer.append(".");
                break;
            }
        }
        
        return buffer;
    }
}

 

public class Level {

    public int levelNo;
    public int strLength;// 输出字符串长度
    public int strTimes;// 各级别输出字符串次数
    public int timeLimit;// 时间限制
    public int perScore;// 各级别正确输入得分

    public Level(int levelNo, int strLength, int strTimes, int timeLimit, int perScore) {
         this.levelNo=levelNo;
         this.strLength=strLength;
         this.perScore=perScore;
         this.timeLimit=timeLimit;
         this.strTimes=strTimes;
    }
}

 

import java.util.Scanner;

public class Player {
        
    public static void main(String[] args) {
        String start="n";
        System.out.println("输入y开始游戏");
        Scanner input=new Scanner(System.in);
        start=input.next();
        if(start.equals("y")) {
            Game gm=new Game();
            gm.star();
        }else {
            System.out.println("输入错误,游戏无法启动");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值