Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加患者被治愈,死亡,腾出床位等逻辑,修改健康人员被感染逻辑 #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>VirusBroadcast</groupId>
<artifactId>VirusBroadcast</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
</dependencies>


</project>
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions src/Constants.java → src/main/java/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ public class Constants {
public static float FATALITY_RATE = 0.50f;//fatality_rate病死率,根据2月6日数据估算(病死数/确诊数)为0.02
public static int DIE_TIME = 100;//死亡时间均值,30天,从发病(确诊)时开始计时
public static double DIE_VARIANCE = 1;//死亡时间方差
public static int CITY_CENTER = 400;


}
10 changes: 9 additions & 1 deletion src/Hospital.java → src/main/java/Hospital.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static Hospital getInstance() {
}

private Point point = new Point(800, 100);//第一个床位所在坐标,用于给其他床位定绝对坐标
private List<Bed> beds = new ArrayList<>();
private List<Bed> beds = new ArrayList<Bed>();

private Hospital() {
//根据床位数量调整医院矩形的大小
Expand Down Expand Up @@ -91,4 +91,12 @@ public Bed returnBed(Bed bed) {
bed.setEmpty(false);
return bed;
}

public void returnBed() {
for (Bed bed : beds){
if (!bed.isEmpty()){
bed.setEmpty(true);
}
}
}
}
3 changes: 1 addition & 2 deletions src/Main.java → src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import javax.swing.*;

import java.util.List;
import java.util.Random;

