Warning: session_start(): open(/var/lib/php/sessions/sess_eirmtrpmqrtnp95f61fh5l2i6c, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
///////////////////////////
// Your API to work with //
///////////////////////////
sinterface GameForAI {
double submit(Submission data); // returns score (0-100)
}
abstract sclass AI {
// stuff you get
GameForAI game;
RGBImage image;
bool visualize = true;
RGBImage image() { ret image; }
int w() { ret image.w(); }
int h() { ret image.h(); }
double submit(Submission data) { ret game.submit(data); }
// implement this method and call submit()
abstract void go();
void done() {}
}
///////////////
// Main Game //
///////////////
static int rounds = 1000;
static double targetScore = 99.0; // stop when this score is reached
static int fps = 50;
static int w, h;
static new UserGame userGame;
static int halfTime;
static ImageSurface is, isUser, isRepro;
static RGBImage img;
static new HashMap words;
static L solution;
volatile sbool aiMode;
static JLabel lblScore, lblTiming, lblInstruction;
static new HashMap ais;
static S winner;
svoid pGame {
substance();
swing {
thread { loadBinarySnippet(#1004373); } // preload applause animation
lblInstruction = jcenteredBoldLabel("Reproduce this image:");
nextImage();
addToWindowTop(is, withMargin(lblInstruction));
addToWindow(is, withMargin(lblScore = jcenteredBoldLabel("Your score: 0%")));
for (final Class c : myNonAbstractClassesImplementing(AI)) {
final S name = shortClassName(c);
final JButton btn = jbutton(name);
onClick(btn, r {
if (aiMode) ret;
btn.setText(name + " Running...");
thread {
Game game = testAI(c, voidfunc(Game game) {
S text = name + " scored " + formatDouble(game.score, 1) + "%";
if (game.error != null)
text += " - ERROR: " + game.error;
setText_noWait(btn, text);
});
}
});
addToWindow(is, withMargin(btn));
}
addToWindow(is, lblTiming = jCenteredLabel(;
titlePopupMenuItem(is, "Show Winner Code", f showWinnerCode);
titlePopupMenuItem(is, "Restart AIs", r { clearMapWithCleanUp(ais); });
addToWindow(is, withMargin(jbutton("Restart AIs", r { clearMapWithCleanUp(ais); })));
packFrame(is);
setFrameWidth(is, 400);
centerTopFrame(is);
hideConsole();
}
}
svoid nextImage {
main.img = makeImage();
if (aiMode) ret; // occasional updates only when AI is running
showTheImage();
}
svoid showTheImage {
bool first = is == null;
is = showZoomedImage_centered(is, img, 1);
if (first) {
w = img.w();
h = img.h();
disableImageSurfaceSelector(is);
addToWindow(is, withTitle("Your Reproduction:", jscroll_centered(isUser = new ImageSurface(newBufferedImage(w, h, Color.white)))));
addToWindow(is, makeTheForm(userGame));
// animate if idle
/*awtEvery(is, 1000, r {
if (!aiMode && isInForeground(is) && !mouseInFrame(is))
nextImage();
});*/
// show current image occasionally when AI is running
/*awtEvery(is, 1000/fps, r {
if (aiMode) showTheImage();
});*/
}
}
// AI stuff
sclass Game implements GameForAI {
int step;
new Best best;
RGBImage bestImage;
double score;
Submission submitted; // what was submitted in this round
Throwable error;
public double submit(Submission data) {
if (data == null) ret 0;
if (submitted != null) fail("No multi-submit please");
submitted = data;
RGBImage image = renderImage(data);
double score = scoreImage(image);
if (best.put(data, score)) {
bestImage = image;
this.score = best.score;
}
ret score;
}
}
Game > UserGame {
public double submit(Submission data) {
submitted = null;
double score = super.submit(data);
RGBImage image = renderImage(data);
isUser.setImage(image);
lblScore.setText("Your score: " + formatDouble(score, 1) + "%");
ret score;
}
}
static Game scoreAI(final AI ai, O onScore) {
aiMode = true;
final long start = sysNow();
try {
final new Game game;
setOpt(ai, +game);
halfTime = rounds/2;
game.step = 0;
while (game.step < rounds) {
++game.step;
game.submitted = null;
double score = game.score;
RGBImage image = img;
try {
setOpt(ai, +image);
ai.go();
} catch e {
if (game.error == null) { // print first error to console
showConsole();
printStackTrace(e);
}
game.error = e;
} finally {
setOpt(ai, image := null);
}
if (game.score > score) {
bool first = isRepro == null;
isRepro = showImage(isRepro, "Best Reproduction", game.bestImage);
if (first)
moveToTopRightCorner(isRepro);
}
pcallF(onScore, game);
//if (alwaysNewImage) nextImage();
}
ai.done();
// TODO: winner saving
/*if (game.points-game.halfTimeScore >= rounds-halfTime) thread {
titleStatus(is, "Solved!");
showAnimationInTopLeftCorner(#1004373, game.points + " of " + rounds + " points!!", 3.0);
winner = structure(ai);
save("winner");
}*/
ret game;
} finally {
aiMode = false;
awt {
lblTiming.setText(formatDouble(toSeconds(sysNow()-start), 1) + " s");
//nextImage();
}
}
}
static AI getAI(Class extends AI> c) {
AI ai = ais.get(c);
if (ai == null) ais.put(c, ai = nu(c));
ret ai;
}
// onScore: voidfunc(Game)
static Game testAI(Class extends AI> c, O onScore) {
ret scoreAI(getAI(c), onScore);
}
svoid showWinnerCode {
if (winner == null) load("winner");
if (winner == null) messageBox("No winner yet");
else showWrappedText("Winner Code - " + programTitle(), winner);
}
static double scoreImage(RGBImage image) {
ret 100*(1.0-rgbDistance(image, img));
}