Download Jar. Uses 4489K of libraries. Click here for Pure Java version (7322L/51K).
1 | !7 |
2 | |
3 | static CirclesAndLines cal; |
4 | static Thinker thinker; |
5 | |
6 | !include #1007377 // traits |
7 | |
8 | p-pretty {
|
9 | cal = new CirclesAndLines; |
10 | makeGraph(); |
11 | Canvas canvas = cal.show(900, 600); |
12 | (thinker = nu(Thinker, +cal, +canvas, |
13 | resetGraph := f makeGraph, think := f think)).init(); |
14 | } |
15 | |
16 | svoid makeGraph {
|
17 | mergedNodes = 0; |
18 | cal.clear(); |
19 | Circle a = cal.circle_autoVis("PLAYER", "Chess Player", 0.25, 0.7);
|
20 | Circle b = cal.circle_autoVis("PLAYER", "Chess Player", 0.75, 0.7);
|
21 | cal.arrow(a, "VS", b); |
22 | cal.circle_autoVis("MAGNUS CARLSEN", 0.25, 0.25);
|
23 | cal.circle_autoVis("HIKARU NAKAMURA", 0.75, 0.25);
|
24 | } |
25 | |
26 | sbool think() {
|
27 | // Find an isolated node and try to merge it with another node |
28 | if (!anyIsolatedNodes()) false; |
29 | Pair<Circle, Circle> p = findTwoRandomNodes(); |
30 | if (p != null && isIsolated(p.a) && nodesAttract(p.a, p.b)) |
31 | mergeNodes(p.a, p.b); |
32 | true; |
33 | } |
34 | |
35 | sbool traitsAttract(S a, S b) {
|
36 | ret isPair(a, b, "player", "magnus carlsen") |
37 | || isPair(a, b, "player", "hikaru nakamura"); |
38 | } |
39 | |
40 | sbool traitsDiverge(S a, S b) {
|
41 | ret isPair(a, b, "magnus carlsen", "hikaru nakamura"); |
42 | } |
43 | |
44 | sbool anyIsolatedNodes() {
|
45 | for (Circle c : cal.circles) if (isIsolated(c)) true; false; |
46 | } |
47 | |
48 | sbool isIsolated(Circle c) {
|
49 | for (Line l : cal.lines) |
50 | if (l.a == c || l.b == c) |
51 | false; |
52 | true; |
53 | } |
54 | |
55 | static Pair<Circle, Circle> findTwoRandomNodes() {
|
56 | ret selectRandomPair(cal.circles); |
57 | } |
58 | |
59 | sbool nodesAttract(Circle a, Circle b) {
|
60 | if (nodesDiverge(a, b)) false; |
61 | for (S x : a.traits()) |
62 | for (S y : b.traits()) |
63 | if (traitsAttract(x, y)) |
64 | true; |
65 | false; |
66 | } |
67 | |
68 | sbool nodesDiverge(Circle a, Circle b) {
|
69 | for (S x : a.traits()) |
70 | for (S y : b.traits()) |
71 | if (traitsDiverge(x, y)) |
72 | true; |
73 | false; |
74 | } |
75 | |
76 | sbool isPair(S a, S b, S x, S y) {
|
77 | ret eqic(a, x) && eqic(b, y) |
78 | || eqic(a, y) && eqic(b, x); |
79 | } |
80 | |
81 | static int mergedNodes; |
82 | |
83 | svoid mergeNodes(Circle a, Circle b) {
|
84 | assertTrue("Circle must be isolated to merge", isIsolated(a));
|
85 | b.img = a.img; |
86 | b.addTraits(a.traits()); |
87 | cal.circles.remove(a); |
88 | thinker.status("Merged " + n(mergedNodes += 2, "node") + ".");
|
89 | } |
90 | |
91 | sclass Thinker {
|
92 | double delay = 0.1; // seconds |
93 | bool continueOnError = true; |
94 | CirclesAndLines cal; |
95 | Canvas canvas; |
96 | JTextArea textArea; |
97 | JButton btnThink; |
98 | volatile bool thinking; |
99 | S status, initialStatus = "Initial state."; |
100 | int step; |
101 | bool inited; |
102 | O think; // func -> bool |
103 | O resetGraph; // runnable |
104 | Throwable error; |
105 | |
106 | void init {
|
107 | if (inited) ret; |
108 | inited = true; |
109 | addControls(); |
110 | thread "Think" { Thinker.this.run(); }
|
111 | } |
112 | |
113 | void addControls {
|
114 | swing {
|
115 | addToWindowRight(canvas, withMargin(vstackWithSpacing( |
116 | jMaxWidth(167, |
117 | jMinHeight(180, jscroll(textArea = setFontSize(16, makeBold(wordWrapTypeWriterTextArea()))))), |
118 | btnThink = fatButton(100, "Think!", r { thinkOrStop() }),
|
119 | fatButton(60, "Think again!", r { thinkAgain() }),
|
120 | fatButton(60, "Reset", r { reset() })
|
121 | ))); |
122 | increaseFrameWidth(canvas, 180); |
123 | status(initialStatus); |
124 | } |
125 | } |
126 | |
127 | void startOver {
|
128 | lock(cal.lock, "StartOver", 2000); |
129 | try {
|
130 | step = 0; status(initialStatus); |
131 | callF(resetGraph); |
132 | } finally {
|
133 | unlock(cal.lock, "StartOver"); |
134 | } |
135 | if (canvas != null) canvas.update(); |
136 | } |
137 | |
138 | void thinkAgain {
|
139 | startOver(); |
140 | thinking(true); |
141 | } |
142 | |
143 | void reset {
|
144 | thinking(false); |
145 | startOver(); |
146 | } |
147 | |
148 | void run {
|
149 | repeat {
|
150 | sleepSeconds(delay); |
151 | if (thinking) pcall {
|
152 | print("Thinking");
|
153 | bool t; |
154 | time { t = think(); }
|
155 | if (!t) {
|
156 | thinking(false); |
157 | print("Done");
|
158 | } else {
|
159 | print("Updating");
|
160 | if (canvas != null) canvas.update(); |
161 | } |
162 | } |
163 | } |
164 | } |
165 | |
166 | void thinking(bool b) {
|
167 | thinking = b; status(); |
168 | setText(btnThink, b ? "<html><center>Stop<br>thinking</center></html>" : "Think!"); |
169 | } |
170 | |
171 | void status(S s) {
|
172 | status = s; status(); |
173 | } |
174 | |
175 | void thinkOrStop {
|
176 | thinking(!thinking); |
177 | } |
178 | |
179 | bool think() {
|
180 | if (think == null) false; |
181 | lock(cal.lock, "Thinking", 5000); |
182 | try {
|
183 | step++; status(); |
184 | if (isFalse(callF(think))) {
|
185 | step--; status(); |
186 | false; |
187 | } |
188 | true; |
189 | } catch e {
|
190 | printStackTrace(e); |
191 | error = getInnerException(e); |
192 | ret continueOnError; |
193 | } finally {
|
194 | unlock(cal.lock, "Done thinking"); |
195 | } |
196 | } |
197 | |
198 | JButton fatButton(int h, S text, O action) {
|
199 | JButton btn = jbutton(text, action); |
200 | btn.setFont(sansSerifBold(16)); |
201 | ret jMinSize(167, h, btn); |
202 | } |
203 | |
204 | void status {
|
205 | setText(textArea, status + "\n\n" + |
206 | (thinking ? "Thinking...\n\n" : "") + |
207 | (step == 0 ? "No thoughts made yet." |
208 | : "Thoughts made: " + step) + |
209 | (error == null ? "" : "\n\nERROR: " + exceptionToStringShort(error))); |
210 | } |
211 | } |
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1007370 |
| Snippet name: | Player VS Player [WORKS] |
| Eternal ID of this version: | #1007370/70 |
| Text MD5: | b509dbf9497084f5f25c446989e38fef |
| Transpilation MD5: | fc94b8766506fe7f94bb20459cd3a887 |
| Author: | stefan |
| Category: | javax / a.i. / gui |
| Type: | JavaX source code (desktop) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2017-03-19 18:02:03 |
| Source code size: | 5030 bytes / 211 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 950 / 2259 |
| Version history: | 69 change(s) |
| Referenced in: | [show references] |