!7 /////////////////////////// // Your API to work with // /////////////////////////// abstract sclass GameForAI { abstract RGBImage getImage(); abstract L submit(Pt point); // returns correct solution so AI can learn from it } abstract sclass AI { // stuff you get GameForAI game; RGBImage image; int steps; RGBImage image() { ret image; } int w() { ret image.w(); } int h() { ret image.h(); } L submit(Pt p) { ret game.submit(p); } L submit(Rect r) { ret submit(middleOfRect(r)); } L submitNothing() { ret game.submit(null); } Rect leftRect() { ret main.leftRect(image); } Rect rightRect() { ret main.rightRect(image); } L clickLeft() { ret submit(leftRect()); } L clickRight() { ret submit(rightRect()); } L clickLeftIf(bool b) { ret b ? clickLeft() : clickRight(); } L clickRightIf(bool b) { ret b ? clickRight() : clickLeft(); } RGBImage leftImage() { ret image.clip(leftRect()); } RGBImage rightImage() { ret image.clip(rightRect()); } // implement this method and call submit() abstract void go(); void done() {} } static int singleImageWidth(RGBImage img) { ret (img.w()-spacing)/2; } static Rect leftRect(RGBImage img) { int iw = singleImageWidth(img); ret new Rect(0, 0, iw, h); } static Rect rightRect(RGBImage img) { int iw = singleImageWidth(img); ret new Rect(iw+spacing, 0, iw, h); } ////////////////////////////////////// // Test AIs. Just add your own here // ////////////////////////////////////// AI > ClickOnBrighterImage { void go { clickLeftIf(rgbAverageBrightness(leftImage()) > rgbAverageBrightness(rightImage())); } } /////////////// // Main Game // /////////////// static S instruction = "Click on the brighter image"; static int w = 150, h = 150; // size of each image (left/right) static int spacing = 10; // space between the two images static int rounds = 1000; static bool alwaysNewImage = true; // prevents AIs being stuck on an image static int fps = 50; static int points, clicks; static ImageSurface is, isWrong; static long isWrongLast; static RGBImage img; static new HashMap words; static L solution; volatile sbool aiMode; static JLabel lblScore, lblTiming; static new HashMap ais; p-substance { thread { loadBinarySnippet(#1004373); } // preload applause animation nextImage(); addToWindow(is, withMargin(lblScore = jcenteredBoldLabel("Your score: 0 of 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) { if (game.step < 50 || (game.step % 10) == 0 || game.step == rounds) { S text = name + " scored " + game.points + " of " + game.step; if (game.error != null) text += " - ERROR: " + game.error; setText_noWait(btn, text); } }); } }); addToWindow(is, withMargin(btn)); } addToWindow(is, lblTiming = jCenteredLabel(; titlePopupMenuItem(is, "Restart AIs", r { clearMapWithCleanUp(ais); }); addToWindow(is, withMargin(jbutton("Restart AIs", r { clearMapWithCleanUp(ais); }))); packFrame(is); centerTopFrame(is); hideConsole(); } svoid nextImage { RGBImage img = rgbImage(0xeef3e6, w*2+spacing, h); double l = random(10)/10.0, r = random(10)/10.0; Rect lr = leftRect(img), rr = rightRect(img); rgbFill(img, lr, new RGB(l)); rgbFill(img, rr, new RGB(r)); solution = l > r ? ll(lr) : l == r ? ll(lr, rr) : ll(rr); main.img = img; if (aiMode) ret; // occasional updates only when AI is running showTheImage(); } svoid showTheImage { bool first = is == null; is = showZoomedImage_centered(is, img, 1.5); if (first) { setFrameTitle(is, instruction); onLeftClick(is, voidfunc(Pt p) { ++clicks; if (anyRectContains(solution, is.pointFromComponentCoordinates(p))) { ++points; nextImage(); } else if (alwaysNewImage) 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(); }); // show current image occasionally when AI is running awtEvery(is, 1000/fps, r { if (aiMode) showTheImage(); }); } } // AI stuff sclass Game extends GameForAI { int points, step; Pt submitted; Throwable error; RGBImage getImage() { ret img; } L submit(Pt p) { if (submitted != null) fail("No multi-submit please"); submitted = p == null ? new Pt(-9, -9) : p; if (p != null && anyRectContains(solution, p)) { ++points; if (!alwaysNewImage) nextImage(); } ret solution; } } static Game scoreAI(AI ai, O onScore) { aiMode = true; final long start = sysNow(); try { final new Game game; setOpt(ai, +game); while (game.step < rounds) { ++game.step; ++ai.steps; game.submitted = null; int points = game.points; 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 (points == game.points && hasElapsed(isWrongLast, 50)) { isWrongLast = sysNow(); bool first = isWrong == null; isWrong = showImage(isWrong, "Last Blunder", rgbMarkPoint(image, game.submitted, Color.red, 3)); if (first) { setFrameWidth(isWrong, 230); moveToTopRightCorner(isWrong); moveFrameDown(isWrong, 100); } } pcallF(onScore, game); if (alwaysNewImage) nextImage(); } print("AI " + shortClassName(ai) + " points after " + rounds + " rounds: " + game.points); ai.done(); if (game.points >= rounds) thread { titleStatus(is, "Solved!"); showAnimationInTopLeftCorner(#1004373, game.points + " of " + rounds + " points!!", 3.0); } ret game; } finally { aiMode = false; awt { lblTiming.setText(formatDouble(toSeconds(sysNow()-start), 1) + " s"); nextImage(); } } } static AI getAI(Class c) { AI ai = ais.get(c); if (ai == null) ais.put(c, ai = nu(c)); ret ai; } // onScore: voidfunc(Game) static Game testAI(Class c, O onScore) { ret scoreAI(getAI(c), onScore); }