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("输入错误,游戏无法启动");
}
}
}