-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
110 lines (89 loc) · 2.1 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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"time"
)
func main() {
startfirst := flag.Bool("startfirst", false, "set this if you are starting first")
resetPls := flag.Bool("reset", false, "reset bird")
flag.Parse()
if *resetPls {
resetBird()
os.Exit(0)
}
log.Printf("Running self test")
testBGPCode()
log.Printf("yup")
LocalB := makeBoard()
RemoteB := battleShipBoard{}
LocalB.Draw()
gameCounter := 0
firstPlay := true
hitmiss := 0
fmt.Print("Your Side Player Two\n")
fmt.Print(combineBoard(LocalB, RemoteB))
reader := bufio.NewReader(os.Stdin)
for {
var text string
var x, y int
if *startfirst {
fmt.Printf("[%06d] Next Move> ", gameCounter)
text, _ = reader.ReadString('\n')
if len(text) != 3 {
log.Printf("wrong length of command %d", len(text))
continue
}
x, y = cordsToNumbers(text)
if x == -1 || y == -1 {
continue
}
fmt.Printf("Firing on %s...", text)
writeBGP(gameCounter, x, y, hitmiss)
firstPlay = false
}
*startfirst = true
fmt.Printf("waiting on players response...\n")
for {
time.Sleep(time.Second)
var err error
var nx, ny int
tempgameCounter := 0
tempgameCounter, nx, ny, hitmiss, err = readBGP()
if err != nil {
fmt.Print("E")
continue
}
fmt.Print(".")
if tempgameCounter > gameCounter || gameCounter == 0 {
gameCounter = tempgameCounter + 1
// !! New move has happened
log.Printf("The other side played a %s%d", string(byte("A"[0])+byte(nx)), ny)
// First, process if we got a hit or not.
if !(firstPlay && *startfirst) {
if hitmiss == 1 {
RemoteB.Board[y][x] = stateHit
log.Printf("It's a Hit!")
} else {
RemoteB.Board[y][x] = stateAttempt
log.Printf("It's a Miss!")
}
}
// Now... did we get hit?
if LocalB.Board[ny][nx] == stateShip {
hitmiss = 1
LocalB.Board[ny][nx] = stateHit
} else {
hitmiss = 0
LocalB.Board[ny][nx] = stateAttempt
}
break
}
}
fmt.Print("Your Side Player Two\n")
fmt.Print(combineBoard(LocalB, RemoteB))
}
}