Warning: session_start(): open(/var/lib/php/sessions/sess_6s6lo5evaqo2umf9h48uhjaras, 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
!7
///////////////////////////
// Your API to work with //
///////////////////////////
abstract sclass GameForAI {
abstract RGBImage getImage();
abstract L cheat();
}
abstract sclass AI {
// stuff you get
GameForAI game;
RGBImage image() { ret game.getImage(); }
int w() { ret image().w(); }
int h() { ret image().h(); }
// stuff you implement
abstract Pt nextClick();
}
///////////////
// A test AI //
///////////////
AI > TestAI {
Pt nextClick() {
ret new Pt(random(image().w()), random(image().h()));
}
}
///////////////
// Main Game //
///////////////
static int w = 400, h = 300;
static int border = 10, fontSize = 30;
static int rounds = 1000;
static S font = #1004887;
static int points, clicks;
static ImageSurface is;
static RGBImage img;
static new HashMap words;
static L solution;
volatile sbool aiMode;
static JLabel lblScore;
p-substance {
nextImage();
addToWindow(is, lblScore = jcenteredlabel("Your score: 0 of 0"));
for (final Class c : nonAbstractClassesImplementing(AI)) {
final S name = shortClassName(c);
final JButton btn = jbutton("Run " + name);
onClick(btn, r {
btn.setText(name + ": Testing...");
thread {
long score = testAI(c);
setText(btn, name + ": " + score + " of " + rounds + " points");
}
});
addToWindow(is, btn);
}
packFrameInTopRightCorner(is);
}
svoid nextImage {
RGBImage img = rgbImage(Color.white, w, h);
words.clear();
for i to 5: {
int num = random(100);
S s = str(num);
RGB color = new RGB(random(0.5), random(0.5), random(0.5));
renderText_fg.set(color.getColor());
BufferedImage ti = renderText(font, fontSize, s);
Rect r;
do {
r = randomRect(w, h, border, ti.getWidth(), ti.getHeight());
if (r == null) fail("Image too small: \*ti.getWidth()*/*\*ti.getHeight()*/ > \*w*/*\*h*/");
} while (anyRectOverlaps(keys(words), r) && licensed());
rgbCopy(ti, img, r.x, r.y);
words.put(r, num);
}
solution = keysWithBiggestValue(words);
bool first = is == null;
is = showImage(is, main.img = img);
if (first) {
onLeftClick(is, voidfunc(Pt p) {
++clicks;
if (anyRectContains(solution, p)) {
++points;
nextImage();
}
lblScore.setText(print("Your score: " + points + " of " + clicks));
});
disableImageSurfaceSelector(is);
// animate if idle
awtEvery(is, 1000, r {
if (!aiMode && isInForeground(is) && !mouseInComponent(is))
nextImage();
});
}
}
// AI stuff
sclass Game extends GameForAI {
int points;
RGBImage getImage() { ret img; }
L cheat() { ret solution; }
void click(Pt p) {
if (anyRectContains(solution, p)) {
++points;
nextImage();
}
}
}
static long scoreAI(AI ai, long rounds) {
aiMode = true;
try {
new Game game;
setOpt(ai, +game);
long step = 0;
pcall {
while (step < rounds) {
++step;
Pt p = ai.nextClick();
if (p == null) { print("AI resigned"); break; }
game.click(p);
//print("Step " + step + ", move: " + p + ", points: " + game.points);
}
}
print("AI " + shortClassName(ai) + " points after " + rounds + " rounds: " + game.points);
ret game.points;
} finally {
aiMode = false;
}
}
static long testAI(Class extends AI> c) {
ret scoreAI(nu(c), rounds);
}