This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
/
board.go
167 lines (139 loc) · 3.14 KB
/
board.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package tetris
import (
"fmt"
"strconv"
"strings"
)
type Board struct {
width int
height int
data [][]bool
}
func NewBoard(width, height int) (*Board, error) {
if (width < 1) || (height < 1) {
err := fmt.Errorf("Width and Height must both be greater than 0")
return nil, err
}
board := &Board{width, height, make([][]bool, height)}
for i := 0; i < height; i++ {
board.data[i] = make([]bool, width)
}
return board, nil
}
func (b *Board) Width() int {
return b.width
}
func (b *Board) Height() int {
return b.height
}
// Used internally to check that the given row and column lie within the board's
// boundaries.
func (b *Board) checkBlockRange(row, col int) error {
if (row < 0) || (col < 0) || (row >= b.height) || (col >= b.width) {
err := fmt.Errorf("Block row:%d, col:%d is out of range", row, col)
return err
}
return nil
}
// Returns the set value for a block (true if set, false if not).
func (b *Board) Block(row, col int) (bool, error) {
err := b.checkBlockRange(row, col)
if err != nil {
return false, err
}
return b.data[row][col], nil
}
// Sets the given block to the supplied set value.
func (b *Board) SetBlock(row, col int, value bool) error {
err := b.checkBlockRange(row, col)
if err != nil {
return err
}
b.data[row][col] = value
return nil
}
// Sets an entire rwo to the given value.
func (b *Board) SetRow(row int, value bool) error {
err := b.checkBlockRange(row, 0)
if err != nil {
return err
}
for col := 0; col < b.width; col++ {
b.data[row][col] = value
}
return nil
}
// Sets an entire column to the given value.
func (b *Board) SetCol(col int, value bool) error {
err := b.checkBlockRange(0, col)
if err != nil {
return err
}
for row := 0; row < b.height; row++ {
b.data[row][col] = value
}
return nil
}
// Copies one row (from) to another (to).
func (b *Board) CopyRow(from, to int) {
// TODO check bounds
for col := 0; col < b.Width(); col++ {
b.data[to][col] = b.data[from][col]
}
}
// Creates a copy of the current board.
func (b *Board) Copy() *Board {
c, _ := NewBoard(b.width, b.height)
for row := 0; row < b.height; row++ {
for col := 0; col < b.width; col++ {
c.data[row][col] = b.data[row][col]
}
}
return c
}
// Determines equality between two boards.
func (b *Board) Equal(other *Board) bool {
if b.width != other.width || b.height != other.height {
return false
}
for row := 0; row < b.height; row++ {
for col := 0; col < b.width; col++ {
if b.data[row][col] != other.data[row][col] {
return false
}
}
}
return true
}
// Outputs the board as a string that kinda sorta looks like a tetris board.
func (b *Board) String() string {
var out string = " ┌"
out += strings.Repeat("─", b.width)
out += "┐\n"
for row := 0; row < b.height; row++ {
r := strconv.Itoa(row)
out += r
if row < 10 {
out += " "
}
out += "│"
for col := 0; col < b.width; col++ {
if b.data[row][col] {
out += "X"
} else {
out += " "
}
}
out += "│\n"
}
out += " └"
out += strings.Repeat("─", b.width)
out += "┘\n"
out += " "
for i := 0; i < b.width; i++ {
r := strconv.Itoa(i)
out += r
}
out += "\n"
return out
}