-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.go
92 lines (76 loc) · 1.45 KB
/
game.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
)
type boat struct{
position [2]int
length int
isVertical bool
}
var boats []boat;
func startGame(){
boats = generateBoats();
go allowRoutes();
for {
resp := ask();
gameCommands(resp, boats);
}
}
func allowRoutes(){
http.HandleFunc("/gameSet", gameSetHandler);
http.ListenAndServe(":9000", nil)
}
func gameSetHandler(w http.ResponseWriter, req *http.Request){
if(req.Method == http.MethodGet){
fmt.Fprintf(w, getGameSet(false, boats));
}
}
func askForGameSet(player string){
fmt.Println("ok");
if value, ok := players[player]; ok {
resp, err := http.Get("http://" + value.String() + ":9000/gameSet");
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
}
}
func getGameSet(private bool, boats []boat) string{
gameSet := "";
gameSet +=" ";
for i:= 0; i < 10; i++{
gameSet +=" ";
if(i != 0){
gameSet +="| ";
}
gameSet += strconv.Itoa(i) ;
}
gameSet +="\n";
letters := [10]string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
for i:= 0; i < 10; i++{
gameSet += letters[i];
for j:= 0; j < 11; j++{
gameSet +=" | ";
if(isBoatPos([2]int{i,j}, boats)) {
if(private){
gameSet +="1";
} else {
gameSet +=" ";
}
} else {
gameSet +=" ";
}
}
gameSet +="\n";
}
return gameSet;
}