Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

199
LINES

< > BotCompany Repo | #1006945 // A. I. Game 5.1 / Letters [solved]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (11426L/83K).

!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<Line> lines;
  
  *() {}
  *(Line... lines) { this.lines = asList(lines); check(); }
  *(L<Line> *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<Submission> 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<Submission> 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;
      }
    }
  }
}

Author comment

Began life as a copy of #1006932

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1006945
Snippet name: A. I. Game 5.1 / Letters [solved]
Eternal ID of this version: #1006945/60
Text MD5: 781794126814b56ed410e06f0e9e346d
Transpilation MD5: 82ad3fd6e48d4b76f624e1057b38d20d
Author: stefan
Category: javax / gui / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-06 23:35:15
Source code size: 4900 bytes / 199 lines
Pitched / IR pitched: No / No
Views / Downloads: 638 / 1738
Version history: 59 change(s)
Referenced in: [show references]