Quickt的抽象方法和接口的运用

在这里插入代码片/**

  • 游戏类

  • @author Chen HuiHong

  • @author 2020年5月17日{time}

  • @Tel 18073799961
    */
    public abstract class Game {
    private Player plsyer;//引用玩家类为属性

    public Game() {
    super();
    }

    public abstract String printStr();//输出字符串返回字符串用于和玩家的输入进行比较

    public abstract void prinResult(String out,String in) ;// 比较游戏输出out和玩家输入in,根据比较结果输出相应信息

    public Game(Player plsyer) {
    super();
    this.plsyer = plsyer;
    }

    public Player getPlsyer() {
    return plsyer;
    }

    public void setPlsyer(Player plsyer) {
    this.plsyer = plsyer;
    }

}
在这里插入图片描述
/**

  • 游戏类

  • @author Chen HuiHong

  • @author 2020年5月17日{time}

  • @Tel 18073799961
    */
    public abstract class Game {
    private Player plsyer;//引用玩家类为属性

    public Game() {
    super();
    }

    public abstract String printStr();//输出字符串返回字符串用于和玩家的输入进行比较

    public abstract void prinResult(String out,String in) ;// 比较游戏输出out和玩家输入in,根据比较结果输出相应信息

    public Game(Player plsyer) {
    super();
    this.plsyer = plsyer;
    }

    public Player getPlsyer() {
    return plsyer;
    }

    public void setPlsyer(Player plsyer) {
    this.plsyer = plsyer;
    }

}

/**

  • 信息存储类
  • @author Chen HuiHong
  • @author 2020年5月17日{time}
  • @Tel 18073799961
    */
    public class LevelParam {
    public final 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);
    }

}

/**

  • 玩家类

  • @author Chen HuiHong

  • @author 2020年5月17日{time}

  • @Tel 18073799961
    */
    public abstract class Player {
    private int levelNo;//当前级别
    private int currScore;//玩家积分
    private long startTime;//开始时间
    private int elapsedTime;//当前级别已用时间

    public Player() {
    super();
    }

    public void show(){
    System.out.println("");
    }

    public Player(int levelNo, int currScore, long startTime, int elapsedTime) {
    super();
    this.levelNo = levelNo;
    this.currScore = currScore;
    this.startTime = startTime;
    this.elapsedTime = elapsedTime;
    }

    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;
    }

    public abstract void play() ;//玩游戏

}

import java.util.Random;

/**

  • 游戏类的实现类

  • @author Chen HuiHong

  • @author 2020年5月17日{time}

  • @Tel 18073799961
    */
    public class SxGame extends Game {

    public SxGame() {

    }

    public SxGame(Player plsyer) {
    super(plsyer);
    // TODO Auto-generated constructor stub
    }

    @Override
    //StringBuffer()
    //构造一个其中不带字符的字符串缓冲区,其初始容量为 16 个字符。
    //Appendable append(char c) 向此 Appendable 添加指定字符。
    public String printStr() {
    Level level =new Level();
    StringBuffer buffer = new StringBuffer();
    Random random = new Random();
    int str = LevelParam.LEVELS[this.getPlsyer()/玩家类对象/.getLevelNo()/得到当前级别/ - 1]
    .getStrLength()/玩家类对象/;
    for(int i = 0;i < str;i++) {
    int rand = random.nextInt(str);
    switch(rand) {
    case 0:
    buffer.append(“1”);
    break;
    case 1:
    buffer.append(“2”);
    break;
    case 2:
    buffer.append(“3”);
    break;
    case 3:
    buffer.append(“4”);
    break;
    case 4:
    buffer.append(“5”);
    break;
    case 5:
    buffer.append(“6”);
    break;

     	}
     }
     System.err.println(buffer.toString());
     return buffer.toString();
    

    }

    @Override
    public void prinResult(String out, String in) {
    if(out != null && in != null) {
    if(out.equals(in)) {
    long time = System.currentTimeMillis();//当前时间currentTimeMillis()
    //如果超时
    if((time-this.getPlsyer().getStartTime())/1000>LevelParam.LEVELS
    [this.getPlsyer().getLevelNo()-1].getTimeLimit())
    {
    System.out.println(“已超时,您的速度有待提高”);
    System.out.println(“最终级别:” + this.getPlsyer().getLevelNo() +",最终玩家积分:"
    + this.getPlsyer().getCurrScore() + “,最终玩家用时:” + this.getPlsyer().getElapsedTime()
    );
    System.exit(1);
    }
    else
    {
    //当前说获得分值
    this.getPlsyer().setCurrScore(this.getPlsyer().getCurrScore()+
    LevelParam.LEVELS[this.getPlsyer().getLevelNo()-1].getPerScore());
    //游戏所需时间
    this.getPlsyer().setElapsedTime((int)(time-this.getPlsyer().getStartTime())/1000);
    int Level = this.getPlsyer().getLevelNo();
    int score = this.getPlsyer().getCurrScore();
    int usetime = this.getPlsyer().getElapsedTime();
    System.out.println(“输入正确,您的级别是(” +Level+ “),您的积分是( “+score+” ),已用时间( “+usetime+” )”);
    if(Level==6)
    {

                     System.out.println("游戏结束");
                     System.out.println("最终级别:" + this.getPlsyer().getLevelNo() +",最终玩家积分:" 
     						+ this.getPlsyer().getCurrScore() + ",最终玩家用时:" + this.getPlsyer().getElapsedTime()
     								);
                     System.exit(1);
                 }
             }
             System.out.println("输入正确,");
        
     		
     	}else {
     		System.out.println("输入错误,退出");
     		System.out.println("最终级别:" + this.getPlsyer().getLevelNo() +",最终玩家积分:" 
     				+ this.getPlsyer().getCurrScore() + ",最终玩家用时:" + this.getPlsyer().getElapsedTime()
     						);
     		  System.exit(0);
     	}	
     	}
     }
    

}

