Skip to content

Instantly share code, notes, and snippets.

@hpoit
Last active January 9, 2018 16:59
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/4aad128ee4ade8e4246ab9502b224330 to your computer and use it in GitHub Desktop.
Save hpoit/4aad128ee4ade8e4246ab9502b224330 to your computer and use it in GitHub Desktop.
go board
abstract type Stone end
struct Empty <: Stone end
struct Black <: Stone end
struct White <: Stone end
Base.show(io::IO, ::Black) = print(io, "⚈")
Base.show(io::IO, ::White) = print(io, "⚆")
Base.show(io::IO, ::Empty) = print(io, "·")
mutable struct Board <: AbstractMatrix{Stone}
array::Matrix{Stone}
end
Base.size(board::Board) = size(board.array)
Base.getindex(board::Board, i) = board.array[i]
Base.IndexStyle(::Board) = IndexLinear()
empty = Empty()
black = Black()
white = White()
createboard(s) = Board(Matrix{Empty}(s, s))
s = 19
b = createboard(s)
b.array[2:12, 1:14] = black
b.array[4:9, 4:10] = white
b
function flip(b)
for celln in 1:length(b)
if b.array[celln] == white
b.array[celln] = black
elseif b.array[celln] == black
b.array[celln] = white
end
end
end
flip(b)
b
createboard(s) = Board(Matrix{Empty}(s, s))
s = 19
b.array = createboard(s)
b.array[8, 8] = white
b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment