Last active
January 9, 2018 16:59
-
-
Save hpoit/4aad128ee4ade8e4246ab9502b224330 to your computer and use it in GitHub Desktop.
go board
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
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