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

1.bed 修正注释 #35

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion src/Bed.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public Bed(int x, int y) {
}

/**
* 是否占用了该床位
* 是否空闲
*/
private boolean isEmpty = true;

Expand Down
6 changes: 4 additions & 2 deletions src/Hospital.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public class Hospital extends Point {
public static final int HOSPITAL_X = 720;
public static final int HOSPITAL_Y = 80;
public static int bedInNeed = 0;
private int width;
private int height = 600;

Expand Down Expand Up @@ -93,10 +94,11 @@ public Bed pickBed() {
* @param bed
* @return
*/
public Bed returnBed(Bed bed) {
public void returnBed(Bed bed) {
if (bed != null) {
beds.remove(bed);
bed.setEmpty(true);
beds.add(bed);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

老实说…… remove一遍再add一遍同一个实例,有什么确切的讲究吗?

}
return bed;
}
}
15 changes: 8 additions & 7 deletions src/MyPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public void paint(Graphics g) {
return;
}
for (Person person : people) {
person.update();//对各种状态的市民进行不同的处理

switch (person.getState()) {
case Person.State.NORMAL: {
//健康人
Expand All @@ -62,11 +64,10 @@ public void paint(Graphics g) {
case Person.State.DEATH: {
//死亡患者

g.setColor(new Color(0x000000));
g.setColor(new Color(0x808080));
break;
}
}
person.update();//对各种状态的市民进行不同的处理
g.fillOval(person.getX(), person.getY(), 3, 3);

}
Expand All @@ -77,7 +78,7 @@ public void paint(Graphics g) {

//显示数据信息
g.setColor(Color.WHITE);
g.drawString("城市总人数:" + Constants.CITY_PERSON_SIZE, captionStartOffsetX, captionStartOffsetY);
g.drawString("城市总人数:" + (Constants.CITY_PERSON_SIZE - PersonPool.getInstance().getPeopleSize(Person.State.DEATH)), captionStartOffsetX, captionStartOffsetY);
g.setColor(new Color(0xdddddd));
g.drawString("健康者人数:" + PersonPool.getInstance().getPeopleSize(Person.State.NORMAL), captionStartOffsetX, captionStartOffsetY + captionSize);
g.setColor(new Color(0xffee00));
Expand All @@ -91,11 +92,11 @@ public void paint(Graphics g) {
g.setColor(new Color(0xE39476));
//暂定急需病床数量为 NEED = 确诊发病者数量 - 已隔离住院数量
//
int needBeds = PersonPool.getInstance().getPeopleSize(Person.State.CONFIRMED)
- PersonPool.getInstance().getPeopleSize(Person.State.FREEZE);
/*int needBeds = PersonPool.getInstance().getPeopleSize(Person.State.CONFIRMED)
- PersonPool.getInstance().getPeopleSize(Person.State.FREEZE);*/

g.drawString("急需病床:" + (needBeds > 0 ? needBeds : 0), captionStartOffsetX, captionStartOffsetY + 6 * captionSize);
g.setColor(new Color(0xccbbcc));
g.drawString("急需病床:" + Hospital.bedInNeed , captionStartOffsetX, captionStartOffsetY + 6 * captionSize);
g.setColor(new Color(0x808080));
g.drawString("病死人数:" + PersonPool.getInstance().getPeopleSize(Person.State.DEATH), captionStartOffsetX, captionStartOffsetY + 7 * captionSize);
g.setColor(new Color(0xffffff));
g.drawString("世界时间(天):" + (int) (worldTime / 10.0), captionStartOffsetX, captionStartOffsetY + 8 * captionSize);
Expand Down
10 changes: 7 additions & 3 deletions src/Person.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import javax.net.ssl.HostnameVerifier;
import java.util.List;
import java.util.Random;

Expand Down Expand Up @@ -195,12 +196,12 @@ 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) {
if ((state == State.CONFIRMED || state == State.FREEZE) && dieMoment == 0) {

int destiny = new Random().nextInt(10000) + 1;//幸运数字,[1,10000]随机数
if (1 <= destiny && destiny <= (int) (Constants.FATALITY_RATE * 10000)) {
Expand All @@ -223,6 +224,9 @@ public void update() {
if (bed == null) {

//没有床位了,报告需求床位数
if(Hospital.bedInNeed < PersonPool.getInstance().getPeopleSize(State.FREEZE)) {
Hospital.bedInNeed++;
}

Copy link
Contributor

@GinRyan GinRyan Feb 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需求床位变量这里存在一点问题。为什么要判断需求床位数量小于已冻结的收治患者人数?已经冻结状态下的确诊患者是有床位实例的引用的。在这里是无法继续从医院的床位中寻找空置床位收治患者给与床位引用。所以判断已经收治的数量无从谈起。另外这里不能直接 Hospital.bedInNeed++,update方法会在重绘整个画布时被调用,有多少人就会被调用多少次,所以我考虑这个方法中有很多状态修改的操作也存在疑虑。所以在这里我只加了注释,没有在这里添加实质性代码。

Copy link
Contributor

@GinRyan GinRyan Feb 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议修改这个值的方式不是在这里++。而是在每个Person实例中修改一个状态变量,并且获取时通过循环获取累加。虽然效率低,但是能保持这个值的正确性。

} else {
//安置病人
Expand Down