Skip to content
Snippets Groups Projects
Commit 31ed353a authored by LABOUREL Arnaud's avatar LABOUREL Arnaud
Browse files

Première version template jeu de la vie

parent c3aaad1c
No related branches found
No related tags found
No related merge requests found
package viewer;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import java.util.Collection;
/**
* A Pixel. Because of antialiasing, each pixel is further decomposed into
* sub-pixels. Each sub-pixels has a color, the color of the pixel is the average
* of the sub-pixels' colors.
*/
record Pixel(int x, int y, Collection<SubPixel> subPixels) {
/**
* Creates a pixel with given coordinates and sub-pixels.
*
* @param x the horizontal coordinate of the pixel on the screen
* @param y the vertical coordinate of the pixel on the screen
* @param subPixels a collection of sub-pixels for this pixel
*/
Pixel {
}
/**
* @return the list of sub-pixels in this pixel
*/
Collection<SubPixel> getSubPixels() {
return subPixels;
}
private Color getAverageColor() {
double red = 0;
double green = 0;
double blue = 0;
int count = 0;
for (SubPixel subPixel : subPixels) {
count++;
Color col = subPixel.getColor();
red += col.getRed();
green += col.getGreen();
blue += col.getBlue();
}
double c = count;
return new Color(red / c, green / c, blue / c, 1.);
}
/**
* Displays the pixel.
*
* @param context the context of the canvas on which to paint.
*/
void render(GraphicsContext context) {
context.setFill(getAverageColor());
context.fillRect(x, y, 1, 1);
}
}
package viewer;
import javafx.scene.paint.Color;
/**
* A subpixel contributes to the color of one pixel. Pixels are usually
* composed of several sub-pixels, whose colors are averaged.
*/
class SubPixel {
private Color color = Color.BLACK;
/**
* Each subpixel has a value that will be used to color them.
*/
final double value;
/**
* Creates a subpixel.
*
* @param value divergence for the corresponding pixel. This will be mapped to a color.
*/
SubPixel(double value) {
this.value = value;
}
/**
* Attributes a color to a subpixel.
*
* @param color the color to give to the subpixel
*/
void setColor(Color color) {
this.color = color;
}
/**
* @return the color of the subpixel. Default is black.
*/
Color getColor() {
return color;
}
/**
* Comparison of two sub-pixels by their values.
*
* @param pix1 first subpixel to compare
* @param pix2 second subpixel to compare
* @return an integer representing the result of the comparison, with the usual convention.
*/
static int compare(SubPixel pix1, SubPixel pix2) {
return Double.compare(pix1.value, pix2.value);
}
}
.root {
-fx-focus-color: transparent;
-fx-font-size: 13px;
}
.background {
-fx-background-color: #1d1d1d;
}
.separator .line {
-fx-border-color: #fff;
}
.button {
-fx-background-color: derive(#1d1d1d, 20%);
-fx-text-fill: #fff;
}
.button:hover,
.button:selected {
-fx-background-color: #fff;
-fx-background-insets: 0 0 -1px 0, 0, 1px, 2px;
-fx-background-radius: 5px, 5px, 4px, 3px;
-fx-text-fill: derive(#1d1d1d, 20%);
}
.label {
-fx-text-fill: #fff;
}
.cell-pane {
-fx-background-color: derive(#1d1d1d, 20%);
}
.alive {
-fx-background-color: #fff;
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import view.MatrixPane?>
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308"
styleClass="background" stylesheets="@style.css"
xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="controller.Controller">
<padding>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
</padding>
<children>
<HBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="24.0"
prefWidth="980.0" spacing="10.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
<children>
<Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL"
prefHeight="24.0" prefWidth="6.0"/>
<ToggleButton fx:id="playToggleButton" maxHeight="-Infinity" maxWidth="-Infinity"
mnemonicParsing="false" onAction="#playToggleButtonAction" prefHeight="24.0"
prefWidth="62.0" styleClass="button" text="Play"/>
<ToggleButton fx:id="pauseToggleButton" maxHeight="-Infinity" maxWidth="-Infinity"
mnemonicParsing="false" onAction="#pauseToggleButtonAction" prefHeight="24.0"
prefWidth="71.0" styleClass="button" text="Pause"/>
<Button fx:id="resetButton" maxHeight="-Infinity" maxWidth="-Infinity"
mnemonicParsing="false" onAction="#resetButtonAction" prefHeight="24.0" prefWidth="70.0"
text="Reset"/>
<Button fx:id="clearButton" maxHeight="-Infinity" maxWidth="-Infinity"
mnemonicParsing="false" onAction="#clearButtonAction" prefHeight="24.0" prefWidth="70.0"
text="Clear"/>
<Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL"
prefHeight="24.0" prefWidth="6.0"/>
<Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL"
prefHeight="24.0" prefWidth="6.0"/>
<Label maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="24.0" prefWidth="103.0"
text="Generation"/>
<Label fx:id="generationNumberLabel" alignment="CENTER_RIGHT" contentDisplay="TEXT_ONLY"
maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="24.0" prefWidth="99.0"/>
<Separator maxHeight="-Infinity" maxWidth="-Infinity" orientation="VERTICAL"
prefHeight="24.0" prefWidth="6.0"/>
</children>
</HBox>
<MatrixPane fx:id="matrixPane" alignment="CENTER" hgap="1.0"
maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="600.0" prefWidth="980.0" vgap="1.0"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="35.0"/>
</children>
</AnchorPane>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.canvas.Canvas?>
<GridPane fx:controller="viewer.Controller"
xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<Canvas fx:id="canvas" width="1200" height="900"/>
</GridPane>
\ No newline at end of file
package mandelbrot;
import static net.obvj.junit.utils.matchers.AdvancedMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class ComplexTest {
private Complex onePlusI;
private Complex minusI;
private Complex minusOne;
private Complex oneMinusI;
private Complex twoI;
private Complex two;
private Complex one;
private Complex i;
private Complex zero;
@BeforeEach
void initializeTestValues(){
onePlusI = new Complex(1,1);
minusI = new Complex(0,-1);
minusOne = new Complex(-1,0);
oneMinusI = new Complex(1, -1);
twoI = new Complex(0,2);
two = new Complex(2,0);
one = new Complex(1,0);
i = new Complex(0,1);
zero = new Complex(0,0);
}
@Test
void testEquals(){
assertThat(onePlusI, is(equalTo(onePlusI)));
assertThat(onePlusI, is(equalTo(new Complex(1, 1))));
assertThat(two, is(not(equalTo(twoI))));
}
@Test
void testGetReal(){
assertThat(two.getReal(), is(closeTo(2., Helpers.EPSILON)));
assertThat(onePlusI.getReal(), is(closeTo(1., Helpers.EPSILON)));
assertThat(oneMinusI.getReal(), is(closeTo(1., Helpers.EPSILON)));
}
@Test
void testGetImaginary(){
assertThat(two.getImaginary(), is(closeTo(0., Helpers.EPSILON)));
assertThat(onePlusI.getImaginary(), is(closeTo(1., Helpers.EPSILON)));
assertThat(oneMinusI.getImaginary(), is(closeTo(-1., Helpers.EPSILON)));
}
@Test
void testOne(){
assertThat(Complex.ONE.getReal(), is(closeTo(1., Helpers.EPSILON)));
assertThat(Complex.ONE.getImaginary(), is(closeTo(0., Helpers.EPSILON)));
}
@Test
void testI(){
assertThat(Complex.I.getReal(), is(closeTo(0., Helpers.EPSILON)));
assertThat(Complex.I.getImaginary(), is(closeTo(1., Helpers.EPSILON)));
}
@Test
void testZero(){
assertThat(Complex.ZERO.getReal(), is(closeTo(0., Helpers.EPSILON)));
assertThat(Complex.ZERO.getImaginary(), is(closeTo(0., Helpers.EPSILON)));
}
@Test
void testNegate(){
assertThat(two.negate(), is(equalTo(new Complex(-2,0))));
assertThat(minusI.negate(), is(equalTo(i)));
assertThat(oneMinusI.negate(), is(equalTo(new Complex(-1, 1))));
}
@Test
void testReciprocal(){
assertThat(one.reciprocal(), is(equalTo(one)));
assertThat(minusI.reciprocal(), is(equalTo(i)));
assertThat(two.reciprocal(), is(equalTo(new Complex(0.5,0))));
assertThat(oneMinusI.reciprocal(), is(equalTo(new Complex(0.5,0.5))));
}
@Test
void testReciprocalOfZero(){
assertThat(()->zero.reciprocal(), throwsException(ArithmeticException.class));
}
@Test
void testSubtract(){
assertThat(zero.subtract(one), is(equalTo(minusOne)));
assertThat(one.subtract(i), is(equalTo(oneMinusI)));
}
@Test
void testDivide(){
assertThat(onePlusI.divide(Complex.ONE), equalTo(onePlusI));
assertThat(Complex.ONE.divide(two), equalTo(new Complex(0.5, 0)));
assertThat(oneMinusI.divide(onePlusI), equalTo(minusI));
}
@Test
void testDivideByZero(){
assertThat(()->one.divide(zero), throwsException(ArithmeticException.class));
}
@Test
void testConjugate(){
assertThat(two.conjugate(), equalTo(two));
assertThat(oneMinusI.conjugate(), equalTo(onePlusI));
}
@Test
void testRotation(){
assertThat(Complex.rotation(Math.PI/2), equalTo(i));
assertThat(Complex.rotation(-Math.PI/2), equalTo(minusI));
assertThat(Complex.rotation(0), equalTo(one));
assertThat(Complex.rotation(Math.PI/4), equalTo(new Complex(Math.sqrt(2)/2., Math.sqrt(2)/2.)));
assertThat(Complex.rotation(Math.PI/3), equalTo(new Complex(1./2., Math.sqrt(3)/2.)));
}
@Test
void testBasicToString(){
assertThat(two.toString(), containsString("2.0"));
assertThat(i.toString(), containsString("i"));
}
@Test
void testToStringFormat(){
assertThat(oneMinusI.toString(), is(equalTo("1.0 - 1.0i")));
assertThat(onePlusI.toString(), is(equalTo("1.0 + 1.0i")));
assertThat(minusI.toString(), is(equalTo("-1.0i")));
assertThat(twoI.toString(), is(equalTo("2.0i")));
assertThat(two.toString(), is(equalTo("2.0")));
}
}
\ No newline at end of file
package model;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class GridTest {
private Grid grid;
@BeforeEach
public void initializeGrid(){
grid = new Grid(3,3);
}
@Test
public void testGetNeighbours(){
assertThat(grid.getNeighbours(1,1), is(notNullValue()));
assertThat(grid.getNeighbours(1,1), hasSize(equalTo(8)));
assertThat(grid.getNeighbours(1,1),
containsInAnyOrder(grid.getCell(0,0),
grid.getCell(0,1),
grid.getCell(0,2),
grid.getCell(1,0),
grid.getCell(1,2),
grid.getCell(2,0),
grid.getCell(2,1),
grid.getCell(2,2)));
}
@Test
public void testCountAliveNeighbours(){
assertThat(grid.countAliveNeighbours(1,1), is(equalTo(0)));
grid.getCell(2,2).setState(CellState.ALIVE);
grid.getCell(0,0).setState(CellState.ALIVE);
assertThat(grid.countAliveNeighbours(1,1), is(equalTo(2)));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment