!7 static int lineWidth = /*18*/20; static int lines = 6; sbool drawLines; !include #1006931 // AI Game & API p-autorestart { newImageText = "New Letter!"; makeInstruction(); pGame(); swing { setFrameTitle(is, "A. I. Game 5"); final JSpinner spinner = jSpinner(lines, 1, 100); addToWindowPack(is, withMargin(jRightAligned(withLabel("Number of lines to use:", spinner)))); onChange(spinner, r { lines = intFromSpinner(spinner); makeInstruction(); restartAIs(); }); } } svoid makeInstruction { setInstruction("Reproduce this image with " + n(lines, "straight line") + ":"); } sclass Line { Pt a, b; *() {} *(Pt *a, Pt *b) {} } sclass Submission { new L lines; *() {} *(Line... lines) { this.lines = asList(lines); check(); } *(L *lines) { check(); } void check { assertEquals(main.lines, l(lines)); } } ////////////////// // PUZZLE MAKER // ////////////////// sS lastLetter; static RGBImage makeImage() { //ret loadRGBImage(/*#1006930*/#1006944); S letter; do { letter = "" + randomCharBetween('A', 'Z'); } while (eq(letter, lastLetter)); lastLetter = letter; renderText_withLeading = false; ret new RGBImage(renderText(#1004568, 100, letter)); } /////////////// // RENDERERS // /////////////// static RGBImage renderImage(Submission s) { ret new RGBImage(renderImage1(s)); } static BufferedImage renderImage1(Submission s) { BufferedImage bi = newBufferedImage(w, h, Color.white); for (Line l : s.lines) drawRoundEdgeLine(bi, l.a, l.b, Color.black, lineWidth); ret bi; } static RGBImage renderWithHints(Submission s) { BufferedImage bi = renderImage1(s); if (drawLines) { for (Line l : s.lines) drawRoundEdgeLine(bi, l.a, l.b, Color.lightGray, 1); ret new RGBImage(bi); } else { RGBImage img = new RGBImage(bi); for (Line l : s.lines) rgbMarkPoints(img, Color.lightGray, 1, l.a, l.b); ret rgbUncache(img); } } ////////////////////////////////////// // Test AIs. Just add your own here // ////////////////////////////////////// AI > AI_Random { new Best best; Line randomLine() { ret new Line(randomPoint(), randomPoint()); } Pt randomPoint() { ret main.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(produceN(func { randomLine() }, lines)); } } 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) { s = cloneThroughStructure(s); varyLine(random(s.lines)); ret s; } void varyLine(Line l) { if (tossACoin()) l.a = varyPoint(l.a); else l.b = varyPoint(l.b); } Pt varyPoint(Pt p) { ret tossACoin() ? randomPoint() : varyPoint(p, 10); } Pt varyPoint(Pt p, int range) { range = max(range, 1); ret new Pt( random(p.x-range, p.x+range+1), random(p.y-range, p.y+range+1)); } } 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; } } } }