!7 static int lineWidth = 18; static int lines = 4; !include #1006931 // AI Game & API p { newImageText = "New Letter!"; makeInstruction(); pGame(); swing { 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 // ////////////////// static RGBImage makeImage() { //ret loadRGBImage(/*#1006930*/#1006944); S letter = "" + randomCharBetween('A', 'Z'); ret new RGBImage(renderText(#1004568, 100, letter)); } //////////////// // FORM MAKER // //////////////// static JComponent makeTheForm(final GameForAI game) { null; // TODO } static Pt ptFromForm(JTextField x, JTextField y) { ret new Pt(intFromTextField(x), intFromTextField(y)); } static Pt ptFromForm(JSpinner x, JSpinner y) { ret new Pt(intFromSpinner(x), intFromSpinner(y)); } ////////////// // RENDERER // ////////////// static RGBImage renderImage(Submission s) { BufferedImage bi = newBufferedImage(w, h, Color.white); for (Line l : s.lines) drawRoundEdgeLine(bi, l.a, l.b, Color.black, lineWidth); ret new RGBImage(bi); } ////////////////////////////////////// // Test AIs. Just add your own here // ////////////////////////////////////// AI > AIRandom { 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)); } } AIRandom > AIRandomWithVariation { 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 > AIRacer { AIRandomWithVariation leader, overtaker; new Best leadersBest; int forceChangeEvery = 2000; int roundsSinceChange; AIRandomWithVariation newAI() { ret new AIRandomWithVariation; } 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! leader = overtaker; overtaker = null; roundsSinceChange = 0; } else if (roundsSinceChange++ >= forceChangeEvery) { // make new overtaker overtaker = null; roundsSinceChange = 0; } } } }