Skip to content

Instantly share code, notes, and snippets.

@hpoit
Last active February 8, 2018 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpoit/85597c9afd11ea7d3781ea68d154bb39 to your computer and use it in GitHub Desktop.
Save hpoit/85597c9afd11ea7d3781ea68d154bb39 to your computer and use it in GitHub Desktop.
Weiqi1 - milestone
julia> abstract type Stone end
julia> struct Black <: Stone end
julia> struct White <: Stone end
julia> abstract type Emptiness end
julia> struct Empty <: Emptiness end
# julia> Base.show(io::IO, ::Empty) = print(io, "·")
# julia> Base.show(io::IO, ::Black) = print(io, "⚈")
# julia> Base.show(io::IO, ::White) = print(io, "⚆")
julia> mutable struct Board <: AbstractMatrix{Union{Stone, Emptiness}}
array::Matrix{Union{Stone, Emptiness}}
end
julia> Base.size(board::Board) = size(board.array)
julia> Base.getindex(board::Board, i) = board.array[i]
julia> Base.IndexStyle(::Board) = IndexLinear()
julia> empty = Empty();
julia> black = Black();
julia> white = White();
julia> createboard(magnitude) = Board(Matrix{Empty}(magnitude, magnitude))
createboard (generic function with 1 method)
julia> magnitude = 19; cb = createboard(magnitude);
julia> abstract type Player end
julia> struct Blackplayer <: Player end
julia> struct Whiteplayer <: Player end
julia> bp = Blackplayer()
Blackplayer()
julia> wp = Whiteplayer()
Whiteplayer()
julia> mutable struct NewPosition{T<:Player}
player::T
coords::Tuple{Int64, Int64}
stone::Stone
end
julia> np = NewPosition(bp, (1,2), black)
NewPosition{Blackplayer}(Blackplayer(), (1, 2), ⚈)
julia> np = NewPosition(wp, (1,2), white)
NewPosition{Whiteplayer}(Whiteplayer(), (1, 2), ⚆)
julia> cb;
julia> cb.array[1,2] = white;
julia> cb;
julia> cb.array[1,2]
julia> function pass(np)
if np.player == bp; np.coords == [0,0]
pass = Black
elseif np.player == wp; np.coords == [0,0]
pass = White
else
println("No passes")
end
end
pass (generic function with 2 methods)
julia> np = NewPosition(bp, (0,0), black)
NewPosition{Blackplayer}(Blackplayer(), (0, 0), ⚈)
julia> pass(np)
Black
julia> np = NewPosition(bp, (1,1), black)
NewPosition{Blackplayer}(Blackplayer(), (1, 1), ⚈)
julia> pass(np)
Black
julia> cb.array[1:19,1:19] = empty;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment