Flipping bits game: Difference between revisions

Content added Content deleted
(→‎{{header|Scala}}: Output or reason for omission.)
Line 1,779: Line 1,779:


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}

<lang julia>module FlippingBitsGame
<lang julia>module FlippingBitsGame


using Compat
using Printf, Random
import Base.size, Base.show, Base.==
using Compat.Printf
using Compat.Random

struct Configuration
struct Configuration
M::BitMatrix
M::BitMatrix
end
end

Base.size(c::Configuration) = size(c.M)
Base.size(c::Configuration) = size(c.M)
function Base.show(io::IO, conf::Configuration)
function Base.show(io::IO, conf::Configuration)
Line 1,810: Line 1,807:
end
end
Base.:(==)(a::Configuration, b::Configuration) = a.M == b.M
Base.:(==)(a::Configuration, b::Configuration) = a.M == b.M

struct Index{D}
struct Index{D}
i::Int
i::Int
Line 1,816: Line 1,813:
const ColIndex = Index{:C}
const ColIndex = Index{:C}
const RowIndex = Index{:R}
const RowIndex = Index{:R}

function Base.flipbits!(conf::Configuration, c::ColIndex)
function flipbits!(conf::Configuration, c::ColIndex)
col = @view conf.M[:, c.i]
col = @view conf.M[:, c.i]
@. col = !col
@. col = !col
return conf
return conf
end
end
function Base.flipbits!(conf::Configuration, r::RowIndex)
function flipbits!(conf::Configuration, r::RowIndex)
row = @view conf.M[r.i, :]
row = @view conf.M[r.i, :]
@. row = !row
@. row = !row
return conf
return conf
end
end
randomconfig(nrow::Integer, ncol::Integer) = Configuration(bitrand(nrow, ncol))


randomconfig(nrow::Integer, ncol::Integer) = Configuration(bitrand(nrow, ncol))
function randommoves!(conf::Configuration, nflips::Integer)
function randommoves!(conf::Configuration, nflips::Integer)
nrow, ncol = size(conf)
nrow, ncol = size(conf)
Line 1,840: Line 1,838:
return conf
return conf
end
end

function play()
function play()
nrow::Int, ncol::Int = 0, 0
nrow::Int, ncol::Int = 0, 0
Line 1,876: Line 1,874:
return
return
end
end
end # module FlippingBitsGame


end # module FlippingBitsGame</lang>
using .FlippingBitsGame


FlippingBitsGame.play()
{{output}}
</lang>{{output}}
<pre>Insert the size of the matrix (nrow [> 1] *space* ncol [> 1]):3 3
<pre>Insert the size of the matrix (nrow [> 1] *space* ncol [> 1]):3 3