Java 生活游戏:-需要帮助在生活游戏中实现坚实的原则
嗨,我正试图运用开放与封闭、单一责任原则、liskov替代原则、界面分离原则和依赖倒置原则等原则来执行生命游戏程序。但我陷入了困境,无法思考我应该在哪里应用这些原则。我是这方面的新手,如果有人知道,请有人帮助我了解如何继续或如何应用它,我将非常感谢。提前谢谢你。 我上传了代码的某些部分,作为应用这些原则的示例 抽象博弈策略Java 生活游戏:-需要帮助在生活游戏中实现坚实的原则,java,eclipse,conways-game-of-life,Java,Eclipse,Conways Game Of Life,嗨,我正试图运用开放与封闭、单一责任原则、liskov替代原则、界面分离原则和依赖倒置原则等原则来执行生命游戏程序。但我陷入了困境,无法思考我应该在哪里应用这些原则。我是这方面的新手,如果有人知道,请有人帮助我了解如何继续或如何应用它,我将非常感谢。提前谢谢你。 我上传了代码的某些部分,作为应用这些原则的示例 抽象博弈策略 public abstract class AbstractGameStratedgy implements GameStratedy { private Rule
public abstract class AbstractGameStratedgy implements GameStratedy {
private Rule [] rules;
@Override
public void setRules(Rule[] rules) {
this.rules = rules;
}
@Override
public Rule[] getRules() {
return rules;
}
@Override
public Set<Cell> findNeighbours(Cell cell,Set<Cell> liveCells)
{
HashSet<Cell> neighbours=new HashSet<Cell>();
int x=cell.getX();
int y=cell.getY();
Cell tempCell;
for(int i=x-1;i<=x+1;i++)
{
if(i<0) continue;
for(int j=y-1;j<=y+1;j++)
{
if(j<0) continue;
tempCell=new Cell(i, j);
if(liveCells.contains(tempCell))
{
tempCell.setState(State.LIVE);
}
neighbours.add(tempCell);
}
}
return neighbours;
}
}
规则执行者
public class RuleRunner {
private GameStratedy gameStratedy;
public RuleRunner(GameStratedy gameStratedy)
{
this.gameStratedy = gameStratedy;
}
public Set<Cell> applyRules(Set<Cell> liveCells) {
HashSet<Cell> nextGeneration=new HashSet<Cell>();
Set<Cell> neighbouringCells;
for(Cell cellFromCurrentGeneration: liveCells)
{
processCell(cellFromCurrentGeneration,liveCells,nextGeneration);
neighbouringCells=gameStratedy.findNeighbours(cellFromCurrentGeneration, liveCells);
for(Cell neighbouringCell:neighbouringCells)
{
processCell(neighbouringCell,liveCells,nextGeneration);
}
}
return filterDead(nextGeneration);
}
private Set<Cell> filterDead(HashSet<Cell> nextGeneration) {
Iterator<Cell> iterator = nextGeneration.iterator();
while(iterator.hasNext())
{
if(State.DEAD.equals(iterator.next().getState()))
{
iterator.remove();
}
}
return nextGeneration;
}
private void processCell(Cell cell,Set<Cell> currentGeneration,Set<Cell> nextGeneration)
{
if(nextGeneration.contains(cell)) return; // already processed
cell=cell.createCopy();
State nextState=cell.getState();
for(Rule rule:gameStratedy.getRules())
{
nextState=rule.nextState(cell.getState(), findLiveNeighbourCount(cell, currentGeneration));
if(!cell.getState().equals(nextState))
{
break;
}
}
cell.setState(nextState);
nextGeneration.add(cell);
}
private int findLiveNeighbourCount(Cell cell,Set<Cell> liveCells)
{
int count=0;
for(Cell c:gameStratedy.findNeighbours(cell, liveCells))
{
if(State.LIVE.equals(c.getState())) count++;
}
return count;
}
}
公共类规则运行程序{
私人博弈策略博弈策略;
公共规则运行者(GameStratedy GameStratedy)
{
this.gameStratedy=gameStratedy;
}
公共集合applyRules(集合liveCells){
HashSet nextGeneration=新HashSet();
设置相邻单元;
for(Cell cellFromCurrentGeneration:liveCells)
{
processCell(cellFromCurrentGeneration,liveCells,nextGeneration);
NeighturingCells=gameStratedy.findNeighbours(cellFromCurrentGeneration,liveCells);
用于(单元邻接单元:邻接单元)
{
processCell(邻近细胞、活细胞、下一代);
}
}
返回过滤器端(下一代);
}
私有集过滤器头(哈希集下一代){
迭代器迭代器=nextGeneration.Iterator();
while(iterator.hasNext())
{
if(State.DEAD.equals(iterator.next().getState()))
{
iterator.remove();
}
}
返回下一代;
}
私有void processCell(单元格单元格,设置currentGeneration,设置nextGeneration)
{
if(nextGeneration.contains(cell))返回;//已处理
cell=cell.createCopy();
State-nextState=cell.getState();
for(规则:gameStratedy.getRules())
{
nextState=rule.nextState(cell.getState(),findLiveNeighbourCount(cell,currentGeneration));
如果(!cell.getState().equals(nextState))
{
打破
}
}
单元格设置状态(下一状态);
添加(单元格);
}
private int findLiveNeighbourCount(单元格、集合liveCells)
{
整数计数=0;
对于(单元格c:gameStratedy.findNeighbours(单元格,liveCells))
{
if(State.LIVE.equals(c.getState())count++;
}
返回计数;
}
}
我之前只共享了我实现的Github存储库中README.md文件的链接,版主向我表达了一个真正的担忧,如果链接后面的页面被删除了怎么办?我提供了一个答案,我希望这有助于理解解决方案,而不必参考我的README.md文件或代码
这个问题是一年前提出来的。你可能已经找到了坚实的原则。由于我最近用PHP OOP解决了Conway的生活游戏,并应用了SOLID设计原则,目的是与工作中的同事分享我对这些原则的理解,因此我在stackoverflow上分享了我解决这个问题的方法,作为这个问题的答案,这个问题到目前为止还没有答案。我希望这对所有来到这里的人都会有所帮助,他们希望通过在康威的《生命问题游戏》中实践这一原则来明确自己的想法
以下是我对需要开设哪些课程的思考进展:
步骤1:我从问题陈述中的明显类开始,这些类是Board和Cell,Board由许多单元格组成
步骤2:在应用单一责任原则时,,一个类应该有一个单一的责任,并且只有一个改变的理由,对于董事会和单元格,我将这两个类划分为以下几类:
- 线路板(用于维护线路板尺寸和活动单元位置,以及getNeighbourCount()方法)
- BoardRenderer(用于在介质上绘制电路板)
- BoardPersister(用于持久化,即在两个步骤之间将线路板布局从内存中保存到MySQL之类的持久存储中)
- BoardInitializer(用于游戏开始时板上活细胞的初始排列)
- 对于单元格,我只需要CellRenderer,因为此时我开始 意识到我的Cell类除了渲染之外没有其他工作要做 (一种方式为活动单元格,另一种方式为非活动单元格)。是董事会 它将知道它的尺寸和位置 活动单元格(因此也是非活动单元格的位置)。我 不需要单元类
- 董事会
- 游戏控制器
- 卷边板(延伸部分)
public class RuleRunner { private GameStratedy gameStratedy; public RuleRunner(GameStratedy gameStratedy) { this.gameStratedy = gameStratedy; } public Set<Cell> applyRules(Set<Cell> liveCells) { HashSet<Cell> nextGeneration=new HashSet<Cell>(); Set<Cell> neighbouringCells; for(Cell cellFromCurrentGeneration: liveCells) { processCell(cellFromCurrentGeneration,liveCells,nextGeneration); neighbouringCells=gameStratedy.findNeighbours(cellFromCurrentGeneration, liveCells); for(Cell neighbouringCell:neighbouringCells) { processCell(neighbouringCell,liveCells,nextGeneration); } } return filterDead(nextGeneration); } private Set<Cell> filterDead(HashSet<Cell> nextGeneration) { Iterator<Cell> iterator = nextGeneration.iterator(); while(iterator.hasNext()) { if(State.DEAD.equals(iterator.next().getState())) { iterator.remove(); } } return nextGeneration; } private void processCell(Cell cell,Set<Cell> currentGeneration,Set<Cell> nextGeneration) { if(nextGeneration.contains(cell)) return; // already processed cell=cell.createCopy(); State nextState=cell.getState(); for(Rule rule:gameStratedy.getRules()) { nextState=rule.nextState(cell.getState(), findLiveNeighbourCount(cell, currentGeneration)); if(!cell.getState().equals(nextState)) { break; } } cell.setState(nextState); nextGeneration.add(cell); } private int findLiveNeighbourCount(Cell cell,Set<Cell> liveCells) { int count=0; for(Cell c:gameStratedy.findNeighbours(cell, liveCells)) { if(State.LIVE.equals(c.getState())) count++; } return count; } }