Flipping bits game: Difference between revisions

Content added Content deleted
(→‎{{header|Go}}: Incorrect.)
(Now code ensures their is a solution)
Line 891: Line 891:


=={{header|Go}}==
=={{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.
The code just works for N=3, but could easily be changed to accomodate higher Ns.


Line 913: Line 913:
var s,t string
var s,t string
var moves int
var moves int
moves = 1
target = randMatrix()
target = randMatrix()
current = randMatrix()
current = target
for i:=0;i<rand.Intn(100);i++ {
current = flipCol(current, rand.Intn(2)+1)
current = flipRow(current, rand.Intn(2)+1)
}
fmt.Println("Target:")
fmt.Println("Target:")
Line 944: Line 948:
}
}
moves++
moves++
u, _ := strconv.ParseInt(t,0,64)
u, _ := strconv.ParseInt(t,0,0)
v := int(u)
if u>3 {
if u>3 {
fmt.Println("Wrong command!")
fmt.Println("Wrong command!")
Line 950: Line 955:
}
}
switch s {
switch s {
case "c": {
case "c": {
fmt.Printf("Column %s will be flipped\n", t)
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": {
case "r": {
fmt.Printf("Row %s will be flipped\n", t)
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: {
default: {
Line 1,008: Line 1,001:
}
}
return randMat
return randMat
}

func flipRow(m matrix, row int) matrix {
for i:=0;i<3;i++ {
if m[row-1][i]==0 {
m[row-1][i]=1
} else {
m[row-1][i]=0
}
}
return m
}

func flipCol(m matrix, col int) matrix {
for i:=0;i<3;i++ {
if m[i][col-1]==0 {
m[i][col-1]=1
} else {
m[i][col-1]=0
}
}
return m
}
}
</lang>
</lang>