-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.go
More file actions
129 lines (109 loc) · 2.72 KB
/
board.go
File metadata and controls
129 lines (109 loc) · 2.72 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package randomart
import (
"errors"
)
// Board represents the state of the randomart grid as it is being explored.
type Board struct {
data []uint8
dimX, dimY int
start, end position
pos position
}
type position struct{ x, y int }
// NewBoard initializes a Board of dimensions x, y with a starting position of
// the center point.
//
// x 0→n, y 0↓n.
func NewBoard(x, y int) (*Board, error) {
if x <= 0 || y <= 0 {
return nil, errors.New("invalid dimensions")
}
data := make([]uint8, x*y)
b := Board{
data: data,
dimX: x,
dimY: y,
}
err := b.setStartPos(x/2, y/2)
return &b, err
}
func (b *Board) setStartPos(x, y int) error {
if x >= b.dimX || x < 0 || y >= b.dimY || y < 0 {
return errors.New("invalid start position")
}
b.pos = position{x: x, y: y}
b.start = position{x: x, y: y}
return nil
}
// Write writes len(p) bytes to the underlying Board. The provided fingerprint
// will be used to explore the board using the drunken bishop algorithm.
//
// Implements the io.Writer interface. The returned number of bytes will always
// equal len(fingerprint), and the error will always be nil
func (b *Board) Write(fingerprint []byte) (n int, err error) {
// leave breadcrumb at start position
b.increment(b.pos.x, b.pos.y)
for _, fingerByte := range fingerprint { // for each byte of fingerprint
for s := uint(0); s < 8; s += 2 { // stride byte in 2 bit chunks
// bitwise right shift the byte by s, which results in last 2 bits
// being the ones we want to analyze. then take the arithmetic AND
// of int 3 (0b11), in order to effectively extract the value of
// those last two bits.
//
// elegant bitshift solution found via:
// https://github.com/calmh/randomart/blob/master/randomart.go
d := (fingerByte >> s) & 0b11
// move in direction
switch d {
case 0: // 0b00 ↖
b.moveLeft()
b.moveUp()
case 1: // 0b01 ↗
b.moveRight()
b.moveUp()
case 2: // 0b10 ↙
b.moveLeft()
b.moveDown()
case 3: // 0b11 ↘
b.moveRight()
b.moveDown()
}
// mark breadcrumb after move
b.increment(b.pos.x, b.pos.y)
}
}
b.end = b.pos
return len(fingerprint), nil
}
// move left if possible
func (b *Board) moveLeft() {
if b.pos.x > 0 {
b.pos.x--
}
}
// move right if possible
func (b *Board) moveRight() {
if b.pos.x < b.dimX-1 {
b.pos.x++
}
}
// move up if possible
func (b *Board) moveUp() {
if b.pos.y > 0 {
b.pos.y--
}
}
// move down if possible
func (b *Board) moveDown() {
if b.pos.y < b.dimY-1 {
b.pos.y++
}
}
// increment the value at the given position
func (b *Board) increment(x, y int) {
b.data[y*b.dimX+x]++
}
// get the value at the given position
func (b *Board) getValue(x, y int) uint8 {
return b.data[y*b.dimX+x]
}