-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathField.go
52 lines (48 loc) · 1.08 KB
/
Field.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
package main
import "strings"
const player1 int = 0
const player2 int = 1
const empty int = 2
const blocked int = 3
// Field class
type Field struct {
width, height int
cells [][]int // I will use [row][col], not x,y
}
func (f *Field) initField(h, w int) {
f.width = w
f.height = h
f.cells = make([][]int, f.height)
for row := 0; row < f.height; row++ {
f.cells[row] = make([]int, f.width) // default initialization with 0s
for col := 0; col < f.width; col++ {
f.cells[row][col] = empty
}
}
}
func stringToInt(s string) int {
switch s {
case ".":
return empty
case "0":
return player1
case "1":
return player2
default:
return blocked
}
}
func (f *Field) parse(text string) {
values := strings.Split(text, ",")
for row := 0; row < f.height; row++ {
for col := 0; col < f.width; col++ {
f.cells[row][col] = stringToInt(values[row*f.height+col])
}
}
}
func (f Field) isValid(row, col int) bool {
isValid := row >= 0 && row < f.height
isValid = isValid && (col >= 0 && col < f.width)
isValid = isValid && f.cells[row][col] == empty
return isValid
}