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

67
LINES

< > BotCompany Repo | #1002228 // Chess Bot (developing)

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

Libraryless. Click here for Pure Java version (1655L/11K/39K).

!752

static class XY {
  int x, y; // 1-8 (a-h), 1-8
  
  *(int *x, int *y) {}
  *() {}
  
  public boolean equals(O o) {
    //ret stdEq(this, o, "x", "y");
    if (!(o instanceof XY)) ret false;
    XY xy = cast o;
    ret x == xy.x && y == xy.y;
  }
  
  boolean valid() {
    ret x >= 1 && x <= 8 && y >= 1 && y <= 8;
  }
  
  public S toString() {
    ret (char) ('a'+x-1) + "" + y;
  }
}

static XY pos(S s) {
  assertEquals(2, l(s));
  ret new XY(toLower(s.charAt(0))-'a'+1, s.charAt(1)-'1'+1);
}

p {
  makeBot("Chess Bot!");
}

answer {
  if "if the * is on *, where can it move?" {
    S piece = m.unq(0), pos = m.unq(1);
    try {
      ret structure(allToString(destPlaces(piece, pos(pos))));
    } catch (Exception e) { ret str(e); }
  }
}

static Map<S, int[]> movementTable = litmap(
  "king", new int[] {-1, -1, 0, -1, 1, -1, -1, 0, 1, 0, -1, 1, 0, 1, 1, 1},
  "queen", new int[] {-1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -7, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 7, -7, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, -1, 0, -2, 0, -3, 0, -4, 0, -5, 0, -6, 0, -7, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, -1, 0, -2, 0, -3, 0, -4, 0, -5, 0, -6, 0, -7},
  "knight", new int[] {-1, -2, -2, -1, 1, -2, 2, -1, 1, 2, 2, 1, -1, 2, -2, 1},
  "rook", new int[] {1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, -1, 0, -2, 0, -3, 0, -4, 0, -5, 0, -6, 0, -7, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, -1, 0, -2, 0, -3, 0, -4, 0, -5, 0, -6, 0, -7},
  "bishop", new int[] {-1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -7, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6, 7, -7, -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7},
);

static L<XY> destPlaces(S piece, XY pos) {
  int[] m = movementTable.get(piece.toLowerCase());
  if (m != null)
    ret movements(pos, m);
  throw fail("What is a *", piece);
}

static L<XY> movements(XY pos, int... bla) {
  new L<XY> l;
  for (int i = 0; i < l(bla); i += 2)
    addIfValid(l, new XY(pos.x + bla[i], pos.y + bla[i+1]));
  ret l;
}

static void addIfValid(L<XY> l, XY xy) {
  if (xy.valid()) l.add(xy);
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002228
Snippet name: Chess Bot (developing)
Eternal ID of this version: #1002228/1
Text MD5: 16f0de86f6507baed72f3e2b6abc40b3
Transpilation MD5: be3108d65ff5f4ee0620c829b5ce49df
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-30 19:30:51
Source code size: 2271 bytes / 67 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 659 / 1564
Referenced in: [show references]