Skip to content

Commit

Permalink
spring-boot 3.3.3
Browse files Browse the repository at this point in the history
java 21
  • Loading branch information
Edward committed Aug 30, 2024
1 parent 910006c commit 7d312b6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 23 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![Dinosaur Edward Game](game-screenshot.gif)

Dinosaur Edward Game is a game based on the popular Chrome dinosaur game (T-rex), developed in Java 17, using only Java without any auxiliary libraries.
Dinosaur Edward Game is a game based on the popular Chrome dinosaur game (T-rex), developed in Java 21, using only Java without any auxiliary libraries.
The game is created with the help of Spring Boot to start the system and Lombok to make the code less verbose.
Additionally, the game features an artificial intelligence that uses natural selection in a genetic algorithm, allowing you to choose between playing against the network or training it.

Expand All @@ -21,7 +21,7 @@ Enjoy the game and have fun!
## Main Features

- The game is inspired by the Chrome dinosaur game (T-rex).
- Developed in Java 17, using only Java without any auxiliary libraries.
- Developed in Java 21, using only Java without any auxiliary libraries.
- Uses Spring Boot to start the system.
- Uses Lombok to make the code less verbose.
- The game features an artificial intelligence based on a neural network trained by a genetic algorithm.
Expand All @@ -48,11 +48,11 @@ Enjoy the game and have fun!

## System Requirements

- Java Development Kit (JDK) 17
- Java Development Kit (JDK) 21

## How to Run the Game

1. Make sure you have Java 17 installed on your system.
1. Make sure you have Java 21 installed on your system.
2. Clone this repository to your development environment.
3. Navigate to the root directory of the project.
4. Run the following commands in the terminal to compile and execute the game:
Expand Down
26 changes: 19 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
<version>3.3.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>br.com.edward</groupId>
<artifactId>dinosaur</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>

<name>dinosaur</name>
<description>Dinosaur Edward Game</description>

<properties>
<java.version>17</java.version>
<java.version>21</java.version>

<!-- Tests -->
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<jacoco.maven.plugin.version>0.8.8</jacoco.maven.plugin.version>
<maven-surefire-plugin.version>3.1.2</maven-surefire-plugin.version>
<jacoco.maven.plugin.version>0.8.10</jacoco.maven.plugin.version>

<!-- Package -->
<exec-maven-plugin.version>3.1.0</exec-maven-plugin.version>
Expand Down Expand Up @@ -145,6 +145,10 @@
<argument>${project.build.directory}/output</argument>
<argument>--main-jar</argument>
<argument>${project.build.finalName}.jar</argument>
<argument>--app-version</argument>
<argument>${project.version}</argument>
<argument>--vendor</argument>
<argument>Edward</argument>
<argument>--icon</argument>
<argument>${project.basedir}/src/main/resources/icons/icon.png</argument>
<argument>--dest</argument>
Expand All @@ -157,7 +161,7 @@
</plugins>
</build>
</profile>
<profile>
<profile>
<id>windows</id>
<build>
<plugins>
Expand All @@ -181,6 +185,10 @@
<argument>${project.build.directory}/output</argument>
<argument>--main-jar</argument>
<argument>${project.build.finalName}.jar</argument>
<argument>--app-version</argument>
<argument>${project.version}</argument>
<argument>--vendor</argument>
<argument>Edward</argument>
<argument>--icon</argument>
<argument>${project.basedir}/src/main/resources/icons/icon.ico</argument>
<argument>--dest</argument>
Expand Down Expand Up @@ -219,6 +227,10 @@
<argument>${project.build.directory}/output</argument>
<argument>--main-jar</argument>
<argument>${project.build.finalName}.jar</argument>
<argument>--app-version</argument>
<argument>${project.version}</argument>
<argument>--vendor</argument>
<argument>Edward</argument>
<argument>--icon</argument>
<argument>${project.basedir}/src/main/resources/icons/icon.icns</argument>
<argument>--dest</argument>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/br/com/edward/dinosaur/ai/NeuralNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public NeuralNetwork(final NeuralNetwork neuralNetwork) {
this.outputLayer = new Layer(random, neuralNetwork.getOutputLayer());
}

public static Optional<NeuralNetwork> get() {
return ObjectUtil.readObjectFromFile(NeuralNetwork.class);
}

public double[] getOutput(final double[] inputs) {
final var layerInputs = new double[inputs.length];
System.arraycopy(inputs, 0, layerInputs, 0, inputs.length);
Expand All @@ -48,8 +52,4 @@ public double[] getOutput(final double[] inputs) {
public void save() {
ObjectUtil.writeObjectToFile(this);
}

public static Optional<NeuralNetwork> get() {
return ObjectUtil.readObjectFromFile(NeuralNetwork.class);
}
}
11 changes: 5 additions & 6 deletions src/main/java/br/com/edward/dinosaur/entity/Dinosaur.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
@Getter
public class Dinosaur extends BaseEntity {

private boolean better;
private BaseEntity lastEnemy;
private long score;
private final NeuralNetwork neuralNetwork;

private final Polygon polygon;
private final double defaultPositionX;
private final double defaultPositionY;
private boolean better;
private BaseEntity lastEnemy;
private long score;
private EnumDinosaurActions state;
private double jumpSpeed;
private boolean death;
Expand Down Expand Up @@ -182,9 +181,9 @@ public void think(final BaseEntity enemy) {
if (this.referencePositionY == this.defaultPositionY && EnumDinosaurActions.CROUCHING.equals(this.state)) {
this.score += 10;
} else if (this.referencePositionY == this.defaultPositionY && EnumDinosaurActions.RUNNING.equals(this.state)) {
this.score += 100;
this.score += 5;
} else {
this.score -= 150;
this.score -= 50;
}
} else {
this.score += 10;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/br/com/edward/dinosaur/entity/Star.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public double getPositionX() {

@Override
public double getPositionY() {
return (this.referencePositionY / 100F) * super.getConfig().getHeight();
return (this.referencePositionY / 100F) * super.getConfig().getHeight();
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ spring:
application:
name: @project.name@
version: @project.version@
threads:
virtual:
enabled: true
logging:
file:
name: ${user.home}/dinosaur/logs/dinosaur.log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void testNeuralNetwork() {
final var network = new NeuralNetwork();
assertThat(network).isNotNull();

final var output = network.getOutput(new double[] {distance, position, width, height, myPosition, speed});
final var output = network.getOutput(new double[]{distance, position, width, height, myPosition, speed});
assertThat(output).hasSize(2);

assertThat(output[0]).isNotNegative();
Expand Down

0 comments on commit 7d312b6

Please sign in to comment.