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

95
LINES

< > BotCompany Repo | #1006667 // A.I. Game [headless] [non-working draft copy made in video]

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

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 MyAI extends AI {
49  
  Pt nextClick() {
50  
    getPixel.....
51  
    ret new Pt(0, 0);
52  
  }
53  
}
54  
55  
sclass RandomAI extends AI {
56  
  Pt nextClick() {
57  
    ret new Pt(random(w), random(h));
58  
  }
59  
}
60  
61  
sclass SearchingAI extends AI {
62  
  Pt nextClick() {
63  
    for (int y = 0; y < h; y += 10)
64  
      for (int x = 0; x < w; x += 10)
65  
        if (getPixel(x, y) != 0xFFFFFF)
66  
          ret new Pt(x, y);
67  
    null;
68  
  }
69  
}
70  
71  
static long pointsAfterNRounds(AI ai, long rounds) {
72  
  new Game game;
73  
  setOpt(ai, +game);
74  
  setOpt(ai, "w", game.w());
75  
  setOpt(ai, "h", game.h());
76  
  long step = 0;
77  
  pcall {
78  
    while (step < rounds) {
79  
      ++step;
80  
      Pt p = ai.nextClick();
81  
      if (p == null) { print("AI resigned"); break; }
82  
      game.click(p);
83  
      //print("Step " + step + ", move: " + p + ", points: " + game.points);
84  
    }
85  
  }
86  
  print("AI " + shortClassName(ai) + " points after " + rounds + " rounds: " + game.points);
87  
  ret game.points;
88  
}
89  
90  
p {
91  
  logOutputPlain();
92  
  pointsAfterNRounds(new MyAI, 10000);
93  
  pointsAfterNRounds(new RandomAI, 10000);
94  
  pointsAfterNRounds(new SearchingAI, 10000);
95  
}

Author comment

Began life as a copy of #1006663

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

Comments [hide]

ID Author/Program Comment Date
1311 stefan See https://www.youtube.com/watch?v=PJC4ej5-PDo 2017-02-01 16:23:30

add comment

Snippet ID: #1006667
Snippet name: A.I. Game [headless] [non-working draft copy made in video]
Eternal ID of this version: #1006667/4
Text MD5: 12e8067a2da2777e19c04be8c411564d
Author: stefan
Category: javax / gui
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-02-01 16:23:03
Source code size: 1979 bytes / 95 lines
Pitched / IR pitched: No / No
Views / Downloads: 633 / 634
Version history: 3 change(s)
Referenced in: [show references]