Flipping bits game: Difference between revisions
Content added Content deleted
(Now code ensures their is a solution) |
(→{{header|Go}}: , gofmt, cleanup; mark as still needing improvement) |
||
Line 891: | Line 891: | ||
=={{header|Go}}== |
=={{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 ( |
import ( |
||
"bytes" |
|||
"fmt" |
"fmt" |
||
"math/rand" |
"math/rand" |
||
"time" |
"time" |
||
"bufio" |
|||
"os" |
|||
"strconv" |
|||
) |
) |
||
Line 909: | Line 906: | ||
func main() { |
func main() { |
||
rand.Seed(time.Now().UnixNano()) |
|||
target := randMatrix() |
|||
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) + 1) |
|||
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(¤t) |
|||
fmt.Print("Flip row (r) or column (c) 1 .. 3 (c1, r3, ...): ") |
fmt.Print("Flip row (r) or column (c) 1 .. 3 (c1, r3, ...): ") |
||
scanner := bufio.NewScanner(os.Stdin) |
|||
var rc rune |
|||
scanner.Split(bufio.ScanBytes) |
|||
var num int |
|||
_, err := fmt.Scanf("%c%d", &rc, &num) |
|||
for scanner.Scan () { |
|||
if err != nil { |
|||
fmt.Println(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!") |
fmt.Println("Wrong command!") |
||
continue |
continue |
||
} |
} |
||
switch |
switch rc { |
||
case 'c': |
|||
fmt.Printf("Column %v will be flipped\n", num) |
|||
current.flipCol(num) |
|||
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!") |
|||
} |
|||
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 |
func (m *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.Fprint(&b, i+1, " ") |
|||
for _, val := range m[i] { |
|||
if j==2 { fmt.Print("\n") } |
|||
fmt.Fprint(&b, val, " ") |
|||
} |
} |
||
} |
} |
||
return b.String() |
|||
} |
} |
||
func randMatrix() matrix { |
func randMatrix() matrix { |
||
var randMat matrix |
var randMat matrix |
||
for i := range randMat { |
|||
rand.Seed(time.Now().UTC().UnixNano()) |
|||
for j := range randMat[i] { |
|||
randMat[i][j] = rand.Intn(2) |
|||
for j:=0;j<3;j++ { |
|||
randMat[i][j]=rand.Intn(2) |
|||
} |
} |
||
} |
} |
||
Line 1,003: | Line 980: | ||
} |
} |
||
func |
func (m *matrix) flipRow(row int) { |
||
for |
for j := range m[row-1] { |
||
m[row-1][j] ^= 1 |
|||
m[row-1][i]=1 |
|||
} else { |
|||
m[row-1][i]=0 |
|||
} |
|||
} |
} |
||
return m |
|||
} |
} |
||
func |
func (m *matrix) flipCol(col int) { |
||
for i:= |
for i := range m { |
||
m[i][col-1] ^= 1 |
|||
m[i][col-1]=1 |
|||
} else { |
|||
m[i][col-1]=0 |
|||
} |
|||
} |
} |
||
}</lang> |
|||
return m |
|||
{{out}} |
|||
} |
|||
</lang> |
|||
Example: |
|||
<pre> |
<pre> |
||
Target: |
Target: |
||
1 2 3 |
|||
1 1 0 0 |
|||
2 1 1 0 |
|||
3 1 0 0 |
|||
Board: |
|||
1 2 3 |
1 2 3 |
||
1 1 0 1 |
1 1 0 1 |
||
2 0 0 |
2 0 0 0 |
||
3 |
3 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: |
Board: |
||
1 2 3 |
1 2 3 |
||
1 |
1 1 0 1 |
||
2 1 |
2 1 1 1 |
||
3 1 0 1 |
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> |
</pre> |
||