MarioWeb/Mario-AI-Interface/src/Play.java
2022-11-09 22:09:00 +08:00

67 lines
2.7 KiB
Java

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import agents.HumanAgent;
import agents.ReplayAgent;
import engine.core.MarioAgentEvent;
import engine.core.MarioGame;
import engine.core.MarioResult;
import engine.helper.Replay;
public class Play {
public static String getLevel(String filepath) {
String content = "";
try {
content = new String(Files.readAllBytes(Paths.get(filepath)));
} catch (IOException e) {
}
return content;
}
public static void main(String[] args) throws IOException {
String groupID = args[0];
String levelName = args[1];
//MarioGame game = new MarioGame();
// String levelPath = String.format("/app/levels/group%s/%s.txt", groupID, levelName); // For web
// String repPath = String.format("/files/tmp.rep"); // For web
// game.setLives(10);
// String levelPath = String.format("./levels/group%s/%s.txt", groupID, levelName); // For local
// String repPath = String.format("./reps/%s_sav.rep", levelName); // For local
// MarioResult r2 = game.playGame(getLevel(levelPath), repPath);
// MarioResult tmpResult = game.playGame(Replay.getRepAgentFromFile(repPath),getLevel(levelPath), 200, repPath);
// MarioGame game2 = new MarioGame();
}
public static byte[] playGameMain(String levelName, int lives, boolean control){
String levelPath = String.format("/app/levels/%s.lvl", levelName); // For web
String repPath = String.format("/files/%s_sav.rep", levelName); // For web
MarioGame game = new MarioGame();
game.setLives(lives);
MarioResult tmpResult = game.playGame(new HumanAgent(control),getLevel(levelPath), 200, repPath);
return Replay.serializeAgentEvents(tmpResult.getAgentEvents());
}
public static byte[] playGameMain(String levelName){
return playGameMain(levelName, 0, true);
}
public static void replayGameMain(String levelName, int lives){
String levelPath = String.format("/app/levels/%s.lvl", levelName); // For web
String repPath = String.format("/files/%s_sav.rep", levelName); // For web
// String levelPath = String.format("/app/levels/group%s/%s.txt", groupID, levelName); // For local
// String repPath = String.format("./files/%s_sav.rep", levelName); // For local
MarioGame game = new MarioGame();
game.setLives(lives);
game.playGame(Replay.getRepAgentFromFile(repPath),getLevel(levelPath), 200, repPath);
}
}