Skip to content

Commit

Permalink
Mario level viewer can also take 2D array
Browse files Browse the repository at this point in the history
And generate a larger level stitched together
  • Loading branch information
schrum2 committed May 22, 2018
1 parent 60c00fc commit 36fa7d5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
Binary file modified LevelClipped.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified LevelFull.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -57,7 +57,7 @@ to generate level images by sending several randomly generated latent vectors to
If you want to see what level is generated for a specific vector of your choice, then you can use
viewer.MarioLevelViewer, which takes a json array of length 32 as a command line parameter that represents
a latent vector for the GAN. If you want to actually play these levels, then you should launch
viewer.MarioLevelPlayer in the same way. Additionally, if MarioLevelPlayer is sent a 2D json array,
viewer.MarioLevelPlayer in the same way. Additionally, if MarioLevelViewer or MarioLevelPlayer are sent a 2D json array,
then each sub-array is interpreted as a separate latent vector for the GAN, and the segments are
stitched together into a larger level.

Expand Down
48 changes: 45 additions & 3 deletions marioaiDagstuhl/src/viewer/MarioLevelViewer.java
Expand Up @@ -6,6 +6,8 @@
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

Expand All @@ -17,6 +19,7 @@
import ch.idsia.tools.CmdLineOptions;
import ch.idsia.tools.EvaluationOptions;
import cmatest.MarioEvalFunction;
import reader.JsonReader;

/**
* This file allows you to generate a level image for any latent vector
Expand Down Expand Up @@ -80,9 +83,48 @@ public static void main(String[] args) throws IOException {
builder.append(str);
}
strLatentVector = builder.toString();
Settings.printInfoMsg("Passed vector: " + strLatentVector);
double[] latentVector = JsonToDoubleArray(strLatentVector);
level = eval.levelFromLatentVector(latentVector);
Settings.printInfoMsg("Passed vector(s): " + strLatentVector);
// If the input starts with two square brackets, then it must be an array of arrays,
// and hence a series of several latent vectors rather than just one. In this case,
// patch all of the levels together into one long level.
if(strLatentVector.subSequence(0, 2).equals("[[")) {
// remove opening/closing brackets
strLatentVector = strLatentVector.substring(1,strLatentVector.length()-1);
String levels = "";
while(strLatentVector.length() > 0) {
int end = strLatentVector.indexOf("]")+1;
String oneVector = strLatentVector.substring(0,end);
System.out.println("ONE VECTOR: " + oneVector);
levels += eval.stringToFromGAN(oneVector); // Use the GAN
strLatentVector = strLatentVector.substring(end); // discard processed vector
if(strLatentVector.length() > 0) {
levels += ",";
strLatentVector = strLatentVector.substring(1); // discard leading comma
}
}
levels = "["+levels+"]"; // Put back in brackets
System.out.println(levels);
List<List<List<Integer>>> allLevels = JsonReader.JsonToInt(levels);
// This list contains several separate levels. The following code
// merges the levels by appending adjacent rows
ArrayList<List<Integer>> oneLevel = new ArrayList<List<Integer>>();
// Create the appropriate number of rows in the array
for(List<Integer> row : allLevels.get(0)) { // Look at first level (assume all are same size)
oneLevel.add(new ArrayList<Integer>()); // Empty row
}
// Now fill up the rows, one level at a time
for(List<List<Integer>> aLevel : allLevels) {
int index = 0;
for(List<Integer> row : aLevel) { // Loot at each row
oneLevel.get(index++).addAll(row);
}
}
// Now create the Mario level from the combined list representation
level = LevelParser.createLevelJson(oneLevel);
} else { // Otherwise, there must be a single latent vector, and thus a single level
double[] latentVector = JsonToDoubleArray(strLatentVector);
level = eval.levelFromLatentVector(latentVector);
}
} else {
System.out.println("Generating level with default vector");
level = eval.levelFromLatentVector(new double[] {0.9881835842209917, -0.9986077315374948, 0.9995512051242508, 0.9998643432807639, -0.9976165917284504, -0.9995247114230822, -0.9997001909358728, 0.9995694511739592, -0.9431036754879115, 0.9998155541290887, 0.9997863689962382, -0.8761392912669269, -0.999843833016589, 0.9993230720045649, 0.9995470247917402, -0.9998847606084427, -0.9998322053148382, 0.9997707200294411, -0.9998905141832997, -0.9999512510490688, -0.9533512808031753, 0.9997703088007039, -0.9992229823819915, 0.9953917828622341, 0.9973473366437476, 0.9943030781608361, 0.9995290290713732, -0.9994945079679955, 0.9997109900652238, -0.9988379572928884, 0.9995070647543864, 0.9994132207570211});
Expand Down

0 comments on commit 36fa7d5

Please sign in to comment.