Skip to content
Snippets Groups Projects
Commit fbf9fbb6 authored by RANDRIANARISON John's avatar RANDRIANARISON John
Browse files

tp3 fini

parent adb13dc8
No related branches found
No related tags found
No related merge requests found
Showing with 193 additions and 50 deletions
......@@ -4,11 +4,16 @@
public class Cell {
private boolean isAlive;
private String color = "Black";
public Cell(){
this.isAlive = false;
}
public void setColor(String hisColor) { color = hisColor; }
public String getColor() { return color; }
/**
* Determines whether this {@link Cell} is alive or not.
*
......@@ -19,6 +24,7 @@ public class Cell {
return this.isAlive;
}
/**
* Determines whether this {@link Cell} is dead or not.
*
......
......@@ -22,7 +22,7 @@ public class GameOfLifeGUI extends JFrame {
labelGrid[x][y] = new JLabel("*");
JLabel label;
if(g.getCell(x,y).isAlive())
labelGrid[x][y].setForeground(Color.red);
labelGrid[x][y].setForeground(Color.black);
else
labelGrid[x][y].setForeground(Color.white);
gridPanel.add(labelGrid[x][y]);
......@@ -39,10 +39,16 @@ public class GameOfLifeGUI extends JFrame {
for (int x = 0; x < numberOfColumns; x++)
for (int y = 0; y < numberOfRows; y++){
JLabel label = labelGrid[x][y];
if(g.getCell(x,y).isAlive())
label.setForeground(Color.red);
if(g.getCell(x,y).isAlive()) {
if ((g.getCell(x,y).getColor()).equals("Black")) {
label.setForeground(Color.black);
} else {
label.setForeground(Color.blue);
}
}
else
label.setForeground(Color.white);
}
}
}
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.*;
/**
* {@code Grid} instances represent the grid in <i>The Game of Life</i>.
......@@ -98,29 +95,95 @@ public class Grid implements Iterable<Cell> {
}
private boolean[][] calculateNextStates() {
return null;
boolean[][] tableau=new boolean[numberOfRows][numberOfColumns];
for (int row=0;row<numberOfRows;row++){
for (int column=0;column<numberOfColumns;column++){
tableau[row][column]=calculateNextState(row,column,getCell(row,column));
}
}
return tableau;
}
private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) {
return false;
boolean Nextstate = cell.isAliveInNextState(this.countAliveNeighbours(rowIndex, columnIndex));
if (Nextstate) {
if (this.countAliveNeighbours(rowIndex, columnIndex) != 2) {
cell.setColor(this.countColorNeighbours(rowIndex, columnIndex));
}
}
return Nextstate;
}
private int countAliveNeighbours(int rowIndex, int columnIndex) {
return 0;
int AliveNeighbours=0;
List<Cell> Neighbours=getNeighbours(rowIndex,columnIndex);
for(Cell cell:Neighbours){
if(cell.isAlive()){
AliveNeighbours++;
}
}
return AliveNeighbours;
}
private List<Cell> getNeighbours(int rowIndex, int columnIndex) {
return null;
List<Cell> listofNeighbours= new ArrayList<Cell>(0);
listofNeighbours.add(getCell(rowIndex+1,columnIndex-1));
listofNeighbours.add(getCell(rowIndex+1,columnIndex));
listofNeighbours.add(getCell(rowIndex+1,columnIndex+1));
listofNeighbours.add(getCell(rowIndex,columnIndex+1));
listofNeighbours.add(getCell(rowIndex-1,columnIndex+1));
listofNeighbours.add(getCell(rowIndex-1,columnIndex));
listofNeighbours.add(getCell(rowIndex-1,columnIndex-1));
listofNeighbours.add(getCell(rowIndex,columnIndex-1));
return listofNeighbours;
}
private String countColorNeighbours(int rowIndex, int columnIndex) {
String CounterColorCell;
int Black = 0;
int Blue = 0;
for (Cell NeighboursCell : this.getNeighbours(rowIndex, columnIndex)) {
if (NeighboursCell.isAlive()) {
if ((NeighboursCell.getColor()).equals("Black")) {
Black+=1;
}
else {
Blue+=1;
}
}
}
if (Black>Blue) {
CounterColorCell="Black";
}
else {
CounterColorCell="Blue";
}
return CounterColorCell;
}
private void goToNextState(boolean[][] nextState) {
for (int ligne = 0; ligne < numberOfRows; ligne++) {
for (int column = 0; column < numberOfColumns; column++) {
if (nextState[ligne][column])
getCell(ligne, column).setAlive();
else
getCell(ligne, column).setDead();
}
}
}
/**
* Sets all {@link Cell}s in this {@code Grid} as dead.
*/
void clear() {
for(int ligne=0;ligne<numberOfRows;ligne++){
for(int column=0;column<numberOfColumns;column++){
getCell(ligne,column).setDead();
}
}
}
/**
......@@ -131,6 +194,23 @@ public class Grid implements Iterable<Cell> {
*/
void randomGeneration(Random random) {
for(int ligne=0;ligne<numberOfRows;ligne++){
for(int column=0;column<numberOfColumns;column++){
Cell cell=getCell(ligne,column);
if(random.nextBoolean()){
cell.setAlive();
if (random.nextBoolean()) {
cell.setColor("Black");
}
else {
cell.setColor("Blue");
}
}
else
cell.isDead();
}
}
}
}
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
Grid.java
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_15" project-jdk-name="15" 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
tp3.iml 0 → 100644
<?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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment