Anonymous user
Flipping bits game: Difference between revisions
→{{header|Go}}: , gofmt, cleanup; mark as still needing improvement
(Now code ensures their is a solution) |
(→{{header|Go}}: , gofmt, cleanup; mark as still needing improvement) |
||
Line 891:
=={{header|Go}}==
{{improve|Go|Really should not be hard coded for 3×3 (or any fixed size) as the task requests arbitrary N×N (for Go that also means using a slice instead of an array of arrays).}}
The code just works for N=3, but could easily be changed to accommodate higher Ns.
<lang go>package main
import (
"bytes"
"fmt"
"math/rand"
"time"
)
Line 909 ⟶ 906:
func main() {
rand.Seed(time.Now().UnixNano())
current := target
for i := rand.Intn(100); i > 0 || target == current; i-- {
current.flipRow(rand.Intn(3) + 1)
}
var moves int
for target != current {
fmt.Println("Target:")
fmt.Println(&target)
fmt.Println("\nBoard:")
fmt.Println(¤t)
fmt.Print("Flip row (r) or column (c) 1 .. 3 (c1, r3, ...): ")
var rc rune
_, err := fmt.Scanf("%c%d", &rc, &num)
}
if num < 1 || num > 3 {
fmt.Println("Wrong command!")
continue
}
switch
case 'r':
fmt.Printf("Row %v will be flipped\n", num)
current.flipRow(num)
default:
fmt.Println("Wrong command!")
}
moves++
fmt.Println("\nMoves taken: ", moves)
}
fmt.Print("You win after ")
if moves == 1 {
fmt.Println("a single move!")
} else {
fmt.Println(moves, "moves!")
}
}
func
var b bytes.Buffer
fmt.Fprint(&b, " 1 2 3")
for i := range m {
fmt.Fprintln(&b)
for _, val := range m[i] {
fmt.Fprint(&b, val, " ")
}
}
return b.String()
}
func randMatrix() matrix {
var randMat matrix
for i := range randMat {
randMat[i][j] = rand.Intn(2)
}
}
Line 1,003 ⟶ 980:
}
func
for
}
}
func
for i :=
}
}</lang>
{{out}}
<pre>
Target:
1 2 3
1 1 0 0
2 1 1 0
3 1 0 0
Board:
1 2 3
1 1 0 1
2 0 0
3
Flip row (r) or column (c) 1 .. 3 (c1, r3, ...): r2
Row 2 will be flipped
Target:
1 2 3
1 1 0 0
2 1 1 0
3 1 0 0
Board:
1 2 3
1
2 1
3 1 0 1
Flip row (r) or column (c) 1 .. 3 (c1, r3, ...): c3
Column 3 will be flipped
You win after 2 moves!
</pre>
|