-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (109 loc) · 2.26 KB
/
main.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
// Point represents an x-y coordinate
type Point struct {
X, Y int
}
// NewPoint returns a new instance of Point
func NewPoint(x, y int) *Point {
point := Point{x, y}
return &point
}
// CellState defines the different states a cell can be in
type CellState int
const (
unknown = iota
miss = iota
hit = iota
sunk = iota
)
// Cell is used to represent a single location on the grid
type Cell struct {
location Point
state CellState
}
// GetCellDisplay returns a string representation for a cell
func (c Cell) GetCellDisplay() string {
var retval string
switch c.state {
case unknown:
retval = "."
case miss:
retval = "x"
case hit:
retval = "X"
case sunk:
retval = "*"
}
return retval
}
// NewCell return a new instance of Cell
func NewCell(location Point, state CellState) *Cell {
cell := Cell{location, state}
return &cell
}
// Grid contains the playing field
type Grid struct {
cells [][]Cell
}
func create(x, y int) *Grid {
grid := make([][]Cell, y)
for cnty := range grid {
grid[y] = make([]Cell, x)
for cntx := range grid[cnty] {
grid[cnty][cntx] = *NewCell(*NewPoint(x, y), unknown)
}
}
return &Grid{grid}
}
// Game contains all the information pertaining to the current game
type Game struct {
grid Grid
}
func (g Game) init(filename string) {
lines, err := readFile("battleships.cfg")
if err != nil {
fmt.Println("Error reading battleships.cfg: %s", err)
}
for index, element := range lines {
//fmt.Println(index, element)
var x, y int
if index == 0 {
s := strings.Split(element, ",")
x, _ = strconv.Atoi(s[0])
y, _ = strconv.Atoi(s[1])
fmt.Println(x, y)
newgrid := create(x, y)
g.grid = *newgrid
//g.grid.cells = make([][]Cell, x*y)
}
}
}
// readFile reads a whole file into memory
// and returns a slice of its lines.
func readFile(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
// Entry point
func main() {
fmt.Println("Battleships Solver")
fmt.Println()
var g Game
g.init("battleships.bss")
}