To be able to edit code and run cells, you need to run the notebook yourself. Where would you like to run the notebook?

This notebook takes about 40 seconds to run.

In the cloud (experimental)

Binder is a free, open source service that runs scientific notebooks in the cloud! It will take a while, usually 2-7 minutes to get a session.

On your computer

(Recommended if you want to store your changes.)

  1. Copy the notebook URL:
  2. Run Pluto

    (Also see: How to install Julia and Pluto)

  3. Paste URL in the Open box

Frontmatter

If you are publishing this notebook on the web, you can set the parameters below to provide HTML metadata. This is useful for search engines and social media.

Author 1
👀 Reading hidden code
using Chess
644 ms
👀 Reading hidden code
126 μs

White to move

Castle rights: KQkq

Open in lichess

👀 Reading hidden code
startboard()
72.3 μs
👀 Reading hidden code
domove(startboard(), "e4") |> moves
89.1 ms
👀 Reading hidden code
using PlutoUI
142 ms
1
@bind tick Slider(1:length(xs); show_value=true)
👀 Reading hidden code
241 ms

Black to move

Castle rights: KQkq

Open in lichess

b = xs[tick]
👀 Reading hidden code
20.3 μs
16
pieces(xs[tick], BLACK) |> squarecount
👀 Reading hidden code
13.2 ms
16
pieces(xs[tick], WHITE) |> squarecount
👀 Reading hidden code
25.0 μs
if ischeckmate(b)
md" $(!sidetomove(b)) wins"
elseif isstalemate(b)
"stalemate"
end
👀 Reading hidden code
349 ms
Base.:(!)(p::PieceColor) = p === BLACK ? WHITE : BLACK
👀 Reading hidden code
450 μs
xs = try_until_result(random_game)
👀 Reading hidden code
40.1 ms

Black to move

Open in lichess

x = try_until_result(random_result)
👀 Reading hidden code
48.8 ms
true
ischeckmate(x)
👀 Reading hidden code
14.6 μs
false
isstalemate(x)
👀 Reading hidden code
17.3 μs
random_result (generic function with 1 method)
function random_result()
board = startboard()
while (next_moves = moves(board); !isempty(next_moves))
choice = rand(next_moves)
board = domove(board, choice)
end
board
end
👀 Reading hidden code
674 μs
random_game (generic function with 1 method)
function random_game()
result = []
board = startboard()
while (next_moves = moves(board); !isempty(next_moves))
choice = rand(next_moves)
board = domove(board, choice)
push!(result, board)
end
result
end
👀 Reading hidden code
724 μs
try_until_result (generic function with 1 method)
function try_until_result(f::Function, args...)
try
f(args...)
catch
try_until_result(f, args...)
end
end
👀 Reading hidden code
602 μs

Chess move input

👀 Reading hidden code
189 μs
islegal (generic function with 1 method)
islegal(b::Board, m::Move) = m ∈ moves(b)
👀 Reading hidden code
449 μs

Black to move

Open in lichess

your_move_board = try_until_result(random_game)[end-20]
👀 Reading hidden code
9.8 ms

Make a move!

👀 Reading hidden code
185 μs
abcdefgh87654321
@bind next_move_raw board_input(your_move_board)
👀 Reading hidden code
51.7 ms
missing
next_move_raw
👀 Reading hidden code
11.6 μs
squarefromstring (generic function with 1 method)
s = squarefromstring
👀 Reading hidden code
13.1 μs
Error message

MethodError: no method matching getindex(::Missing, ::String)

Stack trace

Here is what happened, the most recent locations are first:

  1. next_move = Move(s(next_move_raw["from"]), s(next_move_raw["to"]))
next_move = Move(s(next_move_raw["from"]), s(next_move_raw["to"]))
👀 Reading hidden code
---
Error message

Another cell defining next_move contains errors.

islegal(your_move_board, next_move)
👀 Reading hidden code
---
Error message

Another cell defining next_move contains errors.

Don't panic!
domove(your_move_board, next_move)
👀 Reading hidden code
---
using HypertextLiteral
👀 Reading hidden code
2.9 ms
board_input (generic function with 2 methods)
board_input(b=startboard()) = @htl("""
<div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/cm-chessboard@3.10.6/styles/cm-chessboard.css"/>

<script id="this">
const div = currentScript.parentElement

const {INPUT_EVENT_TYPE, COLOR, Chessboard} = await import("https://cdn.jsdelivr.net/npm/cm-chessboard@3.10.6/src/cm-chessboard/Chessboard.js")

function inputHandler(event) {
if (event.type === INPUT_EVENT_TYPE.moveDone) {
const move = {from: event.squareFrom, to: event.squareTo}
div.value = move
div.dispatchEvent(new CustomEvent("input"))
} else {
return true
}
}
const board_div = html`<div id="board" style="max-width: 300px;">
</div>`
const board = new Chessboard(board_div, {
position: $(repr(fen(b))),
sprite: {url: "https://cdn.jsdelivr.net/npm/cm-chessboard@3.10.6/assets/images/chessboard-sprite-staunty.svg"},
orientation: COLOR.white
})
board.enableMoveInput(inputHandler, COLOR.white)
return board_div
</script>
</div>
""")
👀 Reading hidden code
473 ms

Let's play a game

👀 Reading hidden code
200 μs
board = Ref(startboard())
👀 Reading hidden code
30.0 μs
abcdefgh87654321
@initially missing @bind move_raw f(move_raw)
👀 Reading hidden code
29.6 ms
f (generic function with 1 method)
function f(move_raw)
if move_raw !== missing
move = Move(s(move_raw["from"]), s(move_raw["to"]))
if islegal(board[], move)
# do your move for white
board[] = domove(board[], move)
# do random move for black
board[] = domove(board[], rand(moves(board[])))
end
end
board_input(board[])
end
👀 Reading hidden code
926 μs
intially_function (generic function with 1 method)
👀 Reading hidden code
8.8 ms