MarioWeb/Mario-AI-Interface/src/Play.java

104 lines
4.3 KiB
Java
Raw Normal View History

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
2022-10-21 09:46:57 +00:00
import agents.HumanAgent;
import agents.ReplayAgent;
2023-04-10 08:18:13 +00:00
import com.alibaba.fastjson.JSON;
import engine.core.MarioAgentEvent;
import engine.core.MarioGame;
import engine.core.MarioResult;
2022-10-21 09:46:57 +00:00
import engine.helper.Replay;
public class Play {
2023-02-07 19:38:06 +00:00
private static MarioGame game;
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 {
2022-12-14 12:07:02 +00:00
//FIXME: Debug Use
2023-04-10 12:33:36 +00:00
game = new MarioGame();
2023-04-10 08:18:13 +00:00
//System.out.println(playJavaGame());
2023-04-10 12:33:36 +00:00
//replayGameMain("dfd40950-75e7-44fe-8833-371d32e525af_lvl144",10,20,5);
playJavaGame();
System.out.println("Java: Play Java Main Function Done");
}
public static boolean initialGame(){
game = new MarioGame();
2023-03-07 12:07:40 +00:00
String levelName = "t1";
String levelPath = String.format("/app/levels/%s.lvl", levelName); // For web
2023-03-07 12:55:12 +00:00
String repPath = String.format("/files/%s_sav.rep", levelName); // For web
game.playGame(new HumanAgent(true),getLevel(levelPath),0,repPath,10);
return true;
}
2022-10-21 09:46:57 +00:00
2023-04-10 08:18:13 +00:00
public static String playJavaGame(){
2023-03-07 11:33:30 +00:00
2023-04-10 12:33:36 +00:00
game.setLives(5);
String levelPath = "./levels/group0/lvl73.lvl"; // For local
String repPath = "./reps/dfd40950-75e7-44fe-8833-371d32e525af_lvl73.rep"; // For local
2023-03-01 13:05:56 +00:00
//MarioGame.verbose = true;
//Play Game
2023-04-10 12:33:36 +00:00
//MarioResult tmpResult = game.playGame(new HumanAgent(false),getLevel(levelPath), 10, repPath,30);
2023-03-01 13:05:56 +00:00
//Replay
2023-04-10 12:33:36 +00:00
MarioResult tmpResult = game.playGame(Replay.getRepAgentFromFile(repPath),getLevel(levelPath), 60, repPath,30);
2023-04-10 08:18:13 +00:00
//return Replay.serializeAgentEvents(tmpResult.getAgentEvents());
String jsonString = Replay.serializeGameResult(tmpResult);
2023-03-01 13:05:56 +00:00
2023-04-10 08:18:13 +00:00
return jsonString;//Replay.serializeAgentEvents(tmpResult.getAgentEvents());
2023-03-01 13:05:56 +00:00
}
2023-04-10 08:18:13 +00:00
public static MarioResult playGameMain(String levelName, int lives, boolean control,int time,int col){
2022-12-14 12:07:02 +00:00
String levelPath = String.format("/app/levels/%s.lvl", levelName); // For web
2022-10-21 09:46:57 +00:00
String repPath = String.format("/files/%s_sav.rep", levelName); // For web
2022-12-14 12:07:02 +00:00
// String levelPath = String.format("./levels/group%s/%s.lvl", "0", levelName); // For local
// String repPath = String.format("./reps/%s_sav.rep", levelName); // For local
game.setLives(lives);
2022-12-14 12:07:02 +00:00
MarioResult tmpResult = game.playGame(new HumanAgent(control),getLevel(levelPath), time, repPath,col);
2022-10-21 09:46:57 +00:00
2023-04-10 08:18:13 +00:00
//return Replay.serializeAgentEvents(tmpResult.getAgentEvents());
//return Replay.serializeGameResult(tmpResult);
return tmpResult;
2022-10-21 09:46:57 +00:00
}
2023-04-10 08:18:13 +00:00
public static MarioResult playGameMain(String levelName){
2022-12-14 12:07:02 +00:00
return playGameMain(levelName, 5, false,30,16);
2022-10-30 13:56:48 +00:00
}
2023-02-16 04:15:38 +00:00
public static MarioResult replayGameMain(String levelName, int lives, int time, int col){
2022-10-24 11:48:43 +00:00
String levelPath = String.format("/app/levels/%s.lvl", levelName); // For web
2022-10-21 09:46:57 +00:00
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
2022-11-03 15:26:25 +00:00
game.setLives(lives);
2023-02-16 04:15:38 +00:00
return game.playGame(Replay.getRepAgentFromFile(repPath),getLevel(levelPath), time, repPath,col);
}
2023-02-07 19:38:06 +00:00
public static void stopReplay(){
game.stopGame();
}
2023-04-10 08:18:13 +00:00
public static byte[] serializeActionFromJson(String jsonString){
MarioResult marioResult= JSON.parseObject(jsonString,MarioResult.class);
ArrayList<MarioAgentEvent> events = marioResult.getAgentEvents();
byte[] content = new byte[events.size()];
for (int i = 0; i < events.size(); i++) {
boolean[] action = events.get(i).getActions();
content[i] = Replay.serializeAction(action);
}
return content;
}
}