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
The code just works for N=3, but could easily be changed to accomodate higher Ns.
 
<lang Go>
package main
 
import (
"bytes"
"fmt"
"math/rand"
"time"
"bufio"
"os"
"strconv"
)
 
Line 909 ⟶ 906:
 
func main() {
rand.Seed(time.Now().UnixNano())
var target, current:= matrixrandMatrix()
current := target
var s,t string
 
var moves int
for i := rand.Intn(100); i > 0 || target == current; i-- {
target = randMatrix()
current.flipCol(rand.Intn(3) =+ target1)
current.flipRow(rand.Intn(3) + 1)
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:")
drawBoard(target)
fmt.Println("\nBoard:")
drawBoard(current)
if target == current {
fmt.Println("You win!")
} else {
fmt.Println("Go on, next move!")
}
 
var moves int
for {
for target != current {
fmt.Println("Target:")
fmt.Println(&target)
fmt.Println("\nBoard:")
fmt.Println(&current)
fmt.Print("Flip row (r) or column (c) 1 .. 3 (c1, r3, ...): ")
 
scanner := bufio.NewScanner(os.Stdin)
var rc rune
scanner.Split(bufio.ScanBytes)
ivar :=num 0int
_, err := fmt.Scanf("%c%d", &rc, &num)
for scanner.Scan () {
if ierr !==0 {nil {
s = scannerfmt.TextPrintln(err)
} continue
if i==1 {
t = scanner.Text()
}
i++
if i>1 { break }
}
if num < 1 || num > 3 {
moves++
u, _ := strconv.ParseInt(t,0,0)
v := int(u)
if u>3 {
fmt.Println("Wrong command!")
continue
}
switch src {
case "'c"': {
fmt.Printf("Column %sv will be flipped\n", tnum)
current = .flipCol(current, vnum)
case 'r':
}
fmt.Printf("Row %v will be flipped\n", num)
case "r": {
current.flipRow(num)
fmt.Printf("Row %s will be flipped\n", t)
default:
current = flipRow(current, v)
fmt.Println("Wrong command!")
}
default: {continue
fmt.Println("Wrong command!")
continue
}
}
fmt.Println("\nMoves taken: ", moves)
fmt.Println("Target:")
drawBoard(target)
fmt.Println("Board:")
drawBoard(current)
if target == current {
fmt.Println("You win!")
break
}
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 drawBoard(todrawm *matrix) String() string {
var b bytes.Buffer
fmt.Println(" 1 2 3")
fmt.Fprint(&b, " 1 2 3")
for i:=0;i<3;i++ {
for i := range m {
fmt.Print(i+1, " ")
fmt.Fprintln(&b)
for j:=0;j<3;j++ {
fmt.PrintFprint(todraw[&b, i][j]+1, " ")
for _, val := range m[i] {
if j==2 { fmt.Print("\n") }
fmt.Fprint(&b, val, " ")
}
}
return b.String()
}
 
func randMatrix() matrix {
var randMat matrix
for i := range randMat {
rand.Seed(time.Now().UTC().UnixNano())
for ij :=0;i<3; range randMat[i++] {
randMat[i][j] = rand.Intn(2)
for j:=0;j<3;j++ {
randMat[i][j]=rand.Intn(2)
}
}
Line 1,003 ⟶ 980:
}
 
func flipRow(m *matrix,) flipRow(row int) matrix {
for ij :=0;i<3;i++ range m[row-1] {
if m[row-1][ij] ^==0 {1
m[row-1][i]=1
} else {
m[row-1][i]=0
}
}
return m
}
 
func flipCol(m *matrix,) flipCol(col int) matrix {
for i :=0;i<3;i++ range m {
if m[i][col-1] ^==0 {1
m[i][col-1]=1
} else {
m[i][col-1]=0
}
}
}</lang>
return m
{{out}}
}
</lang>
 
Example:
 
<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 10
3 0 1 0 1
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 01 0 1
2 1 01 01
3 1 0 1
Flip row (r) or column (c) 1 .. 3 (c1, r3, ...): c3
Go on, next move!
Column 3 will be flipped
Flip row (r) or column (c) 1 .. 3 (c1, r3, ...):
You win after 2 moves!
</pre>
 
Anonymous user