Skip to content

Commit

Permalink
Chess attack graph example
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine committed Sep 13, 2022
1 parent c87e5ad commit e3da967
Show file tree
Hide file tree
Showing 10 changed files with 1,071 additions and 4 deletions.
4 changes: 3 additions & 1 deletion examples/chess/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
all: coffee js
all: coffee js graph
coffee:
svgtiler -f map.coffee *.asc
js:
svgtiler -f map.jsx *.asc
graph:
svgtiler -f -o graph map.coffee graph.coffee *.asc
13 changes: 12 additions & 1 deletion examples/chess/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
|-----------|------------|--------|
| ![Initial chessboard](board-init.svg) | [ASCII input](board-init.asc)<br>[SVG output](board-init.svg) | [Wikipedia: Chess](https://en.wikipedia.org/wiki/Chess) |
| ![Immortal Game chessboard](board-immortal.svg) | [ASCII input](board-immortal.asc)<br>[SVG output](board-immortal.svg) | [Wikipedia: Immortal Game](https://en.wikipedia.org/wiki/Immortal_Game) (last position) |
| ![Kasparov's Immortal chessboard](board-kasparov-immortal.svg) | [ASCII input](board-kasparov-immortal.asc)<br>[SVG output](board-karparov-immortal.svg) | [Wikipedia: Karparov's Immortal](https://en.wikipedia.org/wiki/Kasparov%27s_Immortal) (last position) |

## Drawings with Attack Graph

| Rendering | Repo Files | Source |
|-----------|------------|--------|
| ![Initial chessboard](graph/board-init.svg) | [ASCII input](board-init.asc)<br>[SVG output](board-init.svg) | [Wikipedia: Chess](https://en.wikipedia.org/wiki/Chess) |
| ![Immortal Game chessboard](graph/board-immortal.svg) | [ASCII input](board-immortal.asc)<br>[SVG output](board-immortal.svg) | [Wikipedia: Immortal Game](https://en.wikipedia.org/wiki/Immortal_Game) (last position) |
| ![Kasparov's Immortal chessboard](graph/board-kasparov-immortal.svg) | [ASCII input](board-kasparov-immortal.asc)<br>[SVG output](board-karparov-immortal.svg) | [Wikipedia: Karparov's Immortal](https://en.wikipedia.org/wiki/Kasparov%27s_Immortal) (last position) |

## Mapping

Expand All @@ -15,7 +24,9 @@
* Using `require` to load and modify external SVG files
(strip off unnecessary `<svg>` wrapper)
* JSX notation for creating and composing symbols
* `svgtiler.afterRender` for rendering a background
* `svgtiler.afterRender` for rendering a background white rectangle
* [CoffeeScript file](graph.coffee) for rendering the graph of attacks,
illustrating more advanced `svgtiler.afterRender` usage.

## Piece Shapes

Expand Down
2 changes: 1 addition & 1 deletion examples/chess/board-immortal.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/chess/board-init.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions examples/chess/board-kasparov-immortal.asc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
b..r...r
k....p.p
p..q.np.
NppP....
...p.Q..
P....PPB
.PP....P
.K.RR...
224 changes: 224 additions & 0 deletions examples/chess/board-kasparov-immortal.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions examples/chess/graph.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Render piece attack graph

size = 45 # width and height of svg files

graphs = share.graphs ? 'knprbq' # which piece attack graphs to draw
friendly = share.friendly ? false # include friendly fire attacks?
graphColor = 'hsl(286,65%,50%,70%)'
pieces = new Set ['k', 'n', 'p', 'r', 'b', 'q']
pieces.add lc.toUpperCase() for lc in (x for x from pieces)

player = (key) ->
key == key.toLowerCase()

svgtiler.afterRender (render) ->
{drawing} = render
edges = []
for row, y in drawing.keys
for key, x in row
p1 = player key
key = key.toLowerCase()
continue unless graphs.includes key

longMove = (dx, dy) ->
x2 = x + dx
y2 = y + dy
while (neighbor = drawing.get x2, y2)?
return [x2, y2] if pieces.has neighbor
x2 += dx
y2 += dy
return null # no piece found

switch key
when 'k'
neighbors = [[x-1, y+1], [x, y+1], [x+1, y], [x+1, y+1]]
when 'n'
neighbors = [[x-2, y+1], [x-1, y+2], [x+1, y+2], [x+2, y+1]]
when 'p'
neighbors = [[x-1, y+1], [x+1, y+1]]
when 'r'
neighbors = [
longMove +1, 0
longMove 0, +1
]
when 'b'
neighbors = [
longMove +1, +1
longMove -1, +1
]
when 'q'
neighbors = [
longMove +1, 0
longMove 0, +1
longMove +1, +1
longMove -1, +1
]
else
console.warn "Unknown piece type for graph: #{graph}"
continue

for neighbor in neighbors when neighbor?
[x2, y2] = neighbor
key2 = drawing.get x2, y2
continue unless pieces.has key2
p2 = player key2
continue unless friendly or p1 != p2
edges.push \
<line x1={x * size + size/2}
y1={y * size + size/2}
x2={x2 * size + size/2}
y2={y2 * size + size/2}
stroke={graphColor} stroke-width="10" stroke-linecap="round"
/>

<svg>{edges}</svg>

null
Loading

0 comments on commit e3da967

Please sign in to comment.