Generate random chess position: Difference between revisions
Content added Content deleted
(Added Julia language) |
mNo edit summary |
||
Line 268: | Line 268: | ||
struct King end |
struct King end |
||
struct Pawn end |
struct Pawn end |
||
struct Grid |
|||
M::Matrix{Char} |
|||
end |
|||
Grid(n::Integer) = Grid(fill(' ', n, n)) |
|||
Base.size(g::Grid) = size(g.M) |
|||
Base.size(g::Grid, d) = size(g.M, d) |
|||
Base.axes(g::Grid) = axes(g.M) |
|||
Base.axes(g::Grid, d) = axes(g.M, d) |
|||
Base.getindex(g::Grid, I...) = getindex(g.M, I...) |
|||
Base.setindex!(g::Grid, v, I...) = setindex!(g.M, v, I...) |
|||
function Base.show(io::IO, g::Grid) |
|||
println(io, " ", join(@sprintf("%2d", d) for d in 1:size(g, 1))) |
|||
for r in 1:size(g, 2) |
|||
@printf(io, "%2d %s\n", r, join(g.M[r, :], ' ')) |
|||
end |
|||
return nothing |
|||
end |
|||
function placepieces!(grid, ::King) |
function placepieces!(grid, ::King) |
||
Line 332: | Line 313: | ||
return grid |
return grid |
||
end |
end |
||
printgrid(grid) = println(join((join(grid[r, :], ' ') for r in 1:size(grid, 1)), '\n')) |
|||
end # module Chess</lang> |
end # module Chess</lang> |
||
'''Main''': |
'''Main''': |
||
<lang julia>grid = |
<lang julia>grid = fill(' ', 8, 8) |
||
Chess.randposition!(grid) |
Chess.randposition!(grid) |
||
Chess.printgrid(grid)</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> ♕ ♞ ♗ |
||
♖ ♙ ♔ ♙ ♖ ♜ ♙ |
|||
♟ ♙ ♙ ♜ |
|||
♘ ♝ ♙ ♙ ♟ |
|||
♙ ♚ ♝ ♟ |
|||
♟ ♟ ♗ ♟ |
|||
♙ ♞ ♟ ♟ |
|||
♘ ♛</pre> |
|||
8 ♘ ♗ ♞ ♗</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |