Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple maven packaging #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.amidos2006</groupId>
<artifactId>mario-ai-framework</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>

<name>Mario-AI-Framework</name>
<description>The Mario AI framework is a framework for using AI methods with a version of Super Mario Bros.
</description>
<url>https://github.com/amidos2006/Mario-AI-Framework</url>

<licenses>
<license>
<name>Mario-AI-Framework License</name>
<url>https://github.com/amidos2006/Mario-AI-Framework#copyrights</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<developers>
<developer>
<id>amidos2006</id>
<name>Ahmed Khalifa</name>
<email>ahmed@akhalifa.com</email>
<url>http://www.akhalifa.com</url>
</developer>
</developers>

<scm>
<url>https://github.com/amidos2006/Mario-AI-Framework</url>
<developerConnection>scm:git:git@github.com:amidos2006/Mario-AI-Framework.git</developerConnection>
<tag>HEAD</tag>
</scm>

<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>levels</directory>
</resource>
<resource>
<directory>img</directory>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
22 changes: 15 additions & 7 deletions src/PlayLevel.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Paths;

Expand All @@ -23,17 +26,22 @@ public static void printResults(MarioResult result) {
}

public static String getLevel(String filepath) {
String content = "";
try {
content = new String(Files.readAllBytes(Paths.get(filepath)));
} catch (IOException e) {
try (Reader r = new InputStreamReader(PlayLevel.class.getResourceAsStream(filepath))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering what this change is for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous version of the code tried to read level files from file paths. This means that you could only use it if you were running the Java code from a particular working directory in the source tree. If you tried to run it somewhere else, or packaged the code in a jar file then it can't load the level. Instead, what this does is load it as a resource - what you do is tell the system which is compiling your code that the levels folder is a resource folder (the maven config does this) and then when the code is built these are copied into the right place and packaged appropriately so that whenever the code is run (either directly or in a jar file) it can find the resources.

(Obviously the previous approach works find for you already so there's no imperative to accept this - but if you want the maven packaging then you should also do this too)

StringWriter writer = new StringWriter();
char[] buf = new char[1024];
int read;
while ((read = r.read(buf)) != -1) {
writer.write(buf, 0, read);
}
return content;
return writer.toString();
} catch (IOException e) {
return "";
}
}

public static void main(String[] args) {
MarioGame game = new MarioGame();
// printResults(game.playGame(getLevel("../levels/original/lvl-1.txt"), 200, 0));
printResults(game.runGame(new agents.robinBaumgarten.Agent(), getLevel("./levels/original/lvl-1.txt"), 20, 0, true));
// printResults(game.playGame(getLevel("/original/lvl-1.txt"), 200, 0));
printResults(game.runGame(new agents.robinBaumgarten.Agent(), getLevel("/original/lvl-1.txt"), 20, 0, true));
}
}
2 changes: 1 addition & 1 deletion src/engine/helper/Assets.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void init(GraphicsConfiguration gc) {
private static Image getImage(GraphicsConfiguration gc, String imageName) throws IOException {
BufferedImage source = null;
try {
source = ImageIO.read(Assets.class.getResourceAsStream(imageName));
source = ImageIO.read(Assets.class.getResourceAsStream("/"+imageName));
} catch (Exception e) {
}

Expand Down