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

1  
!752
2  
3  
static class XY {
4  
  int x, y; // 1-8 (a-h), 1-8
5  
  
6  
  *(int *x, int *y) {}
7  
  *() {}
8  
  
9  
  public boolean equals(O o) {
10  
    //ret stdEq(this, o, "x", "y");
11  
    if (!(o instanceof XY)) ret false;
12  
    XY xy = cast o;
13  
    ret x == xy.x && y == xy.y;
14  
  }
15  
  
16  
  boolean valid() {
17  
    ret x >= 1 && x <= 8 && y >= 1 && y <= 8;
18  
  }
19  
  
20  
  public S toString() {
21  
    ret (char) ('a'+x-1) + "" + y;
22  
  }
23  
}
24  
25  
static XY pos(S s) {
26  
  assertEquals(2, l(s));
27  
  ret new XY(toLower(s.charAt(0))-'a'+1, s.charAt(1)-'1'+1);
28  
}
29  
30  
p {
31  
  makeBot("Chess Bot!");
32  
}
33  
34  
answer {
35  
  if "if the * is on *, where can it move?" {
36  
    S piece = m.unq(0), pos = m.unq(1);
37  
    try {
38  
      ret structure(allToString(destPlaces(piece, pos(pos))));
39  
    } catch (Exception e) { ret str(e); }
40  
  }
41  
}
42  
43  
static Map<S, int[]> movementTable = litmap(
44  
  "king", new int[] {-1, -1, 0, -1, 1, -1, -1, 0, 1, 0, -1, 1, 0, 1, 1, 1},
45  
  "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},
46  
  "knight", new int[] {-1, -2, -2, -1, 1, -2, 2, -1, 1, 2, 2, 1, -1, 2, -2, 1},
47  
  "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},
48  
  "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},
49  
);
50  
51  
static L<XY> destPlaces(S piece, XY pos) {
52  
  int[] m = movementTable.get(piece.toLowerCase());
53  
  if (m != null)
54  
    ret movements(pos, m);
55  
  throw fail("What is a *", piece);
56  
}
57  
58  
static L<XY> movements(XY pos, int... bla) {
59  
  new L<XY> l;
60  
  for (int i = 0; i < l(bla); i += 2)
61  
    addIfValid(l, new XY(pos.x + bla[i], pos.y + bla[i+1]));
62  
  ret l;
63  
}
64  
65  
static void addIfValid(L<XY> l, XY xy) {
66  
  if (xy.valid()) l.add(xy);
67  
}

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