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

182
LINES

< > BotCompany Repo | #1006674 // Second A. I. Game / Click On The Highest Number!

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

Uses 3874K of libraries. Click here for Pure Java version (5532L/39K/141K).

1  
!7
2  
3  
///////////////////////////
4  
// Your API to work with //
5  
///////////////////////////
6  
7  
abstract sclass GameForAI {
8  
  abstract RGBImage getImage();
9  
  abstract L<Rect> submit(Pt point); // returns correct solution so AI can learn from it
10  
}
11  
12  
abstract sclass AI {
13  
  // stuff you get
14  
  
15  
  GameForAI game;
16  
  RGBImage image() { ret game.getImage(); }
17  
  int w() { ret image().w(); }
18  
  int h() { ret image().h(); }
19  
  L<Rect> submit(Pt p) { ret game.submit(p); }
20  
  
21  
  // implement this method and call submit()
22  
  abstract void go();
23  
}
24  
25  
//////////////////////////////////////
26  
// Test AIs. Just add your own here //
27  
//////////////////////////////////////
28  
29  
AI > TestAI {
30  
  void go {
31  
    submit(new Pt(random(image().w()), random(image().h())));
32  
  }
33  
}
34  
35  
AI > ErrorAI {
36  
  void go {
37  
    throw new NullPointerException;
38  
  }
39  
}
40  
41  
///////////////
42  
// Main Game //
43  
///////////////
44  
45  
static int w = 400, h = 300;
46  
static int border = 10, fontSize = 30;
47  
static int rounds = 1000;
48  
static S font = #1004887;
49  
50  
static int points, clicks;
51  
static ImageSurface is;
52  
static RGBImage img;
53  
static new HashMap<Rect, Int> words;
54  
static L<Rect> solution;
55  
volatile sbool aiMode;
56  
static JLabel lblScore;
57  
58  
p-substance {
59  
  nextImage();
60  
  addToWindow(is, withMargin(lblScore = jcenteredBoldLabel("Your score: 0 of 0")));
61  
  for (final Class c : myNonAbstractClassesImplementing(AI)) {
62  
    final S name = shortClassName(c);
63  
    final JButton btn = jbutton("Run " + name);
64  
    onClick(btn, r {
65  
      if (aiMode) ret;
66  
      btn.setText(name + " Running...");
67  
      thread {
68  
        Game game = testAI(c);
69  
        S text = name + " scored " + game.points + " of " + rounds;
70  
        if (game.error != null)
71  
          text += " - ERROR: " + game.error;
72  
        setText(btn, text);
73  
      }
74  
    });
75  
    addToWindow(is, withMargin(btn));
76  
  }
77  
  centerTopFrame(packFrame(is));
78  
  hideConsole();
79  
}
80  
81  
svoid nextImage {
82  
  RGBImage img = rgbImage(Color.white, w, h);
83  
  words.clear();
84  
  for i to 5: {
85  
    int num = random(100);
86  
    S s = str(num);
87  
    RGB color = new RGB(random(0.5), random(0.5), random(0.5));
88  
    renderText_fg.set(color.getColor());
89  
    BufferedImage ti = renderText(font, fontSize, s);
90  
    Rect r;
91  
    do {
92  
      r = randomRect(w, h, border, ti.getWidth(), ti.getHeight());
93  
      if (r == null) fail("Image too small: \*ti.getWidth()*/*\*ti.getHeight()*/ > \*w*/*\*h*/");
94  
    } while (anyRectOverlaps(keys(words), r) && licensed());
95  
    rgbCopy(ti, img, r.x, r.y);
96  
    words.put(r, num);
97  
  }
98  
  solution = keysWithBiggestValue(words);
99  
  
100  
  main.img = img;
101  
  if (aiMode) ret; // occasional updates only when AI is running
102  
  showTheImage();
103  
}
104  
105  
svoid showTheImage {
106  
  bool first = is == null;
107  
  is = showImage(is, img, "Click On The Highest Number!");
108  
  if (first) {
109  
    onLeftClick(is, voidfunc(Pt p) {
110  
      ++clicks;
111  
      if (anyRectContains(solution, p)) {
112  
        ++points;
113  
        nextImage();
114  
      }
115  
      lblScore.setText(print("Your score: " + points + " of " + clicks));
116  
    });
117  
    disableImageSurfaceSelector(is);
118  
    
119  
    // animate if idle
120  
    awtEvery(is, 1000, r {
121  
      if (!aiMode && isInForeground(is) && !mouseInComponent(is))
122  
        nextImage();
123  
    });
124  
    
125  
    // show current image occasionally when AI is running
126  
    awtEvery(is, 20, r {
127  
      if (aiMode) showTheImage();
128  
    });
129  
  }
130  
}
131  
132  
// AI stuff
133  
134  
sclass Game extends GameForAI {
135  
  int points;
136  
  bool submitted;
137  
  Throwable error;
138  
  
139  
  RGBImage getImage() { ret img; }
140  
141  
  L<Rect> submit(Pt p) {
142  
    if (submitted) fail("No multi-submit please");
143  
    submitted = true;
144  
    if (anyRectContains(solution, p)) {
145  
      ++points;
146  
      nextImage();
147  
    }
148  
    ret solution;
149  
  }
150  
}
151  
152  
static Game scoreAI(AI ai, long rounds) {
153  
  aiMode = true;
154  
  try {
155  
    new Game game;
156  
    setOpt(ai, +game);
157  
    long step = 0;
158  
    while (step < rounds) {
159  
      ++step;
160  
      game.submitted = false;
161  
      try {
162  
        ai.go();
163  
      } catch e {
164  
        if (game.error == null) { // print first error to console
165  
          showConsole();
166  
          printStackTrace(e);
167  
        }
168  
        game.error = e;
169  
      }
170  
      //print("Step " + step + ", move: " + p + ", points: " + game.points);
171  
    }
172  
    print("AI " + shortClassName(ai) + " points after " + rounds + " rounds: " + game.points);
173  
    ret game;
174  
  } finally {
175  
    aiMode = false;
176  
    awt { nextImage(); }
177  
  }
178  
}
179  
180  
static Game testAI(Class<? extends AI> c) {
181  
  ret scoreAI(nu(c), rounds);
182  
}

Author comment

Began life as a copy of #1006656

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: #1006674
Snippet name: Second A. I. Game / Click On The Highest Number!
Eternal ID of this version: #1006674/71
Text MD5: 91b20f8906cd31cc03c39b273f86fd66
Transpilation MD5: 80c927a1119cb61eb8d7764ab20a695c
Author: stefan
Category: javax / gui
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-02-02 03:08:26
Source code size: 4533 bytes / 182 lines
Pitched / IR pitched: No / No
Views / Downloads: 594 / 928
Version history: 70 change(s)
Referenced in: [show references]