Last active
March 5, 2018 15:13
-
-
Save hpoit/3cb145065726eb1451d50542334a504c to your computer and use it in GitHub Desktop.
Weiqi2 - 2nd milestone, thanks to MEastwood at Caltech - func neighbors complete and now func liberties complete!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(m) = Board(Matrix{Empty}(m, m)) | |
createboard (generic function with 1 method) | |
julia> cb = createboard(19); | |
julia> function neighbors(cb, row::Int64, col::Int64) | |
neighbor_list = Tuple{Int, Int}[] | |
if row != 1 | |
push!(neighbor_list, (row-1, col)) | |
end | |
if row != size(cb, 1) | |
push!(neighbor_list, (row+1, col)) | |
end | |
if col != size(cb, 2) | |
push!(neighbor_list, (row, col+1)) | |
end | |
if col != 1 | |
push!(neighbor_list, (row, col-1)) | |
end | |
neighbor_list | |
end | |
neighbors (generic function with 1 method) | |
julia> neighbors(cb, 1, 1) | |
2-element Array{Tuple{Int64,Int64},1}: | |
(2, 1) # south | |
(1, 2) # east | |
julia> neighbors(cb, 19, 19) | |
2-element Array{Tuple{Int64,Int64},1}: | |
(18, 19) # north | |
(19, 18) # west | |
julia> neighbors(cb, 7, 13) | |
4-element Array{Tuple{Int64,Int64},1}: | |
(6, 13) # north | |
(8, 13) # south | |
(7, 14) # east | |
(7, 12) # west | |
julia> function liberties(cb, row::Int64, col::Int64) | |
stone = cb.array[row, col] | |
checked = fill(false, size(cb)) # heap allocation | |
checked[row, col] = true # mark true for visited (row, col) | |
open_set = [] # non-visited nodes | |
closed_set = [] # visited nodes | |
for neighbor ∈ neighbors(cb, row, col) | |
neighbor_row, neighbor_col = neighbor | |
if !checked[neighbor_row, neighbor_col] # if (row, col) not visited | |
if cb.array[neighbor_row, neighbor_col] == stone # if i equals arg | |
push!(open_set, neighbor) | |
elseif cb.array[neighbor_row, neighbor_col] == empty | |
push!(closed_set, neighbor) # liberties | |
end | |
checked[neighbor_row, neighbor_col] = true | |
end | |
end | |
while !isempty(open_set) | |
coords = shift!(open_set) | |
end | |
closed_set # liberties | |
end | |
liberties (generic function with 1 method) | |
julia> cb.array[3:5,3:5] = black; | |
julia> cb | |
19×19 Board: | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · ⚈ ⚈ ⚈ · · · · · · · · · · · · · · | |
· · ⚈ ⚈ ⚈ · · · · · · · · · · · · · · | |
· · ⚈ ⚈ ⚈ · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
· · · · · · · · · · · · · · · · · · · | |
julia> liberties(cb, 4, 5) | |
1-element Array{Any,1}: | |
(4, 6) | |
julia> liberties(cb, 3, 3) | |
2-element Array{Any,1}: | |
(2, 3) | |
(3, 2) | |
julia> liberties(cb, 5, 4) | |
1-element Array{Any,1}: | |
(6, 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment