From 897cb4a1402cf99998db1dc71aa90122c2132a1d Mon Sep 17 00:00:00 2001 From: bigJIU <1572204178@qq.com> Date: Wed, 1 Mar 2023 21:38:39 +0800 Subject: [PATCH 1/2] Add MarioInfo --- .../levels/{ => group0}/lvl65.lvl | 0 Mario-AI-Interface/reps/f_l_sav.rep | Bin 1009 -> 34 bytes Mario-AI-Interface/src/Play.java | 6 ++--- .../src/engine/core/MarioRender.java | 21 +++++++++++++++--- 4 files changed, 21 insertions(+), 6 deletions(-) rename Mario-AI-Interface/levels/{ => group0}/lvl65.lvl (100%) diff --git a/Mario-AI-Interface/levels/lvl65.lvl b/Mario-AI-Interface/levels/group0/lvl65.lvl similarity index 100% rename from Mario-AI-Interface/levels/lvl65.lvl rename to Mario-AI-Interface/levels/group0/lvl65.lvl diff --git a/Mario-AI-Interface/reps/f_l_sav.rep b/Mario-AI-Interface/reps/f_l_sav.rep index e664b0ceec27f1278b667956370b82afd9d1dda2..5642265e8fb13f907e745102e62281ff49fc3dd0 100644 GIT binary patch literal 34 KcmZQzAOrvaA^-sZ literal 1009 ScmZQz7zLvtFd70QCIkQj@c;n; diff --git a/Mario-AI-Interface/src/Play.java b/Mario-AI-Interface/src/Play.java index dc0916d..1ffdb31 100644 --- a/Mario-AI-Interface/src/Play.java +++ b/Mario-AI-Interface/src/Play.java @@ -40,12 +40,12 @@ public class Play { public static byte[] playJavaGame(){ MarioGame game = new MarioGame(); - game.setLives(5); - String levelPath = "./levels/group0/f_l.txt"; // For local + game.setLives(20); + String levelPath = "./levels/group0/lvl1.lvl"; // For local String repPath = "./reps/f_l_sav.rep"; // For local //MarioGame.verbose = true; //Play Game - MarioResult tmpResult = game.playGame(new HumanAgent(false),getLevel(levelPath), 30, repPath,30); + MarioResult tmpResult = game.playGame(new HumanAgent(false),getLevel(levelPath), 1, repPath,30); //Replay //MarioResult tmpResult = game.playGame(Replay.getRepAgentFromFile(repPath),getLevel(levelPath), 30, repPath,30); return Replay.serializeAgentEvents(tmpResult.getAgentEvents()); diff --git a/Mario-AI-Interface/src/engine/core/MarioRender.java b/Mario-AI-Interface/src/engine/core/MarioRender.java index 8ca2fb2..d3bef9b 100644 --- a/Mario-AI-Interface/src/engine/core/MarioRender.java +++ b/Mario-AI-Interface/src/engine/core/MarioRender.java @@ -3,6 +3,7 @@ package engine.core; import javax.swing.*; import engine.helper.Assets; +import engine.helper.GameStatus; import engine.helper.MarioActions; import java.awt.*; @@ -56,8 +57,22 @@ public class MarioRender extends JComponent implements FocusListener { drawStringDropShadow(og, "Buttons: " + pressedButtons, 0, 2, 1); } //Render Status Info - drawString(og,"Game Over! ",11,3,1,0.5f); - drawString(og,"Game Over! ",11,5,1,0.5f); + + switch (world.gameStatus){ + case LOSE: + drawString(og,"Game Over! ",64,40,1,1.5f); + drawString(og,"YOU LOSE! ",66,56,1,1.5f); + break; + case TIME_OUT: + drawString(og,"Game Over! ",64,40,1,1.5f); + drawString(og,"Time OUT ",66,56,1,1.5f); + break; + case WIN: + drawString(og,"CONGRATULATION!",32,40,1,1.5f); + drawString(og,"YOU WIN!!",64,56,1,1.5f); + break; + } + if (scale > 1) { g.drawImage(image, 0, 0, (int) (256 * scale), (int) (240 * scale), null); @@ -69,7 +84,7 @@ public class MarioRender extends JComponent implements FocusListener { public void drawString(Graphics g, String text, int x, int y, int c,float scale){ char[] ch = text.toCharArray(); for (int i = 0; i < ch.length; i++) { - g.drawImage(Assets.font[ch[i] - 32][c], x + i * 8, y,(int) (256 * scale), (int) (240 * scale), null); + g.drawImage(Assets.font[ch[i] - 32][c], x + (int)(i * 8 * scale), y,(int) (8 * scale), (int) (8 * scale), null); } } public void drawStringDropShadow(Graphics g, String text, int x, int y, int c) { From 58beec7b765d2e4b8b457fed14cc6a672354660f Mon Sep 17 00:00:00 2001 From: bigJIU <1572204178@qq.com> Date: Wed, 1 Mar 2023 21:45:41 +0800 Subject: [PATCH 2/2] Delete MarioForwardModel copy --- Mario-AI-Interface/reps/f_l_sav.rep | Bin 34 -> 704 bytes Mario-AI-Interface/src/Play.java | 2 +- .../src/engine/core/MarioGame.java | 212 +----------------- 3 files changed, 7 insertions(+), 207 deletions(-) diff --git a/Mario-AI-Interface/reps/f_l_sav.rep b/Mario-AI-Interface/reps/f_l_sav.rep index 5642265e8fb13f907e745102e62281ff49fc3dd0..9fc5e797f59f7f58c348b7c77c190625830d95ba 100644 GIT binary patch literal 704 zcmd5&%M}A52vkmn3bOWJ=*|G+>b?{ZE(**~Tl>4}y)%=UIcmO(!yiSM4-&zr0QgJb zx;~}3=}9CE+(`wi0NJq@1Mq=TyTy{wacb{w&{8+qL@jXHkI0Ud34;8EZKvr;#j^{x zOePRdQ@3KtCOJZVM|@lUyj+!9ehJP`-kyZ gameEvents = new ArrayList<>(); ArrayList agentEvents = new ArrayList<>(); @@ -185,7 +187,9 @@ public class MarioGame extends KeyAdapter{ agentTimer = new MarioTimer(MarioGame.maxTime); //get actions - boolean[] actions = this.agent.getActions(new MarioForwardModel(this.world), agentTimer); + //boolean[] actions = this.agent.getActions(new MarioForwardModel(this.world), agentTimer); + //TODO: MarioForwardModel + boolean[] actions = this.agent.getActions(null, agentTimer); if (MarioGame.verbose) { if (agentTimer.getRemainingTime() < 0 && Math.abs(agentTimer.getRemainingTime()) > MarioGame.graceTime) { System.out.println("The Agent is slowing down the game by: " @@ -266,207 +270,3 @@ public class MarioGame extends KeyAdapter{ } - -//package engine.core; -// -//import java.util.ArrayList; -//import java.awt.*; -//import java.awt.event.KeyAdapter; -// -//import javax.swing.JFrame; -// -//import agents.HumanAgent; -//import engine.helper.GameStatus; -//import engine.helper.MarioActions; -//import engine.helper.Replay; -// -//public class MarioGame { -// /** -// * the maximum time that agent takes for each step -// */ -// public static final long maxTime = 40; -// /** -// * extra time before reporting that the agent is taking more time that it should -// */ -// public static final long graceTime = 10; -// /** -// * Screen width -// */ -// public static final int width = 256; -// /** -// * Screen height -// */ -// public static final int height = 256; -// /** -// * Screen width in tiles -// */ -// public static final int tileWidth = width / 16; -// /** -// * Screen height in tiles -// */ -// public static final int tileHeight = height / 16; -// /** -// * print debug details -// */ -// public static final boolean verbose = false; -// -// /** -// * pauses the whole game at any moment -// */ -// public boolean pause = false; -// -// /** -// * events that kills the player when it happens only care about type and param -// */ -// private MarioEvent[] killEvents; -// -// //visualization -// private JFrame window = null; -// private MarioRender render = null; -// private MarioAgent agent = null; -// private MarioWorld world = null; -// -// /** -// * Create a mario game to be played -// */ -// public MarioGame() { -// -// } -// -// /** -// * Create a mario game with a different forward model where the player on certain event -// * -// * @param killEvents events that will kill the player -// */ -// public MarioGame(MarioEvent[] killEvents) { -// this.killEvents = killEvents; -// } -// -// private int getDelay(int fps) { -// if (fps <= 0) { -// return 0; -// } -// return 1000 / fps; -// } -// -// private void setAgent(MarioAgent agent) { -// this.agent = agent; -// if (agent instanceof KeyAdapter) { -// this.render.addKeyListener((KeyAdapter) this.agent); -// } -// } -// -// /** -// * Play a certain mario level -// * -// * @param level a string that constitutes the mario level, it uses the same representation as the VGLC but with more details. for more details about each symbol check the json file in the levels folder. -// * @param timer number of ticks for that level to be played. Setting timer to anything <=0 will make the time infinite -// * @return statistics about the current game -// */ -// public byte[] playGame(String level, int timer) { -// return this.runGame(new HumanAgent(), level, timer, 0, true, 30, 2); -// } -// -// /** -// * Run a certain mario level with a certain agent -// * -// * @param agent the current AI agent used to play the game -// * @param level a string that constitutes the mario level, it uses the same representation as the VGLC but with more details. for more details about each symbol check the json file in the levels folder. -// * @param timer number of ticks for that level to be played. Setting timer to anything <=0 will make the time infinite -// * @return statistics about the current game -// */ -// public byte[] runGame(MarioAgent agent, String level, int timer) { -// return this.runGame(agent, level, timer, 0, true, 30, 2); -// } -// -// /** -// * Run a certain mario level with a certain agent -// * -// * @param agent the current AI agent used to play the game -// * @param level a string that constitutes the mario level, it uses the same representation as the VGLC but with more details. for more details about each symbol check the json file in the levels folder. -// * @param timer number of ticks for that level to be played. Setting timer to anything <=0 will make the time infinite -// * @param marioState the initial state that mario appears in. 0 small mario, 1 large mario, and 2 fire mario. -// * @param visuals show the game visuals if it is true and false otherwise -// * @param fps the number of frames per second that the update function is following -// * @param scale the screen scale, that scale value is multiplied by the actual width and height -// * @return statistics about the current game -// */ -// public byte[] runGame(MarioAgent agent, String level, int timer, int marioState, boolean visuals, int fps, float scale) { -// if (visuals) { -// this.window = new JFrame("Mario AI Framework"); -// this.render = new MarioRender(scale); -// this.window.setContentPane(this.render); -// this.window.pack(); -// this.window.setResizable(false); -// this.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); -// this.render.init(); -// this.window.setVisible(true); -// } -// this.setAgent(agent); -// return this.gameLoop(level, timer, marioState, visuals, fps); -// } -// -// private byte[] gameLoop(String level, int timer, int marioState, boolean visual, int fps) { -// this.world = new MarioWorld(this.killEvents); -// this.world.visuals = visual; -// this.world.initializeLevel(level, 1000 * timer); -// if (visual) { -// this.world.initializeVisuals(this.render.getGraphicsConfiguration()); -// } -// this.world.mario.isLarge = marioState > 0; -// this.world.mario.isFire = marioState > 1; -// this.world.update(new boolean[MarioActions.numberOfActions()]); -// long currentTime = System.currentTimeMillis(); -// -// //initialize graphics -// Image renderTarget = null; -// Graphics backBuffer = null; -// Graphics currentBuffer = null; -// if (visual) { -// renderTarget = this.render.createImage(MarioGame.width, MarioGame.height); -// backBuffer = this.render.getGraphics(); -// currentBuffer = renderTarget.getGraphics(); -// this.render.addFocusListener(this.render); -// } -// -// MarioTimer agentTimer = new MarioTimer(MarioGame.maxTime); -// this.agent.initialize(new MarioForwardModel(this.world.clone()), agentTimer); -// -// ArrayList gameEvents = new ArrayList<>(); -// ArrayList agentEvents = new ArrayList<>(); -// while (this.world.gameStatus == GameStatus.RUNNING) { -// if (!this.pause) { -// //get actions -// agentTimer = new MarioTimer(MarioGame.maxTime); -// boolean[] actions = this.agent.getActions(new MarioForwardModel(this.world.clone()), agentTimer); -// if (MarioGame.verbose) { -// if (agentTimer.getRemainingTime() < 0 && Math.abs(agentTimer.getRemainingTime()) > MarioGame.graceTime) { -// System.out.println("The Agent is slowing down the game by: " -// + Math.abs(agentTimer.getRemainingTime()) + " msec."); -// } -// } -// // update world -// this.world.update(actions); -// gameEvents.addAll(this.world.lastFrameEvents); -// agentEvents.add(new MarioAgentEvent(actions, this.world.mario.x, -// this.world.mario.y, (this.world.mario.isLarge ? 1 : 0) + (this.world.mario.isFire ? 1 : 0), -// this.world.mario.onGround, this.world.currentTick)); -// } -// -// //render world -// if (visual) { -// this.render.renderWorld(this.world, renderTarget, backBuffer, currentBuffer); -// } -// //check if delay needed -// if (this.getDelay(fps) > 0) { -// try { -// currentTime += this.getDelay(fps); -// Thread.sleep(Math.max(0, currentTime - System.currentTimeMillis())); -// } catch (InterruptedException e) { -// break; -// } -// } -// } -// return Replay.serializeAgentEvents(agentEvents); -// } -//}