gun fire project 尚未实现多线程处理

package com.fire.action;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;




public class Main {


public static void main(String[] args) {
Main main = new Main();
main.init();
// System.out.println(String.format("%s","1","2"));
// System.out.println(11.11/100);
}
public void init(){
Role aLan = new ALan();
aLan.setYifu(130f);
aLan.setToukui(90f);
aLan.setGun(new XinFengYing());
JiDi jiDi = new JiDi();
caculFight(aLan, jiDi);
}

public void caculFight(Role role, Map map){
i(begin);
while(map.getArmys().size()>0 && role.getBlood()>0){
randomFree();
randomFindAndFire(role,map);
}
i("role message :"+role+", \n armys message :"+map.getArmys()+", \n armys size :"+map.getArmys().size());
i(end);
}

public void i(String message){
System.out.println(new SimpleDateFormat("HH:mm:ss").format(new Date())+" : " +message);
}
public void i(String msg, Object...args){
System.out.println(String.format(new SimpleDateFormat("HH:mm:ss").format(new Date())+" : "+msg, args));
}

public void randomFree(){
try {
Thread.sleep(new Random().nextInt(3000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}


public void fireFree(long time){
try {
Thread.sleep(new Random().nextInt((int)time));
} catch (InterruptedException e) {
e.printStackTrace();
}
}


public void randomFindAndFire(Role role, Map map){
Army army=certionArmy(map); //certion one army
if(isMeFindFirst()){
i(role.getName()+" find "+army.getName()+" first, begin to fire");
fightEachOtherUtilEnd("i",role,army,map);
}else {
i(army.getName()+" find "+role.getName()+" first, begin to fire");
fightEachOtherUtilEnd("he",role,army,map);
}
}
public Army certionArmy(Map map){
return map.getArmys().get(new Random().nextInt(map.getArmys().size()));
}
static boolean reFight = false;
public void fightEachOtherUtilEnd(final String whoBegin,final Role role, final Army army, final Map map){
reFight = true;
while((role.getBlood()>0 || army.getBlood()>0 ) && reFight){
reFight = false;
fireFree(3000);
new Thread(new Runnable() {
@Override
public void run() {
boolean flg=true;
int i=1;
if("i".equals(whoBegin)){
fireFree(100);
}
while(i<=30 && flg){
i++;
fireFree(1000/role.getGun().getSpeed());
int left = alarmOther(i,role, army);
if(left==-1){
i(army.getName() +"is dead, fight end");
map.getArmys().remove(army);
flg=false;
reFight = false;
}else if(left==-2) {
i(role.getName() +"is dead, fight end");
flg = false;
reFight = false;
}
}
reFight = true;
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
boolean flg=true;
int i=1;
if("he".equals(whoBegin)){
fireFree(100);
}
while(i<30 && flg){
i++;
fireFree(1000/army.getGun().getSpeed());
int left = alarmOther(i,army, role);
if(left==-1){
//i(role.getName() +"is dead, fight end");
flg=false;
reFight = false;
}else if(left ==-2){
//i(army.getName() +"is dead, fight end");
flg=false;
reFight = false;
}
}
reFight = true;
}
}).start();
}
}

public int alarmOther( int i,Role role, Role army){
float jingdu = new Float(i==1 ? 1: 1*Math.pow((double)role.getGun().getWending()/100, i));
float alarm = role.getGun().getAlarm()*jingdu -(army.getYifu());
int left =0;
if(alarm>0){
left = new Float(army.getBlood()-alarm).intValue() ;
army.setBlood(left>0 ?left:0 );
}else {
left = army.getBlood();
}
float fy = army.getYifu()-role.getGun().getAlarm()*jingdu;
army.setYifu(fy >0 ?fy:0);
army.setBlood(left>0 ?left:0 );
if(role.getBlood()==0){
return -2;
}
i(role.getName()+"has alarm "+army.getName()+" damage "+ role.getGun().getAlarm()*jingdu+","+army.getName()+"'blood left"+(int)army.getBlood()+", fangyu is "+(int)army.getYifu());
i(role.getName() +"left blood is "+(int)role.getBlood()+",fangyu is "+(int)role.getYifu() );
if(left>0){
return left;
}else {
return -1;
}
}

boolean isMeFindFirst(){
return new Random().nextInt(3) %3 !=0;
}

String begin = "begin to play";
String end = "fight end";
String ifind = "I find army first, begin to fire";
String hefind = "He find me first, begin to fire";
}




 class Map {


protected String name;
protected Army army;
protected List<Army> armys ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Army> getArmys() {
return armys;
}
public void setArmys(List<Army> armys) {
this.armys = armys;
}
public Role getArmy() {
return army;

public void setArmy(Army army) {
this.army = army;


}
 class JiDi extends Map{


public JiDi(){
super.name="Ji Di";
super.army = new Army();
super.armys= new ArrayList<Army>();
for (int i = 1; i < 11; i++) {
Army army = new Army(i);
army.setGun(new AKGun());
armys.add(army);
}

}
}


  class Role {


protected String name;
protected float degree;
protected float wXishu;
protected float hXishu;
protected float width=50;
protected float height=180;
protected float yifu;
protected float toukui;
protected Gun gun;
protected int blood=100;

public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public Gun getGun() {
return gun;
}
public void setGun(Gun gun) {
this.gun = gun;
}
public float getYifu() {
return yifu;
}
public void setYifu(float yifu) {
this.yifu = yifu;
}
public float getToukui() {
return toukui;
}
public void setToukui(float toukui) {
this.toukui = toukui;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getDegree() {
return degree;
}
public void setDegree(float degree) {
this.degree = degree;
}
public float getwXishu() {
return wXishu;
}
public void setwXishu(float wXishu) {
this.wXishu = wXishu;
}
public float gethXishu() {
return hXishu;
}
public void sethXishu(float hXishu) {
this.hXishu = hXishu;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
@Override
public String toString() {
return "Role [name=" + name + ", degree=" + degree + ", wXishu="
+ wXishu + ", hXishu=" + hXishu + ", width=" + width
+ ", height=" + height + ", yifu=" + yifu + ", toukui="
+ toukui + ", gun=" + gun + ", blood=" + blood + "]";
}

}
   class Army extends Role{


public Army(){
super.degree=0.11f;
super.name="Common-Army";
super.wXishu=1.55f;
super.hXishu=1.55f;
super.blood=100;
}

public Army(int index){
this();
super.name=super.name +"-"+index;
}
}
   
   
   class ALan extends Role{


public ALan(){
super.degree=0.99f;
super.name="A-Lan";
super.wXishu=0.75f;
super.hXishu=0.75f;
super.blood=130;
}
}
   class Gun {


protected int alarm;
protected int distance;
protected int range;
protected int speed;
protected float wending;


protected String name;


public int getAlarm() {
return alarm;
}


public void setAlarm(int alarm) {
this.alarm = alarm;
}


public int getDistance() {
return distance;
}


public void setDistance(int distance) {
this.distance = distance;
}


public int getRange() {
return range;
}


public void setRange(int range) {
this.range = range;
}


public int getSpeed() {
return speed;
}


public void setSpeed(int speed) {
this.speed = speed;
}


public float getWending() {
return wending;
}


public void setWending(float wending) {
this.wending = wending;
}


public String getName() {
return name;
}


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

}
   class AKGun extends Gun{


public AKGun(){
super.alarm =45;
super.distance=60;
super.range = 1;
super.name="AK Gun";
super.speed=60;
super.wending=60;
}
}
   class XinFengYing extends AKGun{


public XinFengYing(){
super.alarm =71;
super.distance=95;
super.range = 1;
super.name="Xin Feng Ying";
super.speed=99;
super.wending=90;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静山晚风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值