Expand All @@ -11,8 +10,8 @@
*/
public class Main {


public static void main(String[] args) {

initPanel();
initInfected();
}
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions src/MyPanel.java → src/main/java/MyPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
public class MyPanel extends JPanel implements Runnable {

private int pIndex = 0;//人口池PersonPool的下标,用于遍历每个人


public MyPanel() {
Expand All @@ -36,6 +37,7 @@ public void paint(Graphics g) {
if(people==null){
return;
}

for (Person person : people) {
switch (person.getState()) {
case Person.State.NORMAL: {
Expand All @@ -57,11 +59,20 @@ public void paint(Graphics g) {
g.setColor(new Color(0x48FFFC));
break;
}
default: {
//死亡或者其他
g.setColor(new Color(0x444444));
break;
}
}
person.update();//对各种状态的市民进行不同的处理
g.fillOval(person.getX(), person.getY(), 3, 3);

}
pIndex++;
if (pIndex >= people.size()) {
pIndex = 0;
}

//显示数据信息
g.setColor(Color.WHITE);
Expand Down
67 changes: 40 additions & 27 deletions src/Person.java → src/main/java/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Person {

/**
* 市民的状态
*
* <p>
* 市民状态应该需要细分,虽然有的状态暂未纳入模拟,但是细分状态应该保留
*/
public interface State {
Expand Down Expand Up @@ -151,8 +151,8 @@ private void action() {
}
//存在流动意愿的,将进行流动,流动位移仍然遵循标准正态分布
if (moveTarget == null || moveTarget.isArrived()) {
//在想要移动并且没有目标时,将自身移动目标设置为随机生成的符合正态分布的目标点
//产生N(a,b)的数:Math.sqrt(b)*random.nextGaussian()+a
//在想要移动并且没有目标时,将自身移动目标设置为随机生成的符合正态分布的目标点
//产生N(a,b)的数:Math.sqrt(b)*random.nextGaussian()+a
double targetX = targetSig * new Random().nextGaussian() + targetXU;
double targetY = targetSig * new Random().nextGaussian() + targetYU;
moveTarget = new MoveTarget((int) targetX, (int) targetY);
Expand All @@ -165,7 +165,7 @@ private void action() {
double length = Math.sqrt(Math.pow(dX, 2) + Math.pow(dY, 2));//与目标点的距离

if (length < 1) {
//判断是否到达目标点
//判断是否到达目标点
moveTarget.setArrived(true);
return;
}
Expand All @@ -189,8 +189,8 @@ private void action() {
}

if (x > 700) {
//这个700也许是x方向边界的意思,因为画布大小1000x800
//TODO:如果是边界那么似乎边界判断还差一个y方向
//这个700也许是x方向边界的意思,因为画布大小1000x800
//TODO:如果是边界那么似乎边界判断还差一个y方向
moveTarget = null;
if (udX > 0) {
udX = -udX;
Expand All @@ -211,31 +211,22 @@ private void action() {
*/
public void update() {
//@TODO找时间改为状态机
if (state == State.FREEZE || state == State.DEATH) {
return;//如果已经隔离或者死亡了,就不需要处理了
if (state == State.DEATH) {
return;//如果已经死亡了,就不需要处理了
}
//处理已经确诊的感染者(即患者)
//

//记录死亡时刻
if (state == State.CONFIRMED && dieMoment == 0) {
int destiny = new Random().nextInt(10000)+1;//命运数字,[1,10000]随机数
if (1 <= destiny && destiny <= (int)(Constants.FATALITY_RATE * 10000)) {
//如果命运数字落在死亡区间
int dieTime = (int) (Constants.DIE_VARIANCE * new Random().nextGaussian()+Constants.DIE_TIME);
dieMoment = confirmedTime + dieTime;//发病后确定死亡时刻
//System.out.printf("%d,%f,%d\n",destiny,Constants.FATALITY_RATE * 10000,dieTime);
}
else {
dieMoment = -1;//逃过了死神的魔爪
}

int dieTime = (int) (Constants.DIE_VARIANCE * new Random().nextGaussian() + Constants.DIE_TIME);
dieMoment = confirmedTime + dieTime;//发病后确定死亡时刻
}
//*/


if (state == State.CONFIRMED && MyPanel.worldTime - confirmedTime >= Constants.HOSPITAL_RECEIVE_TIME) {
//如果患者已经确诊,且(世界时刻-确诊时刻)大于医院响应时间,即医院准备好病床了,可以抬走了
//如果患者已经确诊,且(世界时刻-确诊时刻)大于医院响应时间,即医院准备好病床了,可以抬走了
Bed bed = Hospital.getInstance().pickBed();//查找空床位
if (bed == null) {
//没有床位了
//没有床位了,等待空床
// System.out.println("隔离区没有空床位");
} else {
//安置病人
Expand All @@ -245,9 +236,30 @@ public void update() {
bed.setEmpty(false);
}
}

/**
* @author: wang hui
* @email: [email protected]
* @date: 2020年02月06日 18:05
*/
//处理医院隔离患者
if (state == State.FREEZE && MyPanel.worldTime < dieMoment && dieMoment > 0) {
int destiny = new Random().nextInt(10000)+1;//命运数字,[1,10000]随机数
//治愈成功
if (destiny > (int)(Constants.FATALITY_RATE * 10000)) {
state = State.NORMAL;
Hospital.getInstance().returnBed();//腾出一张床位
//无论患者被治愈还是死亡,都将该患者送入医院大门口,为简单起见,大门口坐标为市中心坐标(400,400)
x = Constants.CITY_CENTER;
y = Constants.CITY_CENTER;
}
}
//处理病死者
if((state == State.CONFIRMED || state == State.FREEZE )&& MyPanel.worldTime >= dieMoment && dieMoment > 0) {
state = State.DEATH;//患者死亡
if ((state == State.CONFIRMED || state == State.FREEZE) && MyPanel.worldTime >= dieMoment && dieMoment > 0) {
state = State.DEATH;//患者死亡
Hospital.getInstance().returnBed();//腾出一张床位
x = Constants.CITY_CENTER;
y = Constants.CITY_CENTER;
}
//处理发病的潜伏期感染者
if (MyPanel.worldTime - infectedTime > Constants.SHADOW_TIME && state == State.SHADOW) {
Expand All @@ -262,7 +274,8 @@ public void update() {
return;
}
for (Person person : people) {
if (person.getState() == State.NORMAL) {
//去除死亡的人和健康的人
if (person.getState() == State.NORMAL || person.getState() == State.DEATH) {
continue;
}
float random = new Random().nextFloat();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.