Jump to content

Generate random chess position: Difference between revisions

m
no edit summary
(Added Julia language)
mNo edit summary
Line 268:
struct King 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)
Line 332 ⟶ 313:
return grid
end
 
printgrid(grid) = println(join((join(grid[r, :], ' ') for r in 1:size(grid, 1)), '\n'))
 
end # module Chess</lang>
 
'''Main''':
<lang julia>grid = Chess.Gridfill(' ', 8, 8)
Chess.randposition!(grid)
printlnChess.printgrid(grid)</lang>
 
{{out}}
<pre> 1 2 3 4 5 6 7 8
1
2 ♟ ♙ ♙ ♜
3 ♟ ♟
4 ♝ ♟
5
6
7 ♛</pre>
8 ♘ ♗ ♞ ♗</pre>
 
=={{header|Kotlin}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.