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 | } |