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).

!7

abstract sclass GameViewForAI {
  abstract int w();
  abstract int h();
  abstract int getPixel(int x, int y);
}

abstract sclass AI {
  GameViewForAI game;
  int w, h;
  
  int getPixel(int x, int y) {
    ret game.getPixel(x, y);
  }
  
  abstract Pt nextClick();
}

sclass Game extends GameViewForAI {
  long points;
  int w = 500, h = 500;
  int rw = 50, rh = 50, border = 10;
  int x1, y1;
  
  int w() { ret w; }
  int h() { ret h; }
  
  int getPixel(int x, int y) {
    ret new Rect(x1, y1, rw, rh).contains(x, y)
      ? 0xFF0000 : 0xFFFFFF;
  }
  
  void nextImage() {
    x1 = random(border, w-rw-border);
    y1 = random(border, h-rh-border);
  }
  
  void click(Pt p) {
    if (rectContains(x1, y1, rw, rh, p)) {
      ++points;
      //print("Points: " + points);
      nextImage();
    }
  }
}

sclass DummyAI extends AI {
  Pt nextClick() {
    ret new Pt(50, 50);
  }
}

sclass RandomAI extends AI {
  Pt nextClick() {
    ret new Pt(random(w), random(h));
  }
}

sclass SearchingAI extends AI {
  Pt nextClick() {
    for (int y = 0; y < h; y += 10)
      for (int x = 0; x < w; x += 10)
        if (getPixel(x, y) != 0xFFFFFF)
          ret new Pt(x, y);
    null;
  }
}

static long pointsAfterNRounds(AI ai, long rounds) {
  new Game game;
  setOpt(ai, +game);
  setOpt(ai, "w", game.w());
  setOpt(ai, "h", game.h());
  long step = 0;
  pcall {
    while (step < rounds) {
      ++step;
      Pt p = ai.nextClick();
      if (p == null) { print("AI resigned"); break; }
      game.click(p);
      //print("Step " + step + ", move: " + p + ", points: " + game.points);
    }
  }
  print("AI " + shortClassName(ai) + " points after " + rounds + " rounds: " + game.points);
  ret game.points;
}

p {
  logOutputPlain();
  pointsAfterNRounds(new DummyAI, 10000);
  pointsAfterNRounds(new RandomAI, 10000);
  pointsAfterNRounds(new SearchingAI, 10000);
}

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: 549 / 687
Referenced in: [show references]