锁定老帖子 主题:国王和100个犯人-
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2010-01-15
最后修改:2010-01-15
不知道是怎么个好法?是若是OO,第一种稍微强点,可是代码中那么多废话也要命。还有那个老是C++什么的,现在又不是在C++,既然要写java就像点java。
|
|
返回顶楼 | |
发表时间:2010-01-15
最后修改:2010-01-15
初中刚毕业写得不好不要打击我。。。 、
package test; public class Prisoner { private Integer id; private boolean isGuard ; Prisoner(Integer id , Boolean isGuard){ this.id = id; this.isGuard = isGuard; } public boolean isGuard() { return isGuard; } public void setGuard(boolean isGuard) { this.isGuard = isGuard; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } } package test; public class Counter { private Integer record = 0; private Integer dates = 0; public Integer getRecord() { return record; } public void setRecord(Integer record) { this.record = record; } public Integer getDates() { return dates; } public void setDates(Integer dates) { this.dates = dates; } } package test; public class Light { private boolean status = false; public boolean isStatus() { return status; } public void setStatus(boolean status) { this.status = status; } } package test; import java.util.ArrayList; import java.util.List; import java.util.Random; public class Game { private static Counter counter = new Counter(); private static Light light = new Light(); public static Counter getInstance() { return counter; } public synchronized static void record() { counter.setDates(counter.getDates() + 1); if (light.isStatus()) { light.setStatus(false); counter.setRecord(counter.getRecord() + 1); } } public synchronized static void guard(Prisoner prisoner) { counter.setDates(counter.getDates() + 1); if (!prisoner.isGuard() && !light.isStatus()) { light.setStatus(true); prisoner.setGuard(true); } } public synchronized static Integer getRecord() { return counter.getRecord(); } public static void show() { System.out.println(counter.getDates()); } public static void main(String[] args) { List list = new ArrayList(100); list.add(Game.getInstance()); for (int i = 0; i < 100; i++) list.add(new Prisoner(i, false)); Object o = null; Random random = new Random(); while (Game.getRecord() < 99) { o = list.get(random.nextInt(100)); if (o instanceof Counter) Game.record(); if (o instanceof Prisoner) Game.guard((Prisoner) o); } Game.show(); } } |
|
返回顶楼 | |
发表时间:2010-01-15
最后修改:2010-01-15
sunrenen 写道 初中刚毕业写得不好不要打击我。。。 、
package test; public class Prisoner { private Integer id; private boolean isGuard ; Prisoner(Integer id , Boolean isGuard){ this.id = id; this.isGuard = isGuard; } public boolean isGuard() { return isGuard; } public void setGuard(boolean isGuard) { this.isGuard = isGuard; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } } package test; public class Counter { private Integer record = 0; private Integer dates = 0; public Integer getRecord() { return record; } public void setRecord(Integer record) { this.record = record; } public Integer getDates() { return dates; } public void setDates(Integer dates) { this.dates = dates; } } package test; public class Light { private boolean status = false; public boolean isStatus() { return status; } public void setStatus(boolean status) { this.status = status; } } package test; import java.util.ArrayList; import java.util.List; import java.util.Random; public class Game { private static Counter counter = new Counter(); private static Light light = new Light(); public static Counter getInstance() { return counter; } public synchronized static void record() { counter.setDates(counter.getDates() + 1); if (light.isStatus()) { counter.setRecord(counter.getRecord() + 1); } } public synchronized static void guard(Prisoner prisoner) { counter.setDates(counter.getDates() + 1); if (!prisoner.isGuard() && !light.isStatus()) { light.setStatus(true); } } public synchronized static Integer getRecord() { return counter.getRecord(); } public static void show() { System.out.println(counter.getDates()); } public static void main(String[] args) { List list = new ArrayList(100); list.add(Game.getInstance()); for (int i = 0; i < 100; i++) list.add(new Prisoner(i, false)); Object o = null; Random random = new Random(); while (Game.getRecord() < 99) { o = list.get(random.nextInt(100)); if (o instanceof Counter) Game.record(); if (o instanceof Prisoner) Game.guard((Prisoner) o); } Game.show(); } } 这个单线程程序为什么要搞这么多 synchronized方法啊 isStatus()这个方法名字不太好 |
|
返回顶楼 | |
发表时间:2010-01-15
最后修改:2010-01-15
hkliya 写道 sunrenen 写道 初中刚毕业写得不好不要打击我。。。 、
package test; public class Prisoner { private Integer id; private boolean isGuard ; Prisoner(Integer id , Boolean isGuard){ this.id = id; this.isGuard = isGuard; } public boolean isGuard() { return isGuard; } public void setGuard(boolean isGuard) { this.isGuard = isGuard; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } } package test; public class Counter { private Integer record = 0; private Integer dates = 0; public Integer getRecord() { return record; } public void setRecord(Integer record) { this.record = record; } public Integer getDates() { return dates; } public void setDates(Integer dates) { this.dates = dates; } } package test; public class Light { private boolean status = false; public boolean isStatus() { return status; } public void setStatus(boolean status) { this.status = status; } } package test; import java.util.ArrayList; import java.util.List; import java.util.Random; public class Game { private static Counter counter = new Counter(); private static Light light = new Light(); public static Counter getInstance() { return counter; } public synchronized static void record() { counter.setDates(counter.getDates() + 1); if (light.isStatus()) { counter.setRecord(counter.getRecord() + 1); } } public synchronized static void guard(Prisoner prisoner) { counter.setDates(counter.getDates() + 1); if (!prisoner.isGuard() && !light.isStatus()) { light.setStatus(true); } } public synchronized static Integer getRecord() { return counter.getRecord(); } public static void show() { System.out.println(counter.getDates()); } public static void main(String[] args) { List list = new ArrayList(100); list.add(Game.getInstance()); for (int i = 0; i < 100; i++) list.add(new Prisoner(i, false)); Object o = null; Random random = new Random(); while (Game.getRecord() < 99) { o = list.get(random.nextInt(100)); if (o instanceof Counter) Game.record(); if (o instanceof Prisoner) Game.guard((Prisoner) o); } Game.show(); } } 这个单线程程序为什么要搞这么多 synchronized方法啊 isStatus()这个方法名字不太好 呵呵。。习惯了写web程序了。。isXX也是相当说javabean...getXX |
|
返回顶楼 | |
发表时间:2010-01-15
最后修改:2010-01-15
我的算法不知道对不对 假设初始灯是关着的
囚犯有两种状态 1.如果第一次看到灯是亮的那么关灯 2.不满足状态1时什么也不做 计数员有2种状态 1.只要出去了就开灯 2.当所有人都去过广场以后像国王宣布 灯 public class Light { boolean lightStatus = false ; } 囚犯 public class Prisoner { //是否是第一次出去 当出去时遇见灯是亮着的才算第一次 int outTime = 0 ; public int useLight(Light light){ int free = 0 ; if(light.lightStatus&&this.outTime==0) { this.outTime ++ ; }else{ //如果不满足上面的 条件什么也不做 free = 3; } return free ; } } 计数员 public final class SpecialPrisoner extends Prisoner{ int prisonerCnt = 0 ; //计算可以确定多少个犯人来过广场 @Override public int useLight(Light light) { int free = 1 ; if(!light.lightStatus){ //如果灯灭了就增加一个去过广场的囚犯 this.prisonerCnt ++ ; } if(this.prisonerCnt==100){ free = 2 ; } return free; } } 监狱 import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; public class Jail { public static void main(String[] args) { Random ran = new Random(); List<Prisoner> list = new ArrayList<Prisoner>(); //装载囚犯99人和计数员囚犯到监狱 for(int i = 0 ; i<99 ;i++){ list.add(new Prisoner()); } list.add(new SpecialPrisoner()); //进入小黑屋时乱序 Collections.shuffle(list); Light light = new Light(); boolean flag = true; int dayCnt = 0 ; //记录天数 while(flag) { dayCnt++; int free = list.get(ran.nextInt(100)).useLight(light); if(free == 0){ light.lightStatus = false ; }else if(free == 1){ light.lightStatus = true ; }else if(free == 2){ flag = false ; System.out.println("为了自由经过了"+dayCnt+"天"); } } } } |
|
返回顶楼 | |
发表时间:2010-01-16
最后修改:2010-01-16
楼主,我猜你只有半年实际JAVA经验.
OO思维比较乱.看了你的代码大约一分钟左右,随便列以下几点不足. 1.任务的完成,counter通知king为妙,而不是king主动轮询. 或许以后关心有没放风完的不止king一个人,还有queen...等 2.100个人中只有一个是counter,也主是说private boolean isCounter=false; 这样一个域要重复99次. 而 private int turnOffTimes; 这个filed对于99个实例来说,完成是废的.内存利用率比较低. 3.为什么king里面要new 100个person? 你这是认为这100person是king的一个必要组成部分... 4.太多为show 而 show的代码...... 5.其它....懒得说了,,, . |
|
返回顶楼 | |
发表时间:2010-01-16
已经失业半年的 =================================== 灯 package justforfun; public class Light { private static Light _light; // 电灯 private boolean _state; // 电灯的状态 private Light() { _state = false; // 默认是关闭的 } public static Light creatLight() { if (_light == null) { _light = new Light(); } return _light; } public void open() { _state = true; } public void close() { _state = false; } public boolean isOpen() { return _state; } } 犯人 package justforfun; import java.util.Observable; public class Prisoner extends Observable { protected int _no; // 犯人的编号 private boolean _operationFlag; // 是否进行过操作 public Prisoner(int no) { _no = no; _operationFlag = false; } public void operation(Light light) { if (!_operationFlag && !light.isOpen()) { light.open(); _operationFlag = true; } } public static Counter upgradeToCounter(Prisoner prisoner, int size) { return new Counter(prisoner._no, size); } } 计数者 package justforfun; public class Counter extends Prisoner { private int _count = 0; // 关灯的次数 private int _maxCount; // 监狱中罪犯的人数 public Counter(int no, int maxCount) { super(no); _maxCount = maxCount; } @Override public void operation(Light light) { if (light.isOpen()) { light.close(); _count++; if (isEnd()) { setChanged(); notifyObservers("所有的犯人都出来放过风了"); } } } private boolean isEnd() { return _count == _maxCount - 1; } }
监狱 package justforfun; import java.util.ArrayList; import java.util.List; public class Prison { private List<Prisoner> _prison; // 监狱 private int _size; // 监狱大小 private static final int INIT_SIZE = 100; // 监狱初始大小为100 public Prison() { this(INIT_SIZE); } public Prison(int size) { _size = size; _prison = new ArrayList<Prisoner>(_size); } public boolean addPrisoner(Prisoner prisoner) { if (_prison.size() >= _size) { throw new RuntimeException("监狱已满!请申请扩建。"); } else { _prison.add(prisoner); return true; } } public void replacePrisoner(Prisoner prisoner) { _prison.set(prisoner._no, prisoner); } public Prisoner getPrisoner(int no) { if (no > _prison.size()) { throw new RuntimeException("对不起!没有你要找的人。"); } else { return _prison.get(no); } } public int getPrisonSize() { return _prison.size(); } }
国王 package justforfun; import java.util.Observable; import java.util.Observer; import java.util.Random; public class King implements Observer { boolean isEnd = false; // 是否结束 public static final int PRISON_SIZE = 100; public static void main(String[] args) { King king = new King(); Prison prison = initPrison(king); meeting(king, prison); // 犯人们在开会 int day = start(king, prison); System.out.println("过去了" + day / 365 + "年" + day % 365 + "天, 犯人们都解放了"); } private static int start(King king, Prison prison) { Light light = Light.creatLight(); // 无尽的日子开始了... int day = 0; while (!king.isEnd) { // 煎熬中... prison.getPrisoner(new Random().nextInt(PRISON_SIZE)).operation( light); day++; } return day; } // 先抓点犯人扔进监狱 private static Prison initPrison(King king) { Prison prison = new Prison(PRISON_SIZE); for (int i = 0; i < PRISON_SIZE; i++) { Prisoner prisoner = new Prisoner(i); prisoner.addObserver(king); prison.addPrisoner(prisoner); } return prison; } private static void meeting(King king, Prison prison) { Prisoner specialPrisoner = prison.getPrisoner(new Random() .nextInt(PRISON_SIZE)); // 选出一个犯人 specialPrisoner = Prisoner.upgradeToCounter(specialPrisoner, prison .getPrisonSize()); // 犯人升级为计数者 specialPrisoner.addObserver(king); prison.replacePrisoner(specialPrisoner); } // 接收来自犯人的通知 @Override public void update(Observable prisoner, Object content) { System.out.println(content); isEnd = true; } }
|
|
返回顶楼 | |
发表时间:2010-01-16
public class Prison { private Prisoner[] prisoners = new Prisoner[100]; public Prison() { Light light = new Light(); prisoners[0] = new Counter(light); for (int i = 1; i < 100; i++) { prisoners[i] = new OtherPrisioner(light); } } public void run() { int day = 0; while (true) { day++; int i = new java.util.Random().nextInt(100); prisoners[i].setHasGoOut(true); prisoners[i].handleTheLight(); if (i == 0 && prisoners[i].finishCount()) { System.out.println("数完了。"); break; } } for (int i = 0; i < 100; i++) { if (!prisoners[i].hasGoOut()) { System.out.println("全部斩杀。"); break; } } System.out.println("共用了" + day / 365); } public static void main(String[] args) { new Prison().run(); } }
测试先行,先写个总场景吧。 总之就是个各类之间的知识应该尽量隔离吧。 |
|
返回顶楼 | |
发表时间:2010-01-16
mxswl 写道 楼主,我猜你只有半年实际JAVA经验.
OO思维比较乱.看了你的代码大约一分钟左右,随便列以下几点不足. 1.任务的完成,counter通知king为妙,而不是king主动轮询. 或许以后关心有没放风完的不止king一个人,还有queen...等 2.100个人中只有一个是counter,也主是说private boolean isCounter=false; 这样一个域要重复99次. 而 private int turnOffTimes; 这个filed对于99个实例来说,完成是废的.内存利用率比较低. 3.为什么king里面要new 100个person? 你这是认为这100person是king的一个必要组成部分... 4.太多为show 而 show的代码...... 5.其它....懒得说了,,, . 谢谢批评 第一个问题,我这里不就是counter通知king吗?我并没有让king去主动查询哦 第二个问题在第二版代码里改用继承了 第三个问题,国王不是应该知道100个犯人在哪里吗?不知道的话怎么去控制他们玩游戏呢? 第四个问题,本来就是来show的。。。 第五个问题,已经说的不少了,非常感谢! |
|
返回顶楼 | |
发表时间:2010-01-16
linhong_1001 写道 不知道是怎么个好法?是若是OO,第一种稍微强点,可是代码中那么多废话也要命。还有那个老是C++什么的,现在又不是在C++,既然要写java就像点java。
C++没什么不好的啊,C++也是OO的。Java不也是从c++发展过来的吗?刚学Java,觉得Java规范太多,C++灵活些。我觉得我写的很像Java。等号两边加不加空格,if语句怎么写这完全是个人习惯,无关大局,有必要争论吗? |
|
返回顶楼 | |