2022-10-20 12:08:31 +00:00
|
|
|
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;
|
2022-10-20 12:08:31 +00:00
|
|
|
import engine.core.MarioAgentEvent;
|
|
|
|
import engine.core.MarioGame;
|
|
|
|
import engine.core.MarioResult;
|
2022-10-21 09:46:57 +00:00
|
|
|
import engine.helper.Replay;
|
2022-10-20 12:08:31 +00:00
|
|
|
|
|
|
|
public class Play {
|
2023-02-07 19:38:06 +00:00
|
|
|
private static MarioGame game;
|
2022-10-20 12:08:31 +00:00
|
|
|
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 {
|
2023-02-25 07:27:43 +00:00
|
|
|
|
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();
|
2023-02-25 07:27:43 +00:00
|
|
|
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);
|
2023-02-25 07:27:43 +00:00
|
|
|
return true;
|
2022-10-20 12:08:31 +00:00
|
|
|
}
|
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);
|
2023-04-26 07:51:26 +00:00
|
|
|
String levelPath = "./levels/lvl155.lvl"; // For local
|
|
|
|
String repPath = "./reps/b179cbd3-ee8e-48de-baf8-da6438461d1d_lvl155.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-10-20 12:08:31 +00:00
|
|
|
|
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
|
2023-02-25 07:27:43 +00:00
|
|
|
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
|
2023-02-25 07:27:43 +00:00
|
|
|
|
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);
|
2022-10-20 12:08:31 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
2022-10-20 12:08:31 +00:00
|
|
|
}
|