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

85 lines
3.4 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;
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-03-28 06:03:23 +00:00
// game = new MarioGame();
2023-03-07 12:07:40 +00:00
// 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-03-01 13:05:56 +00:00
public static byte[] playJavaGame(){
2023-03-07 11:33:30 +00:00
2023-03-03 07:52:11 +00:00
game.setLives(3);
2023-03-01 13:38:39 +00:00
String levelPath = "./levels/group0/lvl1.lvl"; // For local
2023-03-01 13:05:56 +00:00
String repPath = "./reps/f_l_sav.rep"; // For local
//MarioGame.verbose = true;
//Play Game
2023-03-03 07:52:11 +00:00
MarioResult tmpResult = game.playGame(new HumanAgent(false),getLevel(levelPath), 10, repPath,30);
2023-03-01 13:05:56 +00:00
//Replay
//MarioResult tmpResult = game.playGame(Replay.getRepAgentFromFile(repPath),getLevel(levelPath), 30, repPath,30);
return Replay.serializeAgentEvents(tmpResult.getAgentEvents());
}
2022-12-14 12:07:02 +00:00
public static byte[] 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
return Replay.serializeAgentEvents(tmpResult.getAgentEvents());
}
2022-11-09 14:09:00 +00:00
public static byte[] 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();
}
}