-
Notifications
You must be signed in to change notification settings - Fork 17
/
board.go
153 lines (127 loc) · 2.72 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
package main
import (
cr "crypto/rand"
"fmt"
"math"
"math/big"
"math/rand"
"strconv"
"strings"
"github.com/mgutz/ansi"
)
type boardState int
const square string = "■"
const (
stateEmpty boardState = iota // 0
stateShip boardState = iota // 1
stateHit boardState = iota // 2
stateAttempt boardState = iota // 3
)
type battleShipBoard struct {
Board [10][10]boardState
}
func (b *battleShipBoard) Draw() string {
str := ""
str += fmt.Sprint("_|A|B|C|D|E|F|G|H|I|J|_\n")
for y, stripe := range b.Board {
str += fmt.Sprintf("%d|", y)
for _, x := range stripe {
str += fmt.Sprintf("%s|", x.Draw())
}
str += fmt.Sprintf("%d\n", y)
}
str += fmt.Sprint("_|A|B|C|D|E|F|G|H|I|J|_\n")
return str
}
var cblack = ansi.ColorCode("black+h:black")
var cship = ansi.ColorCode("black+h:white")
var chit = ansi.ColorCode("red+h:red")
var cattempt = ansi.ColorCode("yellow:yellow")
func (b boardState) Draw() string {
if b == stateEmpty {
return cblack + square + ansi.DefaultBG + ansi.DefaultFG
}
if b == stateShip {
return cship + square + ansi.DefaultBG + ansi.DefaultFG
}
if b == stateHit {
return chit + square + ansi.DefaultBG + ansi.DefaultFG
}
if b == stateAttempt {
return cattempt + square + ansi.DefaultBG + ansi.DefaultFG
}
return ""
}
func cordsToNumbers(in string) (X, Y int) {
in = strings.ToLower(in)
i1 := int(in[0])
if i1 > 96 && i1 < 107 {
X = i1 - 97
} else {
return -1, -1
}
i2, err := strconv.ParseInt(string(in[1]), 10, 64)
if err != nil {
return -1, -1
}
if i2 < 11 {
return X, int(i2)
}
return -1, -1
}
func combineBoard(b1, b2 battleShipBoard) string {
b1r, b2r := b1.Draw(), b2.Draw()
b1rs, b2rs := strings.Split(b1r, "\n"), strings.Split(b2r, "\n")
str := ""
for k, _ := range b1rs {
str += fmt.Sprintf("%s %s\n", b1rs[k], b2rs[k])
}
return str
}
func makeBoard() battleShipBoard {
a := battleShipBoard{}
ri, _ := cr.Int(cr.Reader, big.NewInt(math.MaxInt64))
rand.Seed(ri.Int64())
a = placeShip(5, a)
a = placeShip(4, a)
a = placeShip(3, a)
a = placeShip(3, a)
a = placeShip(2, a)
return a
}
func placeShip(size int, bo battleShipBoard) battleShipBoard {
for {
board := bo
sideways := rand.Int() % 2
if sideways == 0 { // ship goes up
X := rand.Int() % 10
Y := rand.Int() % 10
if Y+size > 10 {
continue
}
for y := Y; y < Y+size; y++ {
if board.Board[y][X] != stateEmpty {
continue
}
board.Board[y][X] = stateShip
}
bo = board
break
} else {
X := rand.Int() % 10
Y := rand.Int() % 10
if X+size > 10 {
continue
}
for x := X; x < X+size; x++ {
if board.Board[Y][x] != stateEmpty {
continue
}
board.Board[Y][x] = stateShip
}
bo = board
break
}
}
return bo
}