带你深入了解Java!QuickHit 项目!

< QuickHit 项目 >

在这里插入图片描述

需求分析

分析实体

  • 级别信息:
    级别编号
    字符长度(游戏难度)
    字符出现的次数(每个等级都不一样)
  • 时间限制
    增长积分(每通关一次,增长的几分数)
    玩家信息:
    当前积分
    当前级别的开始时间 1s = 1000ms
    当前级别已用时间
    级别信息
  • 游戏信息: 玩游戏(程序入口)
    没有属性
    方法1:输出每关的随机字符串
    方法2:游戏结算
    在这里插入图片描述
    在这里插入图片描述

代码如下

  • 信息类
package gatsby4y15;

/**
 * 信息类
 * @author Douer
 *
 */
public class Message {
	//等级信息
	private int grades;
	//不同等级输出字符长度
	private int length;
	//关数
	private int close;
	//每级别关的时间限制
	private int timess;
	//每级别通关增长积分
	private int integrals;
	@Override
	public String toString() {
		return "Message [grades=" + grades + ", length=" + length + ", close=" + close + ", timess=" + timess
				+ ", integrals=" + integrals + "]";
	}
	
	public Message(int grades, int length, int close, int timess, int integrals) {
		super();
		this.grades = grades;
		this.length = length;
		this.close = close;
		this.timess = timess;
		this.integrals = integrals;
	}
	public Message() {
		super();
	}	
	
	public int getGrades() {
		return grades;
	}
	public void setGrades(int grades) {
		this.grades = grades;
	}
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public int getClose() {
		return close;
	}
	public void setClose(int close) {
		this.close = close;
	}
	public int getTimess() {
		return timess;
	}
	public void setTimess(int timess) {
		this.timess = timess;
	}
	public int getIntegrals() {
		return integrals;
	}
	public void setIntegrals(int integrals) {
		this.integrals = integrals;
	}

}
  • 存储信息类
package gatsby4y15;

/**
 * 存储信息类
 * @author Douer
 *
 */
public class Messages {
	//定义数组存储信息
	public final static Message[] chars = new Message[6];
	static {
		chars[0] = new Message(1,2,10,60,1);
		chars[1] = new Message(2,3,9,50,2);
		chars[2] = new Message(3,4,8,40,3);
		chars[3] = new Message(4,5,7,30,8);
		chars[4] = new Message(5,5,6,20,10);
		chars[5] = new Message(6,5,5,15,15);
		}
}
  • 玩家类
package gatsby4y15;

/**
 * 玩家类
 * @author Douer
 *
 */
import java.util.*;
public class Player {
	//等级
	private int grade;
	//目前积分
	private int integral;
	//开始时间
	private long time;
	//已经所用的时间
	private int times;
	Scanner input = new Scanner(System.in);
	//玩游戏方法
	public void playGame() {
		Game game = new Game(this);
		Message[] message = Messages.chars;
		//外循环等级
		for(int one = 0;one < message.length;one++) {
			//初始化等级信息
			this.grade ++;
			this.integral = 0;
			this.time =System.currentTimeMillis();
			//内循环关数
			for(int two = 0;two < message[one].getClose();two++) {
				String gameInput = game.gameGo();
				String playerInput = input.next();
				game.compare(playerInput,gameInput);
			}
		}
	}
	@Override
	public String toString() {
		return "Player [grade=" + grade + ", integral=" + integral + ", time=" + time + ", times=" + times + ", input="
				+ input + "]";
	}
	public int getGrade() {
		return grade;
	}
	public void setGrade(int grade) {
		this.grade = grade;
	}
	public int getIntegral() {
		return integral;
	}
	public void setIntegral(int integral) {
		this.integral = integral;
	}
	public long getTime() {
		return time;
	}
	public void setTime(long time) {
		this.time = time;
	}
	public int getTimes() {
		return times;
	}
	public void setTimes(int times) {
		this.times = times;
	}
	public Player(int grade, int integral, long time, int times) {
		super();
		this.grade = grade;
		this.integral = integral;
		this.time = time;
		this.times = times;
	}
	public Player() {
		super();
	}
}

  • 游戏类
package gatsby4y15;

/**
 * 游戏类
 * @author Douer
 *
 */
public class Game {
	private Player player;
	//游戏输出方法
	public String gameGo() {
		//获取当前等级信息
		int grade = player.getGrade();
		Message message = Messages.chars[grade-1];
		//用于随机字符
		char[] character = {'*','<','>','%','!'};
		String ran = "";
		for(int one = 0;one < message.getLength();one++) {
			int random = (int)(Math.random()*5);
		
			ran += character[random];
		}
		System.out.println(ran);
		return ran;
	}
	//判断方法
	//playerInput 人输入
	//  GameInput 游戏输出
	public void compare(String playerInput,String GameInput) {
		
		if(GameInput.equals(playerInput)) {
			//得出当前时间的毫秒值
			long playTime = System.currentTimeMillis();
			//毫秒计算出秒
			int goTime = (int)((playTime - player.getTime())/1000);
			//计算得出所用的时间
			player.setTimes(goTime);
			//获取玩家等级
//			int one1 = player.getGrade();
//			Message message = Messages.chars[one1-1];
			Message message = Messages.chars[player.getGrade()-1];
			if(message.getTimess() > goTime) {
				//增长积分法
				player.setIntegral(player.getIntegral()+message.getIntegrals());
				//判断是否满级
				Message max = Messages.chars[Messages.chars.length-1];
				if(player.getGrade() == max.getGrades() && player.getIntegral() >= (max.getClose() * max.getIntegrals())) {
					System.out.println("  你已经成神!恭喜通关!");
				}else {
					System.out.print("你的等级为  "+player.getGrade());
					System.out.print("你的积分 "+player.getIntegral());
					System.out.println("已用时间 "+player.getTimes());
				}
			}else {
				System.out.println("你的速度慢到家了!回去再苦练10年吧");
				System.exit(0);
			}
		}else {
			System.out.println("你太菜科!回去在苦练10年吧");
			System.exit(0);
		}
	}

	public Game() {
		super();
	}

	public Game(Player player) {
		super();
		this.player = player;
	}
	
 }
  • 测试类
package gatsby4y15;

/**
 * 测试类
 * @author Douer
 *
 */
public class Text {
	public static void main(String[] args) {
		new Player().playGame();
	}
}

猜你喜欢

转载自blog.csdn.net/Gastby98/article/details/89320965