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

142
LINES

< > BotCompany Repo | #1007323 // Is Donald Good Or Bad? [WORKS]

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

Uses 3874K of libraries. Click here for Pure Java version (5061L/35K/132K).

1  
!7
2  
3  
!include #1007303 // CirclesAndLines
4  
5  
static CirclesAndLines cal;
6  
static Circle a, b, c, d;
7  
static Canvas canvas;
8  
static JButton btnThink, btnStartOver;
9  
static JTextArea textArea;
10  
volatile sbool thinking;
11  
static int step;
12  
static S status, initial = "I wonder: Is Donald good or bad?";
13  
14  
extend Base {
15  
  new L<S> traits;
16  
  bool hasTrait(S t) { ret containsIC(traits(), t); }
17  
  L<S> traits() { if (text != null && neq(first(traits), text)) traits.add(0, text); ret traits; }
18  
  
19  
  S textForRender() {
20  
    L<S> traits = traits();
21  
    if (l(traits) <= 1) ret text;
22  
    ret text + " [" + join(", ", dropFirst(traits)) + "]";
23  
  }
24  
}
25  
26  
p {
27  
  substance("Magellan");
28  
  cal = new CirclesAndLines;
29  
  a = cal.addCircle(#1007291, 0.25, 0.25, "DONALD");
30  
  b = cal.addCircle(#1007292, 0.75, 0.25, "BOMBS");
31  
  c = cal.addCircle(#1007314, 0.25, 0.715, "GOOD");
32  
  d = cal.addCircle(#1007319, 0.75, 0.8,  "BAD");
33  
  addArrows();
34  
  canvas = cal.showAsFrame(1000, 600);
35  
  swing {
36  
    addToWindowRight(canvas, withMargin(vstackWithSpacing(
37  
      jMaxWidth(167,
38  
        jMinHeight(180, jscroll(textArea = setFontSize(16, makeBold(wordWrapTypeWriterTextArea()))))),
39  
      btnThink = fatButton(100, "Think!", f action),
40  
      btnStartOver = fatButton(60, "Start over", f startOver))));
41  
    status = initial; status();
42  
  }
43  
  
44  
  hideConsole();
45  
  
46  
  repeat {
47  
    sleepSeconds(2);
48  
    if (thinking) pcall {
49  
      print("Thinking");
50  
      bool t;
51  
      time { t = think(); }
52  
      if (!t) {
53  
        thinking(false);
54  
        print("Done");
55  
      } else {
56  
        print("Updating");
57  
        canvas.update();
58  
      }
59  
    }
60  
  }
61  
}
62  
63  
svoid startOver {
64  
  cal.lock.lock();
65  
  step = 0; status = initial; status();
66  
  try {
67  
    for (Circle c : cal.circles) c.traits.clear();
68  
    cal.lines.clear();
69  
    addArrows();
70  
  } finally {
71  
    cal.lock.unlock();
72  
  }
73  
  canvas.update();
74  
}
75  
76  
svoid addArrows {
77  
  cal.addArrow(a, b, "MAKES");
78  
  cal.addArrow(b, d, "ARE");
79  
}
80  
81  
svoid thinking(bool b) {
82  
  thinking = b; status();
83  
  setText(btnThink, b ? "<html><center>Stop<br>thinking</center></html>" : "Think!");
84  
}
85  
86  
svoid action {
87  
  thinking(!thinking);
88  
}
89  
90  
// for (X is Y): addTrait(X, Y);
91  
// for (X makes bad): addLink(X is bad);
92  
sbool think() {
93  
  cal.lock.lock();
94  
  try {
95  
    step++; status();
96  
    bool change = false;
97  
    for (Line l : cloneList(cal.lines)) {
98  
      if (l.hasTrait("is") || l.hasTrait("are")) {
99  
        int n = l(l.a.traits());
100  
        // l.a.traits().addAll(l.b.traits()); // funny version
101  
        setAddAll(l.a.traits(), l.b.traits());
102  
        change |= l(l.a.traits()) > n;
103  
      }
104  
      if (eqic(l.text, "makes") && l.b.hasTrait("bad")) {
105  
        change |= addLink(l.a, "IS", cal.findCircle("bad"));
106  
      }
107  
    }
108  
    if (a.hasTrait("bad"))
109  
      status = "Donald is bad!";
110  
    else if (a.hasTrait("good"))
111  
      status = "Donald is good!";
112  
    status();
113  
    ret change;
114  
  } finally {
115  
    cal.lock.unlock();
116  
  }
117  
}
118  
119  
sbool addLink(Circle a, S link, Circle b) {
120  
  Arrow arrow = cal.findArrow(a, b);
121  
  if (arrow == null) {
122  
    arrow = cal.addArrow(a, b, link);
123  
    true;
124  
  } else {
125  
    if (arrow.hasTrait(link)) false;
126  
    arrow.traits().add(link);
127  
    true;
128  
  }
129  
}
130  
131  
static JButton fatButton(int h, S text, O action) {
132  
  JButton btn = jbutton(text, action);
133  
  btn.setFont(sansSerifBold(16));
134  
  ret jMinSize(167, h, btn);
135  
}
136  
137  
svoid status {
138  
  setText(textArea, status + "\n\n" +
139  
    (thinking ? "Thinking...\n\n" : "") +
140  
    (step == 0 ? "No thoughts made yet."
141  
      : "Thoughts made: " + step));
142  
}

Author comment

Began life as a copy of #1007312

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1007323
Snippet name: Is Donald Good Or Bad? [WORKS]
Eternal ID of this version: #1007323/63
Text MD5: 524a198dfbff67e986f0e33f55feeef8
Transpilation MD5: bec7b2072da0adac7f26f40755cb22a0
Author: stefan
Category: javax / gui
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-03-16 01:37:29
Source code size: 3596 bytes / 142 lines
Pitched / IR pitched: No / No
Views / Downloads: 614 / 923
Version history: 62 change(s)
Referenced in: [show references]