From d66e438dc1352ef9784936eea9553d7708cc4d90 Mon Sep 17 00:00:00 2001 From: acr31 Date: Tue, 13 Apr 2021 15:34:32 +0100 Subject: [PATCH 1/3] Minor changes to load resources from classpath rather than files This is needed if you want to jar package this code (for example). --- src/PlayLevel.java | 24 ++++++++++++++++-------- src/engine/helper/Assets.java | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/PlayLevel.java b/src/PlayLevel.java index 890cb3dcf7..6023d85fa9 100644 --- a/src/PlayLevel.java +++ b/src/PlayLevel.java @@ -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; @@ -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))) { + 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)); + MarioGame game = new MarioGame(); + // 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)); } } diff --git a/src/engine/helper/Assets.java b/src/engine/helper/Assets.java index 7581d463f0..73d8252d03 100755 --- a/src/engine/helper/Assets.java +++ b/src/engine/helper/Assets.java @@ -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) { } From 2b8d18d421b25a8c933aaed704d91e419a6b0c13 Mon Sep 17 00:00:00 2001 From: acr31 Date: Tue, 13 Apr 2021 15:38:13 +0100 Subject: [PATCH 2/3] Maven config --- pom.xml | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 pom.xml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000000..2616a0151f --- /dev/null +++ b/pom.xml @@ -0,0 +1,65 @@ + + 4.0.0 + com.github.amidos2006 + mario-ai-framework + jar + 1.0.0-SNAPSHOT + + Mario-AI-Framework + The Mario AI framework is a framework for using AI methods with a version of Super Mario Bros. + + https://github.com/amidos2006/Mario-AI-Framework + + + + Mario-AI-Framework License + https://github.com/amidos2006/Mario-AI-Framework#copyrights + repo + + + + + UTF-8 + + + + + amidos2006 + Ahmed Khalifa + ahmed@akhalifa.com + http://www.akhalifa.com + + + + + https://github.com/amidos2006/Mario-AI-Framework + scm:git:git@github.com:amidos2006/Mario-AI-Framework.git + HEAD + + + + src + + + levels + + + img + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 8 + 8 + + + + + From 65290465701e99d0a69b76762c9125500e9e963d Mon Sep 17 00:00:00 2001 From: acr31 Date: Thu, 15 Apr 2021 14:56:23 +0100 Subject: [PATCH 3/3] restored indentation in main method --- src/PlayLevel.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PlayLevel.java b/src/PlayLevel.java index 6023d85fa9..7b85ba8e93 100644 --- a/src/PlayLevel.java +++ b/src/PlayLevel.java @@ -40,8 +40,8 @@ public static String getLevel(String filepath) { } public static void main(String[] args) { - MarioGame game = new MarioGame(); - // 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)); + MarioGame game = new MarioGame(); + // 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)); } }