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

94
LINES

< > BotCompany Repo | #1006663 // A.I. Game [headless]

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

Libraryless. Click here for Pure Java version (2157L/14K/48K).

1  
!7
2  
3  
abstract sclass GameViewForAI {
4  
  abstract int w();
5  
  abstract int h();
6  
  abstract int getPixel(int x, int y);
7  
}
8  
9  
abstract sclass AI {
10  
  GameViewForAI game;
11  
  int w, h;
12  
  
13  
  int getPixel(int x, int y) {
14  
    ret game.getPixel(x, y);
15  
  }
16  
  
17  
  abstract Pt nextClick();
18  
}
19  
20  
sclass Game extends GameViewForAI {
21  
  long points;
22  
  int w = 500, h = 500;
23  
  int rw = 50, rh = 50, border = 10;
24  
  int x1, y1;
25  
  
26  
  int w() { ret w; }
27  
  int h() { ret h; }
28  
  
29  
  int getPixel(int x, int y) {
30  
    ret new Rect(x1, y1, rw, rh).contains(x, y)
31  
      ? 0xFF0000 : 0xFFFFFF;
32  
  }
33  
  
34  
  void nextImage() {
35  
    x1 = random(border, w-rw-border);
36  
    y1 = random(border, h-rh-border);
37  
  }
38  
  
39  
  void click(Pt p) {
40  
    if (rectContains(x1, y1, rw, rh, p)) {
41  
      ++points;
42  
      //print("Points: " + points);
43  
      nextImage();
44  
    }
45  
  }
46  
}
47  
48  
sclass DummyAI extends AI {
49  
  Pt nextClick() {
50  
    ret new Pt(50, 50);
51  
  }
52  
}
53  
54  
sclass RandomAI extends AI {
55  
  Pt nextClick() {
56  
    ret new Pt(random(w), random(h));
57  
  }
58  
}
59  
60  
sclass SearchingAI extends AI {
61  
  Pt nextClick() {
62  
    for (int y = 0; y < h; y += 10)
63  
      for (int x = 0; x < w; x += 10)
64  
        if (getPixel(x, y) != 0xFFFFFF)
65  
          ret new Pt(x, y);
66  
    null;
67  
  }
68  
}
69  
70  
static long pointsAfterNRounds(AI ai, long rounds) {
71  
  new Game game;
72  
  setOpt(ai, +game);
73  
  setOpt(ai, "w", game.w());
74  
  setOpt(ai, "h", game.h());
75  
  long step = 0;
76  
  pcall {
77  
    while (step < rounds) {
78  
      ++step;
79  
      Pt p = ai.nextClick();
80  
      if (p == null) { print("AI resigned"); break; }
81  
      game.click(p);
82  
      //print("Step " + step + ", move: " + p + ", points: " + game.points);
83  
    }
84  
  }
85  
  print("AI " + shortClassName(ai) + " points after " + rounds + " rounds: " + game.points);
86  
  ret game.points;
87  
}
88  
89  
p {
90  
  logOutputPlain();
91  
  pointsAfterNRounds(new DummyAI, 10000);
92  
  pointsAfterNRounds(new RandomAI, 10000);
93  
  pointsAfterNRounds(new SearchingAI, 10000);
94  
}

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, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv

No comments. add comment

Snippet ID: #1006663
Snippet name: A.I. Game [headless]
Eternal ID of this version: #1006663/1
Text MD5: 3facdd3122c4cfbc380beee43c913891
Transpilation MD5: b2eb3fb66ae5dc21554cf8a2b3d97589
Author: stefan
Category: javax / gui
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-02-03 06:10:07
Source code size: 1968 bytes / 94 lines
Pitched / IR pitched: No / No
Views / Downloads: 552 / 690
Referenced in: [show references]