Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
1 result

Target

Select target project
  • das.s/tp3
  • f19002502/tp3
  • r17010960/tp3
  • l19004806/tp3
  • y19010055/tp3
  • o18034026/tp3
  • z18029613/tp3
  • p19021289/tp3
  • d19027596/tp3
  • f18010428/tp3
  • f19003868/tp3
  • c19022214/tp3
  • c19017929/tp3
  • m16014784/tp3
  • a19028956/tp3
  • c19026071/tp3
  • h18008908/tp3
17 results
Select Git revision
  • master
1 result
Show changes
Commits on Source (9)
Showing
with 146 additions and 10 deletions
# Default ignored files
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" project-jdk-name="13" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/tp3.iml" filepath="$PROJECT_DIR$/tp3.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
...@@ -4,9 +4,11 @@ ...@@ -4,9 +4,11 @@
public class Cell { public class Cell {
private boolean isAlive; private boolean isAlive;
private boolean isRed;
public Cell(){ public Cell(){
this.isAlive = false; this.isAlive = false;
this.isRed = false;
} }
/** /**
...@@ -19,6 +21,8 @@ public class Cell { ...@@ -19,6 +21,8 @@ public class Cell {
return this.isAlive; return this.isAlive;
} }
public boolean isRed() { return this.isRed; }
/** /**
* Determines whether this {@link Cell} is dead or not. * Determines whether this {@link Cell} is dead or not.
* *
...@@ -39,6 +43,9 @@ public class Cell { ...@@ -39,6 +43,9 @@ public class Cell {
this.isAlive = true; this.isAlive = true;
} }
public void setBlue() { isRed = false; }
public void setRed() { isRed = true; }
/** /**
* Sets the state of this {@link Cell} to dead. * Sets the state of this {@link Cell} to dead.
* *
......
...@@ -39,8 +39,12 @@ public class GameOfLifeGUI extends JFrame { ...@@ -39,8 +39,12 @@ public class GameOfLifeGUI extends JFrame {
for (int x = 0; x < numberOfColumns; x++) for (int x = 0; x < numberOfColumns; x++)
for (int y = 0; y < numberOfRows; y++){ for (int y = 0; y < numberOfRows; y++){
JLabel label = labelGrid[x][y]; JLabel label = labelGrid[x][y];
if(g.getCell(x,y).isAlive()) if(g.getCell(x,y).isAlive()) {
if (g.getCell(x, y).isRed())
label.setForeground(Color.red); label.setForeground(Color.red);
else
label.setForeground(Color.blue);
}
else else
label.setForeground(Color.white); label.setForeground(Color.white);
} }
......
import java.util.Arrays; import java.util.*;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
/** /**
* {@code Grid} instances represent the grid in <i>The Game of Life</i>. * {@code Grid} instances represent the grid in <i>The Game of Life</i>.
...@@ -98,29 +95,68 @@ public class Grid implements Iterable<Cell> { ...@@ -98,29 +95,68 @@ public class Grid implements Iterable<Cell> {
} }
private boolean[][] calculateNextStates() { private boolean[][] calculateNextStates() {
return null; boolean[][] states = new boolean[getNumberOfRows()][getNumberOfColumns()];
for (int i = 0; i < getNumberOfRows(); i++) {
for (int j = 0; j < getNumberOfColumns(); j++) {
states[i][j] = calculateNextState(i, j, getCell(i, j));
}
}
return states;
} }
private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) {
return false; int aliveNeighbours = countAliveNeighbours(rowIndex, columnIndex);
if (cell.isAlive()) {
return aliveNeighbours >= 2 && aliveNeighbours <= 3;
}
if (willBeRed(rowIndex, columnIndex))
getCell(rowIndex, columnIndex).setRed();
else
getCell(rowIndex, columnIndex).setBlue();
return aliveNeighbours == 3;
} }
private int countAliveNeighbours(int rowIndex, int columnIndex) { private int countAliveNeighbours(int rowIndex, int columnIndex) {
return 0; int aliveNeighbours = 0;
List<Cell> cells = getNeighbours(rowIndex, columnIndex);
for (Cell cell : cells) {
if (cell.isAlive())
aliveNeighbours++;
}
return aliveNeighbours;
} }
private List<Cell> getNeighbours(int rowIndex, int columnIndex) { private List<Cell> getNeighbours(int rowIndex, int columnIndex) {
return null; List<Cell> cells = new ArrayList<>();
for (int i = rowIndex - 1; i <= rowIndex + 1; i++) {
for (int j = columnIndex - 1; j <= columnIndex + 1; j++) {
if ((!(i == rowIndex && j == columnIndex)) && i > 0 && j > 0 && i < getNumberOfRows() && j < getNumberOfColumns())
cells.add(getCell(i, j));
}
}
return cells;
} }
private void goToNextState(boolean[][] nextState) { private void goToNextState(boolean[][] nextState) {
for (int i = 0; i < getNumberOfRows(); i++) {
for (int j = 0; j < getNumberOfColumns(); j++) {
if (nextState[i][j])
getCell(i, j).setAlive();
else
getCell(i, j).setDead();
}
}
} }
/** /**
* Sets all {@link Cell}s in this {@code Grid} as dead. * Sets all {@link Cell}s in this {@code Grid} as dead.
*/ */
void clear() { void clear() {
GridIterator iterator = new GridIterator(this);
while (iterator.hasNext()) {
iterator.next().setDead();
}
} }
/** /**
...@@ -131,6 +167,29 @@ public class Grid implements Iterable<Cell> { ...@@ -131,6 +167,29 @@ public class Grid implements Iterable<Cell> {
*/ */
void randomGeneration(Random random) { void randomGeneration(Random random) {
GridIterator iterator = new GridIterator(this);
while (iterator.hasNext()) {
Cell cell = iterator.next();
if (random.nextBoolean()) {
cell.setAlive();
if (random.nextBoolean())
cell.setRed();
else
cell.setBlue();
}
else
cell.setDead();
}
}
private boolean willBeRed(int rowIndex, int cocolumnIndex) {
List<Cell> cells = getNeighbours(rowIndex, cocolumnIndex);
int redCells = 0;
for (Cell cell : cells) {
if (cell.isAlive() && cell.isRed())
redCells++;
}
return redCells >= 2;
} }
} }
# Default ignored files
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" project-jdk-name="13" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/tp3.iml" filepath="$PROJECT_DIR$/tp3.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
File added
File added
File added
File added
File added
File added
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file