**

  • 玩家类的实现类

  • @author Chen HuiHong

  • @author 2020年5月19日{time}

  • @Tel 18073799961
    */
    public class SxPlayer extends Player {
    private String name;

    public SxPlayer() {
    super();
    }

    public SxPlayer(int levelNo, int currScore, long startTime, int elapsedTime) {
    super(levelNo, currScore, startTime, elapsedTime);
    // TODO Auto-generated constructor stub
    }

    public String getName() {
    return name;
    }

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

    @Override
    public void play() {
    System.out.println( “开始玩游戏****”);
    Scanner input = new Scanner(System.in);
    SxGame sxgame= new SxGame(this);//创建游戏实例

      //异常声明点
      try {
     	sxgame.getPlsyer().setLevelNo(1);//当前级别
     	
      }catch (NullPointerException e) {
     	 System.err.println("级别为空");
     }catch (Exception e) {
     	 System.err.println("级别处错误");
     	 e.printStackTrace();//打印堆栈跟踪
     	System.out.println(e.getMessage());//(获取消息)
     }
      
      
     ass: for (int i = 0; i < LevelParam.LEVELS.length; i++) {
          
      
          setLevelNo(getLevelNo()+1);
          SxPlayer.this.setCurrScore(0);
          System.out.println("恭喜你进入下一级别");
          sxgame.getPlsyer()/*玩家类对象*/.setStartTime(System.currentTimeMillis())/*传入游戏开始时间*/;//游戏开始,给Player的开始时间赋值
          //内层循环
          for(int j=0;j<LevelParam.LEVELS[sxgame.getPlsyer()/*玩家类对象*/.getLevelNo()-1]/*得到级别*/
         		 .getStrTime()/*得到当前级别开始时间*/;j++)
          {                
              String out=sxgame.printStr();//接收游戏输出的字符串
              String in=input.next();//接受玩家输入的字符串
              try {
             	 sxgame.prinResult(out,in) ;//判断
              }catch (Exception e) {
     			System.err.println("比较方法调用错误");
     			e.printStackTrace();
     			System.err.println(e.getMessage());
     		}
              
          }
          }
    
       }
    

    public void info(Player player) {
    System.out.println(“最终级别:” + player.getLevelNo() +",最终玩家积分:"

    • player.getCurrScore() + “,最终玩家用时:” + (player.getElapsedTime()-player.getStartTime()));
      }

}

import java.util.Scanner;

public class TestQuick {

public static void main(String[] args) {
	System.out.println("*********开始游戏******");
	/*
	 * LevelParam llp = new LevelParam(); Scanner se = new Scanner(System.in);
	 * 
	 * 
	 * 
	 * SxGame sx = new SxGame(); System.out.println(sx.printStr()); String num =
	 * se.next(); sx.prinResult(sx.printStr(), num);
	 */
	LevelParam lip =new LevelParam();
	Player py = new SxPlayer();
	py.play();
	SxPlayer xp = (SxPlayer)py;
	xp.info(xp);
	

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值