Update Replay

This commit is contained in:
bigJIU 2023-02-16 12:15:38 +08:00
parent 50644250c6
commit 2bc77cfe4f
11 changed files with 148 additions and 53 deletions

28
.idea/workspace.xml generated
View File

@ -5,13 +5,14 @@
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Mario-AI-Interface.jar" beforeDir="false" afterPath="$PROJECT_DIR$/Mario-AI-Interface.jar" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Mario-AI-Interface.jar.js" beforeDir="false" afterPath="$PROJECT_DIR$/Mario-AI-Interface.jar.js" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Mario-AI-Interface/reps/f_l_sav.rep" beforeDir="false" afterPath="$PROJECT_DIR$/Mario-AI-Interface/reps/f_l_sav.rep" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Mario-AI-Interface/src/Play.java" beforeDir="false" afterPath="$PROJECT_DIR$/Mario-AI-Interface/src/Play.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Mario-AI-Interface/src/agents/ReplayAgent.java" beforeDir="false" afterPath="$PROJECT_DIR$/Mario-AI-Interface/src/agents/ReplayAgent.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Mario-AI-Interface/src/engine/core/MarioForwardModel.java" beforeDir="false" afterPath="$PROJECT_DIR$/Mario-AI-Interface/src/engine/core/MarioForwardModel.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Mario-AI-Interface/src/engine/core/MarioGame.java" beforeDir="false" afterPath="$PROJECT_DIR$/Mario-AI-Interface/src/engine/core/MarioGame.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/data/questionare.csv" beforeDir="false" afterPath="$PROJECT_DIR$/data/questionare.csv" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/templates/GameAnnotation.html" beforeDir="false" afterPath="$PROJECT_DIR$/templates/GameAnnotation.html" afterDir="false" />
<change beforePath="$PROJECT_DIR$/templates/GameAnnotation2.html" beforeDir="false" afterPath="$PROJECT_DIR$/templates/GameAnnotation2.html" afterDir="false" />
<change beforePath="$PROJECT_DIR$/templates/GameOver.html" beforeDir="false" afterPath="$PROJECT_DIR$/templates/GameOver.html" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -47,16 +48,17 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
&quot;WebServerToolWindowFactoryState&quot;: &quot;false&quot;,
&quot;last_opened_file_path&quot;: &quot;C:/Users/duaho/Projects/website materials/MarioWeb/templates&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"WebServerToolWindowFactoryState": "false",
"last_opened_file_path": "C:/Users/duaho/Documents/Python Projects/evogym",
"nodejs_package_manager_path": "npm",
"settings.editor.selected.configurable": "application.passwordSafe"
}
}</component>
}]]></component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="C:\Users\duaho\Projects\website materials\MarioWeb\templates" />
@ -209,6 +211,8 @@
<workItem from="1671001937679" duration="1407000" />
<workItem from="1671278879840" duration="3791000" />
<workItem from="1675757773553" duration="7165000" />
<workItem from="1675829047440" duration="2956000" />
<workItem from="1676377283551" duration="34000" />
</task>
<servers />
</component>
@ -229,7 +233,7 @@
<component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/MarioWeb$MarioWeb.coverage" NAME="MarioWeb Coverage Results" MODIFIED="1669305478201" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
<SUITE FILE_PATH="coverage/MarioWeb$test.coverage" NAME="test Coverage Results" MODIFIED="1668079935841" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
<SUITE FILE_PATH="coverage/MarioWeb$Flask__main_py_.coverage" NAME="Flask (main.py) Coverage Results" MODIFIED="1675781448695" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
<SUITE FILE_PATH="coverage/MarioWeb$Flask__main_py_.coverage" NAME="Flask (main.py) Coverage Results" MODIFIED="1675839259180" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
<SUITE FILE_PATH="coverage/MarioWeb$Flask__main_py___1_.coverage" NAME="Flask (main.py) (1) Coverage Results" MODIFIED="1668079945407" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="" />
</component>
</project>

