Flipping bits game: Difference between revisions

Now code ensures their is a solution
(→‎{{header|Go}}: Incorrect.)
(Now code ensures their is a solution)
Line 891:
 
=={{header|Go}}==
 
{{incorrect|Go|No attempt to ensure target is reachable from starting position.}}
The code just works for N=3, but could easily be changed to accomodate higher Ns.
 
Line 913:
var s,t string
var moves int
moves = 1
target = randMatrix()
current = randMatrix()target
for i:=0;i<3rand.Intn(100);i++ {
current = flipCol(current, rand.Intn(2)+1)
current = flipRow(current, rand.Intn(2)+1)
}
fmt.Println("Target:")
Line 944 ⟶ 948:
}
moves++
u, _ := strconv.ParseInt(t,0,640)
v := int(u)
if u>3 {
fmt.Println("Wrong command!")
Line 950 ⟶ 955:
}
switch s {
case "c": {
fmt.Printf("Column %s will be flipped\n", t)
current = flipCol(current, v)
for i:=0;i<3;i++ {
if current[i][u-1]==0 {
current[i][u-1]=1
} else {
current[i][u-1]=0
}
}
}
case "r": {
fmt.Printf("Row %s will be flipped\n", t)
current = flipRow(current, v)
for i:=0;i<3;i++ {
if current[u-1][i]==0 {
current[u-1][i]=1
} else {
current[u-1][i]=0
}
}
}
default: {
Line 1,008 ⟶ 1,001:
}
return randMat
}
 
func flipRow(m matrix, row int) matrix {
for i:=0;i<3;i++ {
if currentm[urow-1][i]==0 {
currentm[urow-1][i]=1
} else {
currentm[urow-1][i]=0
}
}
return m
}
 
func flipCol(m matrix, col int) matrix {
for i:=0;i<3;i++ {
if currentm[i][ucol-1]==0 {
currentm[i][ucol-1]=1
} else {
currentm[i][ucol-1]=0
}
}
return m
}
</lang>
Anonymous user