!7 static int boxes = 3; !include #1006931 // AI Game & API !include #1014957 // AI_Racer 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; 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 { randomBoxWithColor(w, h) }, 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); } }