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).

1  
!7
2  
3  
static int lineWidth = /*18*/20;
4  
static int lines = 6;
5  
sbool drawLines;
6  
7  
!include #1006931 // AI Game & API
8  
9  
p-autorestart {
10  
  newImageText = "New Letter!";
11  
  makeInstruction();
12  
  pGame();
13  
  swing {
14  
    setFrameTitle(is, "A. I. Game 5");
15  
    final JSpinner spinner = jSpinner(lines, 1, 100);
16  
    addToWindowPack(is, withMargin(jRightAligned(withLabel("Number of lines to use:", spinner))));
17  
    onChange(spinner, r {
18  
      lines = intFromSpinner(spinner);
19  
      makeInstruction();
20  
      restartAIs();
21  
    });
22  
  }
23  
}
24  
25  
svoid makeInstruction {
26  
  setInstruction("Reproduce this image with " + n(lines, "straight line") + ":");
27  
}
28  
29  
sclass Line {
30  
  Pt a, b;
31  
  
32  
  *() {}
33  
  *(Pt *a, Pt *b) {}
34  
}
35  
36  
sclass Submission {
37  
  new L<Line> lines;
38  
  
39  
  *() {}
40  
  *(Line... lines) { this.lines = asList(lines); check(); }
41  
  *(L<Line> *lines) { check(); }
42  
  
43  
  void check {
44  
    assertEquals(main.lines, l(lines));
45  
  }
46  
}
47  
48  
//////////////////
49  
// PUZZLE MAKER //
50  
//////////////////
51  
52  
sS lastLetter;
53  
54  
static RGBImage makeImage() {
55  
  //ret loadRGBImage(/*#1006930*/#1006944);
56  
  S letter;
57  
  do {
58  
    letter = "" + randomCharBetween('A', 'Z');
59  
  } while (eq(letter, lastLetter));
60  
  lastLetter = letter;
61  
  renderText_withLeading = false;
62  
  ret new RGBImage(renderText(#1004568, 100, letter));
63  
}
64  
65  
///////////////
66  
// RENDERERS //
67  
///////////////
68  
69  
static RGBImage renderImage(Submission s) {
70  
  ret new RGBImage(renderImage1(s));
71  
}
72  
73  
static BufferedImage renderImage1(Submission s) {
74  
  BufferedImage bi = newBufferedImage(w, h, Color.white);
75  
  for (Line l : s.lines)
76  
    drawRoundEdgeLine(bi, l.a, l.b, Color.black, lineWidth);
77  
  ret bi;
78  
}
79  
80  
static RGBImage renderWithHints(Submission s) {
81  
  BufferedImage bi = renderImage1(s);
82  
  if (drawLines) {
83  
    for (Line l : s.lines)
84  
      drawRoundEdgeLine(bi, l.a, l.b, Color.lightGray, 1);
85  
    ret new RGBImage(bi);
86  
  } else {
87  
    RGBImage img = new RGBImage(bi);
88  
    for (Line l : s.lines)
89  
      rgbMarkPoints(img, Color.lightGray, 1, l.a, l.b);
90  
    ret rgbUncache(img);
91  
  }
92  
}
93  
94  
//////////////////////////////////////
95  
// Test AIs. Just add your own here //
96  
//////////////////////////////////////
97  
98  
AI > AI_Random {
99  
  new Best<Submission> best;
100  
  
101  
  Line randomLine() {
102  
    ret new Line(randomPoint(), randomPoint());
103  
  }
104  
  
105  
  Pt randomPoint() { ret main.randomPoint(image()); }
106  
107  
  void go {
108  
    //print("Round: " + round());
109  
    if (round() == 1 && best.has()) // TODO: automate this
110  
      submit(best.get());
111  
    else {
112  
      Submission guess = guess();
113  
      updateBest(guess, submit(guess));
114  
    }
115  
  }
116  
  
117  
  void updateBest(Submission guess, double score) {
118  
    best.put(guess, score);
119  
  }
120  
  
121  
  Submission guess() {
122  
    ret new Submission(produceN(func { randomLine() }, lines));
123  
  }
124  
}
125  
126  
AI_Random > AI_RandomWithVariation {
127  
  int n;
128  
  
129  
  Submission guess() {
130  
    if (odd(n++) && best.has())
131  
      ret vary(best.get());
132  
    else
133  
      ret super.guess();
134  
  }
135  
  
136  
  Submission vary(Submission s) {
137  
    s = cloneThroughStructure(s);
138  
    varyLine(random(s.lines));
139  
    ret s;
140  
  }
141  
  
142  
  void varyLine(Line l) {
143  
    if (tossACoin())
144  
      l.a = varyPoint(l.a);
145  
    else
146  
      l.b = varyPoint(l.b);
147  
  }
148  
  
149  
  Pt varyPoint(Pt p) {
150  
    ret tossACoin() ? randomPoint() : varyPoint(p, 10);
151  
  }
152  
  
153  
  Pt varyPoint(Pt p, int range) {
154  
    range = max(range, 1);
155  
    ret new Pt(
156  
      random(p.x-range, p.x+range+1),
157  
      random(p.y-range, p.y+range+1));
158  
  }
159  
}
160  
161  
AI > AI_Racer {
162  
  AI_RandomWithVariation leader, overtaker;
163  
  new Best<Submission> leadersBest;
164  
  int discardEvery = 5000;
165  
  int roundsSinceChange;
166  
  
167  
  AI_RandomWithVariation newAI() { ret new AI_RandomWithVariation; }
168  
  
169  
  void go {
170  
    if (round() == 1 && leadersBest.has()) // TODO: automate this
171  
      submit(leadersBest.get());
172  
    else if (tossACoin()) {
173  
      if (leader == null) leader = newAI();
174  
      initSubAI(leader);
175  
      Submission guess = leader.guess();
176  
      double score = submit(guess);
177  
      leader.updateBest(guess, score);
178  
      leadersBest.put(guess, score);
179  
    } else {
180  
      if (overtaker == null) overtaker = newAI();
181  
      initSubAI(overtaker);
182  
      Submission guess = overtaker.guess();
183  
      double score = submit(guess);
184  
      overtaker.updateBest(guess, score);
185  
      if (score > leadersBest.score()) {
186  
        // displace leader!
187  
        print("Overtake at " + formatScore(overtaker.best.score()) + " vs " + formatScore(leadersBest.score()));
188  
        leader = overtaker;
189  
        overtaker = null;
190  
        roundsSinceChange = 0;
191  
      } else if (roundsSinceChange++ >= discardEvery) {
192  
        // make new overtaker
193  
        print("Discarding overtaker at " + formatScore(overtaker.best.score()) + " vs " + formatScore(leadersBest.score()));
194  
        overtaker = null;
195  
        roundsSinceChange = 0;
196  
      }
197  
    }
198  
  }
199  
}

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: 654 / 1771
Version history: 59 change(s)
Referenced in: [show references]