Skip to content

Commit 9bbb8f4

Browse files
committed
Pre-First test POC
1 parent 6c40287 commit 9bbb8f4

File tree

3 files changed

+175
-50
lines changed

3 files changed

+175
-50
lines changed

birdc.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"bytes"
54
"encoding/binary"
65
"flag"
76
"fmt"
@@ -12,7 +11,7 @@ import (
1211
"strconv"
1312
"strings"
1413

15-
"github.com/nareix/bits"
14+
"github.com/bamiaux/iobit"
1615
)
1716

1817
type bgpCommunity struct {
@@ -58,13 +57,13 @@ S = Hit or Miss on last move
5857
+-------------------------------+
5958
*/
6059

61-
func numberToBitReader(in uint16) *bits.Reader {
60+
func numberToBitReader(in uint16) iobit.Reader {
6261
actually := uint16(in)
6362

6463
bits2 := make([]byte, 2)
6564

66-
binary.LittleEndian.PutUint16(bits2, actually)
67-
return &bits.Reader{R: bytes.NewReader(bits2)}
65+
binary.BigEndian.PutUint16(bits2, actually)
66+
return iobit.NewReader(bits2)
6867
}
6968

7069
var errNotEnoughData = fmt.Errorf("Not enough data to make a move")
@@ -80,7 +79,7 @@ func readBGP() (gameIncrementor, X, Y, HitOrMissOnLast int, err error) {
8079
if community.AS == uint16(*communityAS) {
8180
// okay, so we are now interested!
8281
r := numberToBitReader(community.Data)
83-
t, _ := r.ReadBits(2)
82+
t := r.Uint8(2)
8483

8584
if t == 1 {
8685
// Counter
@@ -89,7 +88,7 @@ func readBGP() (gameIncrementor, X, Y, HitOrMissOnLast int, err error) {
8988
return 0, 0, 0, 0, errDupeType
9089
}
9190
readCounter = true
92-
c, _ := r.ReadBits(14)
91+
c := r.Uint16(14)
9392
gameIncrementor = int(c)
9493

9594
} else if t == 2 {
@@ -98,11 +97,11 @@ func readBGP() (gameIncrementor, X, Y, HitOrMissOnLast int, err error) {
9897
return 0, 0, 0, 0, errDupeType
9998
}
10099
readPosition = true
101-
xp, _ := r.ReadBits(4)
100+
xp := r.Uint16(4)
102101
X = int(xp)
103-
yp, _ := r.ReadBits(4)
102+
yp := r.Uint16(4)
104103
Y = int(yp)
105-
hs, _ := r.ReadBits(1)
104+
hs := r.Uint16(1)
106105
HitOrMissOnLast = int(hs)
107106

108107
} else {
@@ -120,34 +119,32 @@ func readBGP() (gameIncrementor, X, Y, HitOrMissOnLast int, err error) {
120119
func writeBGP(gameIncrementor, X, Y, HitOrMissOnLast int) error {
121120

122121
counternumberbytes := make([]byte, 2)
123-
counternumberbytesbuffer := bytes.NewBuffer(counternumberbytes)
124-
counternumberbits := bits.Writer{
125-
W: counternumberbytesbuffer,
126-
}
122+
counternumberbits := iobit.NewWriter(counternumberbytes)
127123

128-
counternumberbits.WriteBits(1, 2)
129-
counternumberbits.WriteBits(uint(gameIncrementor), 14)
124+
counternumberbits.PutUint16(2, 0x1)
125+
counternumberbits.PutUint16(14, uint16(gameIncrementor))
126+
counternumberbits.Flush()
130127

131-
counterCommunity := binary.LittleEndian.Uint16(counternumberbytes)
128+
counterCommunity := binary.BigEndian.Uint16(counternumberbytes)
132129

133130
positionnumberbytes := make([]byte, 2)
134-
positionnumberbytesbuffer := bytes.NewBuffer(positionnumberbytes)
135-
positionnumberbits := bits.Writer{
136-
W: positionnumberbytesbuffer,
137-
}
131+
positionnumberbits := iobit.NewWriter(positionnumberbytes)
132+
133+
positionnumberbits.PutUint16(2, 0x2)
134+
positionnumberbits.PutUint16(4, uint16(X))
135+
positionnumberbits.PutUint16(4, uint16(Y))
136+
positionnumberbits.PutBit(HitOrMissOnLast == 1)
137+
positionnumberbits.Flush()
138138

139-
positionnumberbits.WriteBits(2, 2)
140-
positionnumberbits.WriteBits(uint(X), 4)
141-
positionnumberbits.WriteBits(uint(Y), 4)
142-
positionnumberbits.WriteBits(uint(HitOrMissOnLast), 1)
139+
fmt.Printf("%x/%x", counternumberbytes, positionnumberbytes)
143140

144-
positionCommunity := binary.LittleEndian.Uint16(positionnumberbytes)
141+
positionCommunity := binary.BigEndian.Uint16(positionnumberbytes)
145142

146143
// Now we have the two community strings counterCommunity and positionCommunity
147144

148145
templatestring := fmt.Sprintf(
149146
"\nbgp_community.add((%d,%d));\nbgp_community.add((%d,%d));\n",
150-
communityAS, positionCommunity, communityAS, counterCommunity)
147+
*communityAS, positionCommunity, *communityAS, counterCommunity)
151148

152149
templateBytes, err := ioutil.ReadFile(*templatePath)
153150
if err != nil {
@@ -170,7 +167,7 @@ func writeBGP(gameIncrementor, X, Y, HitOrMissOnLast int) error {
170167

171168
defer conn.Close()
172169

173-
conn.Write([]byte(fmt.Sprintf("reload all\n")))
170+
conn.Write([]byte(fmt.Sprintf("configure\n")))
174171

175172
buffer := make([]byte, 90000)
176173
_, err = conn.Read(buffer)

board.go

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package main
22

33
import (
4+
cr "crypto/rand"
45
"fmt"
6+
"math"
7+
"math/big"
8+
"math/rand"
59
"strconv"
610
"strings"
711

@@ -23,16 +27,18 @@ type battleShipBoard struct {
2327
Board [10][10]boardState
2428
}
2529

26-
func (b *battleShipBoard) Draw() {
27-
fmt.Print("_|A|B|C|D|E|F|G|H|I|J|_\n")
30+
func (b *battleShipBoard) Draw() string {
31+
str := ""
32+
str += fmt.Sprint("_|A|B|C|D|E|F|G|H|I|J|_\n")
2833
for y, stripe := range b.Board {
29-
fmt.Printf("%d|", y)
34+
str += fmt.Sprintf("%d|", y)
3035
for _, x := range stripe {
31-
fmt.Printf("%s|", x.Draw())
36+
str += fmt.Sprintf("%s|", x.Draw())
3237
}
33-
fmt.Printf("%d\n", y)
38+
str += fmt.Sprintf("%d\n", y)
3439
}
35-
fmt.Print("_|A|B|C|D|E|F|G|H|I|J|_\n")
40+
str += fmt.Sprint("_|A|B|C|D|E|F|G|H|I|J|_\n")
41+
return str
3642
}
3743

3844
var cblack = ansi.ColorCode("black+h:black")
@@ -76,3 +82,72 @@ func cordsToNumbers(in string) (X, Y int) {
7682
}
7783
return -1, -1
7884
}
85+
86+
func combineBoard(b1, b2 battleShipBoard) string {
87+
b1r, b2r := b1.Draw(), b2.Draw()
88+
89+
b1rs, b2rs := strings.Split(b1r, "\n"), strings.Split(b2r, "\n")
90+
91+
str := ""
92+
for k, _ := range b1rs {
93+
str += fmt.Sprintf("%s %s\n", b1rs[k], b2rs[k])
94+
}
95+
96+
return str
97+
}
98+
99+
func makeBoard() battleShipBoard {
100+
a := battleShipBoard{}
101+
ri, _ := cr.Int(cr.Reader, big.NewInt(math.MaxInt64))
102+
rand.Seed(ri.Int64())
103+
104+
a = placeShip(5, a)
105+
a = placeShip(4, a)
106+
a = placeShip(3, a)
107+
a = placeShip(3, a)
108+
a = placeShip(2, a)
109+
return a
110+
}
111+
112+
func placeShip(size int, bo battleShipBoard) battleShipBoard {
113+
114+
for {
115+
board := bo
116+
sideways := rand.Int() % 2
117+
118+
if sideways == 0 { // ship goes up
119+
X := rand.Int() % 10
120+
Y := rand.Int() % 10
121+
if Y+size > 10 {
122+
continue
123+
}
124+
125+
for y := Y; y < Y+size; y++ {
126+
if board.Board[y][X] != stateEmpty {
127+
continue
128+
}
129+
board.Board[y][X] = stateShip
130+
}
131+
bo = board
132+
break
133+
} else {
134+
X := rand.Int() % 10
135+
Y := rand.Int() % 10
136+
137+
if X+size > 10 {
138+
continue
139+
}
140+
141+
for x := X; x < X+size; x++ {
142+
if board.Board[Y][x] != stateEmpty {
143+
continue
144+
}
145+
board.Board[Y][x] = stateShip
146+
}
147+
bo = board
148+
break
149+
}
150+
}
151+
152+
return bo
153+
}

main.go

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,88 @@ import (
66
"fmt"
77
"log"
88
"os"
9+
"time"
910
)
1011

1112
func main() {
13+
startfirst := flag.Bool("startfirst", false, "set this if you are starting first")
1214
flag.Parse()
1315
log.Printf("yup")
1416

15-
LocalB := battleShipBoard{}
16-
// RemoteB := battleShipBoard{}
17-
18-
LocalB.Board[1][5] = stateShip
19-
LocalB.Board[1][2] = stateHit
20-
LocalB.Board[2][2] = stateAttempt
17+
LocalB := makeBoard()
18+
RemoteB := battleShipBoard{}
2119

2220
LocalB.Draw()
2321

22+
gameCounter := 0
23+
hitmiss := 0
24+
25+
fmt.Print("Your Side Player Two\n")
26+
fmt.Print(combineBoard(LocalB, RemoteB))
27+
2428
reader := bufio.NewReader(os.Stdin)
2529
for {
26-
fmt.Print("Next Move> ")
27-
text, _ := reader.ReadString('\n')
28-
if len(text) != 3 {
29-
log.Printf("wrong length of command %d", len(text))
30-
continue
30+
var text string
31+
var x, y int
32+
if *startfirst {
33+
fmt.Printf("[%06d] Next Move> ", gameCounter)
34+
text, _ = reader.ReadString('\n')
35+
if len(text) != 3 {
36+
log.Printf("wrong length of command %d", len(text))
37+
continue
38+
}
39+
x, y = cordsToNumbers(text)
40+
if x == -1 || y == -1 {
41+
continue
42+
}
43+
44+
fmt.Printf("Firing on %s...", text)
45+
writeBGP(gameCounter, x, y, hitmiss)
3146
}
32-
x, y := cordsToNumbers(text)
33-
if x == -1 || y == -1 {
34-
continue
47+
48+
*startfirst = true
49+
50+
fmt.Printf("waiting on players responce...\n")
51+
52+
for {
53+
time.Sleep(time.Second)
54+
var err error
55+
var nx, ny int
56+
tempgameCounter := 0
57+
tempgameCounter, nx, ny, hitmiss, err = readBGP()
58+
if err != nil {
59+
fmt.Print(".")
60+
continue
61+
}
62+
fmt.Print("!")
63+
64+
if tempgameCounter > gameCounter {
65+
gameCounter = tempgameCounter + 1
66+
// !! New move has happened
67+
68+
// First, process if we got a hit or not.
69+
if hitmiss == 1 {
70+
RemoteB.Board[y][x] = stateHit
71+
log.Printf("It's a Hit!")
72+
} else {
73+
RemoteB.Board[y][x] = stateAttempt
74+
log.Printf("It's a Miss!")
75+
}
76+
77+
// Now... did we get hit?
78+
if LocalB.Board[ny][nx] == stateShip {
79+
hitmiss = 1
80+
LocalB.Board[ny][nx] = stateHit
81+
} else {
82+
hitmiss = 0
83+
LocalB.Board[ny][nx] = stateAttempt
84+
}
85+
break
86+
}
3587
}
36-
LocalB.Board[y][x] = stateHit
37-
LocalB.Draw()
88+
89+
fmt.Print("Your Side Player Two\n")
90+
fmt.Print(combineBoard(LocalB, RemoteB))
3891
}
3992

4093
}

0 commit comments

Comments
 (0)