!7 static int boxes = 3; !include #1006931 // AI Game & API p-autorestart { newImageText = "New Letter!"; reproZoom = 1; makeInstruction(); pGame(); swing { setFrameTitle(is, "A. I. Game 5"); final JSpinner spinner = jSpinner(boxes, 1, 100); addToWindowPack_keepWidth(is, withMargin(jRightAligned(withLabel("Number of boxes to use:", spinner)))); onChange(spinner, r { boxes = intFromSpinner(spinner); makeInstruction(); restartAIs(); }); clickButton(last(aiButtons)); // Start the winner AI! } } svoid makeInstruction { setInstruction("Reproduce this image with " + n2(boxes, "box", "boxes") + ":"); } sclass Submission extends BackgroundPlusBoxes { *() {} *(RGB background, BoxWithColor... boxes) { super(background, boxes); check(); } *(RGB background, L boxes) { super(background, boxes); check(); } void check { assertEquals(main.boxes, l(boxes)); } } ////////////////// // PUZZLE MAKER // ////////////////// sS lastLetter; static RGBImage makeImage() { lastLetter = randomUpperCaseLetterNotEqualTo(lastLetter); ret randomLetterImage(lastLetter, #1004568, 100); } /////////////// // RENDERERS // /////////////// static RGBImage renderImage(Submission s) { ret new RGBImage(renderImage1(s)); } static BufferedImage renderImage1(Submission s) { ret renderBackgroundPlusBoxes(w, h, s); } static BufferedImage renderWithHints(Submission s) { BufferedImage bi = renderImage1(s); for (BoxWithColor e : s.boxes) { int x1 = min(e.a.x, e.b.x), y1 = min(e.a.y, e.b.y); int x2 = max(e.a.x, e.b.x), y2 = max(e.a.y, e.b.y); drawRect(bi, x1, y1, x2-x1+1, y2-y1+1, colorWithAlpha(0.5, Color.black)); } ret bi; } ////////////////////////////////////// // Test AIs. Just add your own here // ////////////////////////////////////// AI > AI_Random { new Best best; BoxWithColor randomBox() { ret new BoxWithColor(randomPoint(), randomPoint(), randomColor()); } Pt randomPoint() { ret _randomPoint(image()); } void go { //print("Round: " + round()); if (round() == 1 && best.has()) // TODO: automate this submit(best.get()); else { Submission guess = guess(); updateBest(guess, submit(guess)); } } void updateBest(Submission guess, double score) { best.put(guess, score); } Submission guess() { ret new Submission(randomColor(), produceN(func { randomBox() }, boxes)); } } AI_Random > AI_RandomWithVariation { int n; Submission guess() { if (odd(n++) && best.has()) ret vary(best.get()); else ret super.guess(); } Submission vary(Submission s) { ret varyBackgroundPlusBoxes(s, w, h); } } AI > AI_Racer { AI_RandomWithVariation leader, overtaker; new Best leadersBest; int discardEvery = 5000; int roundsSinceChange; AI_RandomWithVariation newAI() { ret new AI_RandomWithVariation; } void go { if (round() == 1 && leadersBest.has()) // TODO: automate this submit(leadersBest.get()); else if (tossACoin()) { if (leader == null) leader = newAI(); initSubAI(leader); Submission guess = leader.guess(); double score = submit(guess); leader.updateBest(guess, score); leadersBest.put(guess, score); } else { if (overtaker == null) overtaker = newAI(); initSubAI(overtaker); Submission guess = overtaker.guess(); double score = submit(guess); overtaker.updateBest(guess, score); if (score > leadersBest.score()) { // displace leader! print("Overtake at " + formatScore(overtaker.best.score()) + " vs " + formatScore(leadersBest.score())); leader = overtaker; overtaker = null; roundsSinceChange = 0; } else if (roundsSinceChange++ >= discardEvery) { // make new overtaker print("Discarding overtaker at " + formatScore(overtaker.best.score()) + " vs " + formatScore(leadersBest.score())); overtaker = null; roundsSinceChange = 0; } } } }