-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob2.go
55 lines (48 loc) · 945 Bytes
/
prob2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
/*
* Print out probability of 1, randomly-chosen, knight's move starting from
* each of the 64 squares on the board, staying on the board.
*/
import "fmt"
type board [8][8]int
func main() {
var b board
b1 := onemove(b)
printBoard(b1, 1)
}
func printBoard(bd board, k int) {
for i := 0; i < 8; i++ {
for j := 0; j < 8; j++ {
n := bd[i][j]
fmt.Printf("%2d ", n)
if j == 7 {
// end of row, print newline
fmt.Println()
}
}
}
fmt.Println()
}
var moveIncr = [][2]int{
{-1, 2}, {1, 2},
{-1, -2}, {1, -2},
{2, -1}, {2, 1},
{-2, -1}, {-2, 1},
}
func onemove(bd board) board {
for i := 0; i < 8; i++ {
for j := 0; j < 8; j++ {
for _, increment := range moveIncr {
xNext := i + increment[0]
yNext := j + increment[1]
if xNext >= 0 && xNext <= 7 {
if yNext >= 0 && yNext <= 7 {
// knight at <i,j> still on the board
bd[i][j]++
}
}
}
}
}
return bd
}