Jump to content

Flipping bits game: Difference between revisions

m
(→‎{{header|Scala}}: Output or reason for omission.)
Line 1,779:
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<lang julia>module FlippingBitsGame
 
using CompatPrintf, Random
import Base.size, Base.show, Base.==
using Compat.Printf
using Compat.Random
 
struct Configuration
M::BitMatrix
end
 
Base.size(c::Configuration) = size(c.M)
function Base.show(io::IO, conf::Configuration)
Line 1,810 ⟶ 1,807:
end
Base.:(==)(a::Configuration, b::Configuration) = a.M == b.M
 
struct Index{D}
i::Int
Line 1,816 ⟶ 1,813:
const ColIndex = Index{:C}
const RowIndex = Index{:R}
 
function Base.flipbits!(conf::Configuration, c::ColIndex)
col = @view conf.M[:, c.i]
@. col = !col
return conf
end
function Base.flipbits!(conf::Configuration, r::RowIndex)
row = @view conf.M[r.i, :]
@. row = !row
return conf
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)
nrow, ncol = size(conf)
Line 1,840 ⟶ 1,838:
return conf
end
 
function play()
nrow::Int, ncol::Int = 0, 0
Line 1,876 ⟶ 1,874:
return
end
end # module FlippingBitsGame
 
end # moduleusing .FlippingBitsGame</lang>
 
FlippingBitsGame.play()
</lang>{{output}}
<pre>Insert the size of the matrix (nrow [> 1] *space* ncol [> 1]):3 3
 
4,105

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.