Binary file not shown.

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -28,11 +28,11 @@ public class Play {
// 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
/* game.setLives(10);
String levelPath = "./levels/group0/f_l.txt"; // For local
String repPath = "./reps/f_l_sav.rep"; */ // For local
// MarioResult r2 = game.playGame(getLevel(levelPath), repPath);
// MarioResult tmpResult = game.playGame(Replay.getRepAgentFromFile(repPath),getLevel(levelPath), 200, repPath);
//game.playGame(Replay.getRepAgentFromFile(repPath),getLevel(levelPath), 30, repPath,20);
// MarioGame game2 = new MarioGame();
//FIXME: Debug Use
@ -58,7 +58,7 @@ public class Play {
return playGameMain(levelName, 5, false,30,16);
}
public static void replayGameMain(String levelName, int lives, int time, int col){
public static MarioResult replayGameMain(String levelName, int lives, int time, int col){
String levelPath = String.format("/app/levels/%s.lvl", levelName); // For web
String repPath = String.format("/files/%s_sav.rep", levelName); // For web
@ -67,7 +67,7 @@ public class Play {
game = new MarioGame();
game.setLives(lives);
game.playGame(Replay.getRepAgentFromFile(repPath),getLevel(levelPath), time, repPath,col);
return game.playGame(Replay.getRepAgentFromFile(repPath),getLevel(levelPath), time, repPath,col);
}
public static void stopReplay(){
game.stopGame();

View File

@ -3,17 +3,25 @@ package agents;
import engine.core.MarioAgent;
import engine.core.MarioForwardModel;
import engine.core.MarioTimer;
import engine.helper.GameStatus;
public class ReplayAgent implements MarioAgent {
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class ReplayAgent extends KeyAdapter implements MarioAgent {
private int p;
private final boolean[][] actions;
private boolean isPressed;
private boolean[][] actions;
private MarioForwardModel marioForwardModel;
@Override
public void initialize(MarioForwardModel model, MarioTimer timer) {
marioForwardModel = model;
}
public ReplayAgent(boolean[][] actions) {
this.actions = actions;
isPressed = false;
this.p = 0;
}
@ -23,13 +31,43 @@ public class ReplayAgent implements MarioAgent {
@Override
public boolean[] getActions(MarioForwardModel model, MarioTimer timer) {
if(!isPressed){
if (p >= actions.length)
return new boolean[5];
return this.actions[p++];
}else{
System.out.println("return true");
return new boolean[]{true, true, true, true, true};
}
}
@Override
public String getAgentName() {
return "ReplayAgent";
}
@Override
public void keyPressed(KeyEvent e) {
toggleKey(e.getKeyCode(), true);
}
@Override
public void keyReleased(KeyEvent e) {
toggleKey(e.getKeyCode(), false);
}
private void toggleKey(int keyCode, boolean isPressed) {
if(keyCode == KeyEvent.VK_Q){
if(isPressed){
this.isPressed = true;
System.out.println("Pressed");
}else{
this.isPressed = false;
}
}
}
}

View File

@ -205,7 +205,7 @@ public class MarioForwardModel {
*/
public final int obsGridHeight = MarioGame.tileHeight;
private MarioWorld world;
public MarioWorld world;
// stats
private int fallKill;
@ -232,7 +232,7 @@ public class MarioForwardModel {
* @return a clone from the current forward model state
*/
public MarioForwardModel clone() {
MarioForwardModel model = new MarioForwardModel(this.world.clone());
MarioForwardModel model = new MarioForwardModel(this.world);
model.fallKill = this.fallKill;
model.stompKill = this.stompKill;
model.fireKill = this.fireKill;

View File

@ -1,5 +1,6 @@
package engine.core;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.KeyAdapter;
@ -11,7 +12,7 @@ import engine.helper.GameStatus;
import engine.helper.MarioActions;
import engine.helper.Replay;
public class MarioGame {
public class MarioGame extends KeyAdapter{
/**
* the maximum time that agent takes for each step
*/
@ -170,7 +171,7 @@ public class MarioGame {
ArrayList<MarioEvent> gameEvents = new ArrayList<>();
ArrayList<MarioAgentEvent> agentEvents = new ArrayList<>();
boolean midBreak = false;
int segNum = 0;
while (this.world.gameStatus == GameStatus.RUNNING) {
if (!this.pause) {
@ -183,13 +184,20 @@ public class MarioGame {
agentTimer = new MarioTimer(MarioGame.maxTime);
//get actions
boolean[] actions = this.agent.getActions(new MarioForwardModel(this.world.clone()), agentTimer);
boolean[] actions = this.agent.getActions(new MarioForwardModel(this.world), 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.");
}
}
// mid break
if(actions[1]&&actions[2]){
this.world.gameStatus = GameStatus.LOSE;
midBreak = true;
break;
}
// update world
this.world.update(actions);
gameEvents.addAll(this.world.lastFrameEvents);
@ -213,9 +221,8 @@ public class MarioGame {
}
}
MarioResult res = new MarioResult(this.world, gameEvents, agentEvents);
if (!resultPath.isEmpty()) {
if (!resultPath.isEmpty()&&!midBreak) {
Replay.saveReplay(resultPath, res.getAgentEvents());
//showNewWindow(this.window);
}
return new MarioResult(this.world, gameEvents, agentEvents);
}
@ -223,10 +230,32 @@ public class MarioGame {
public void stopGame(){
this.world.gameStatus = GameStatus.LOSE;
}
@Override
public void keyPressed(KeyEvent e) {
toggleKey(e.getKeyCode(), true);
}
@Override
public void keyReleased(KeyEvent e) {
toggleKey(e.getKeyCode(), false);
}
private void toggleKey(int keyCode, boolean isPressed) {
if(keyCode == KeyEvent.VK_Q){
if(isPressed){
stopGame();
System.out.println("Pressed mg");
}
}
}
public void setLives(int lives) {
this.initialLives = lives;
}
}

View File

@ -166,3 +166,8 @@ IP,Style,Frequency,Age,Gender
127.0.0.1,B,A,A,A,A,A,,
127.0.0.1,A,A,A,A,A,A,,
127.0.0.1,A,A,A,A,A,A,,
127.0.0.1,A,A,A,A,A,A,,
127.0.0.1,A,A,A,A,A,A,,
127.0.0.1,A,A,A,A,A,A,,
127.0.0.1,A,A,A,A,A,A,,
127.0.0.1,A,A,A,A,A,A,,

1 IP,Style,Frequency,Age,Gender
166 127.0.0.1,B,A,A,A,A,A,,
167 127.0.0.1,A,A,A,A,A,A,,
168 127.0.0.1,A,A,A,A,A,A,,
169 127.0.0.1,A,A,A,A,A,A,,
170 127.0.0.1,A,A,A,A,A,A,,
171 127.0.0.1,A,A,A,A,A,A,,
172 127.0.0.1,A,A,A,A,A,A,,
173 127.0.0.1,A,A,A,A,A,A,,

View File

@ -37,7 +37,7 @@
.popwindow {
width:500px;
height:550px;
height:580px;
border:3px solid #ff928e;
visibility: hidden;
position: absolute;
@ -168,7 +168,8 @@
<div id="replayWindow" class="popwindow">
<div id="replayWindowGame">
</div>
<div align="center"><button onClick='closeReplayWindow()'>Close Window</button></div>
<p>Click "Q" to stop playing and quit</p>
<div align="center" id = "replayButton"><button onClick='closeReplayWindow()'>Close Window</button></div>
</div>
@ -206,14 +207,19 @@ function Replay(id){
if(id==1){
console.log("{{level1}}")
var replayWindow=document.getElementById("replayWindow");
var replayButton=document.getElementById("replayButton");
replayWindow.style.visibility = 'visible';
cjCall("Play", "replayGameMain", "{{level1}}",5,30,16);
var result = cjCall("Play", "replayGameMain", "{{level1}}",5,30,16);
result.then(function(){replayButton.style.visibility = 'visible'});
}else if(id==2){
console.log("{{level2}}")
var replayWindow=document.getElementById("replayWindow");
var replayButton=document.getElementById("replayButton");
replayWindow.style.visibility = 'visible';
gameWindow = document.createElement("div")
cjCall("Play", "replayGameMain", "{{level2}}",5,30,16);
var result = cjCall("Play", "replayGameMain", "{{level2}}",5,30,16);
result.then(function(){replayButton.style.visibility = 'visible'});
}else{
}
@ -224,8 +230,10 @@ function Replay(id){
<script>
function closeReplayWindow(){
var replayWindow=document.getElementById("replayWindow");
var replayButton=document.getElementById("replayButton");
cjCall("Play", "stopReplay");
replayWindow.style.visibility = 'hidden';
replayButton.style.visibility = 'hidden';
}
window.onload=function(){
closeReplayWindow();

View File

@ -116,7 +116,8 @@
<div id="replayWindow" class="popwindow">
<div id="replayWindowGame">
</div>
<div align="center"><button onClick="closeReplayWindow()">Close Window</button></div>
<p>Click "Q" to stop playing and quit</p>
<div align="center" id = "replayButton"><button onClick='closeReplayWindow()'>Close Window</button></div>
</div>
@ -153,18 +154,25 @@ function Replay(id){
if(id==1){
console.log("{{level1}}")
var replayWindow=document.getElementById("replayWindow");
var replayButton=document.getElementById("replayButton");
replayWindow.style.visibility = 'visible';
cjCall("Play", "replayGameMain", "{{level1}}",5,30,16);
var result = cjCall("Play", "replayGameMain", "{{level1}}",5,30,16);
result.then(function(){replayButton.style.visibility = 'visible'});
}else if(id==2){
console.log("{{level2}}")
var replayWindow=document.getElementById("replayWindow");
var replayButton=document.getElementById("replayButton");
replayWindow.style.visibility = 'visible';
cjCall("Play", "replayGameMain", "{{level2}}",5,30,16);
var result = cjCall("Play", "replayGameMain", "{{level2}}",5,30,16);
result.then(function(){replayButton.style.visibility = 'visible'});
}else if(id==3){
console.log("{{level2}}")
var replayWindow=document.getElementById("replayWindow");
var replayButton=document.getElementById("replayButton");
replayWindow.style.visibility = 'visible';
cjCall("Play", "replayGameMain", "{{level3}}",5,30,16);
var result = cjCall("Play", "replayGameMain", "{{level3}}",5,30,16);
result.then(function(){replayButton.style.visibility = 'visible'});
}else{
console.log("No such ID")
}
@ -175,7 +183,10 @@ function Replay(id){
<script>
function closeReplayWindow(){
var replayWindow=document.getElementById("replayWindow");
var replayButton=document.getElementById("replayButton");
cjCall("Play", "stopReplay");
replayWindow.style.visibility = 'hidden';
replayButton.style.visibility = 'hidden';
}
window.onload=function(){
closeReplayWindow();