面向对象之模拟英雄对战小demo

业务逻辑

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

public class Game {
	Scanner sc = new Scanner(System.in);
	//初始化
	public void init(){
		System.out.println("-------LOL---------");
		System.out.print("请输入你的昵称:");
		String name = sc.next();
		PersonPlayer pp = new PersonPlayer(name);
		ComputerPlayer cp = new ComputerPlayer("电脑");
		start(pp,cp);
	}
	//开始游戏
	public void start(Player pp, Player cp){
		System.out.println("游戏开始(" + pp.name + "vs" + cp.name + ")");
		boolean end = false;
		//回合循环
		while(!end){
			//玩家进行攻击
			System.out.print(pp.name + "使用技能:");
			String skill1 = sc.next();
			pp.attack(skill1, cp);
			//电脑进行攻击
			String skill2 = randomSkill();
			cp.attack(skill2, pp);
			System.out.println("电脑使用" + skill2 + "技能");
			//打印血量
			print(pp, cp, skill1, skill2);
			end = win(pp, cp);
		}
	}
	//随机生成电脑的技能(q,w,e,r)
	public String randomSkill(){
		String skill = null;
		int check = new Random().nextInt(4);
		switch (check) {
		case 0:
			skill = "q";
			break;
		case 1:
			skill = "w";
			break;
		case 2:
			skill = "e";
			break;
		case 3:
			skill = "r";
			break;
		default:
			break;
		}
		return skill;
	}
	//打印回合结果
	public void print(Player pp, Player cp, String skill1, String skill2){
		System.out.println(pp.name + "使用" + skill1 + ",打出" + decideHurt(skill1) + "伤害," + cp.name + "还剩" + cp.blood + "血");
		System.out.println(cp.name + "使用" + skill2 + ",打出" + decideHurt(skill2) + "伤害," + pp.name + "还剩" + pp.blood + "血");
	}
	//计算技能伤害
	public int  decideHurt(String skill){
		int hurt = 0;
		switch (skill) {
		case "q":
			hurt = 200;
			break;
		case "w":
			hurt = 300;
			break;
		case "e":
			hurt = 400;
			break;
		case "r":
			hurt = 600;
			break;
		default:
			break;
		}
		return hurt;
	}
	//判断输赢
	public boolean win(Player pp,Player cp){
		boolean flag = false;
		if(pp.blood < 0){
			System.out.println(cp.name + "赢了");
			flag = true;
		}else if(cp.blood < 0){
			System.out.println(pp.name + "赢了");
			flag = true;
		}
		return flag;
	}
}

玩家类

/**
 * 玩家类
 * @author Administrator
 *
 */
public class Player {
	public String name;
	public int blood;
	public Player() {
		super();
	}
	public Player(String name) {
		super();
		this.name = name;
		this.blood = 3000;
	}
	//攻击方法
	public void attack(String skill,Player player){
		if("q".equals(skill)){
			player.blood -= 200;
		}else if("w".equals(skill)){
			player.blood -= 300;
		}else if("e".equals(skill)){
			player.blood -= 400;
		}else if("r".equals(skill)){
			player.blood -= 600;
		}
	}
}

电脑玩家


public class ComputerPlayer extends Player{
	public ComputerPlayer(String name){
		super(name);
	}
	
}

玩家

public class PersonPlayer extends Player{
	public PersonPlayer(String name){
		super(name);
	}
}

测试

import java.util.Random;

/**
 * LOL
 * 请输入游戏昵称: 输入
 * 输入vs电脑产生的昵称
 * 游戏开始
 * 回合循环
 * {
 * 请输入使用的技能:q
 * 你使用q技能,打出200伤害,电脑还有2800血
 * 电脑使用r技能,打出800伤害,你还有2200血
 * }
 * 你输了/你赢了
 * 
 * 界面:init()
 * 人,玩家对象(昵称,血量,攻击方法(键盘输入))
 * 电脑,玩家对象(昵称,血量,攻击方法(随机产生))
 * 游戏对象,(判断输赢结果)
 * @author Administrator
 *
 */
public class Test {
	public static void main(String[] args) {
		new Game().init();
	}
}





上一篇:POJ3660:Cow Contest(Floyd传递闭包)


下一篇:关系代数中除法的SQL实现