-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (81 loc) · 2.27 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
package main
import (
"fmt"
"net/http"
"log"
"encoding/json"
)
import . "./core"
import . "./definitions"
var mySnake SnakeConfig = SnakeConfig {
Apiversion : "1",
Author : "bigboi",
Color :"#000000",
Head : "dead",
Tail : "bolt",
}
var currentGame MainRequest
var gameState GameState
func entry(w http.ResponseWriter, r *http.Request){
snakeJSON, _ := json.Marshal(&mySnake)
w.Header().Set("Content-Type", "application/json")
//Write calls WriteHeader with http.StatusOk (200) before send data
w.Write(snakeJSON)
fmt.Printf("%s %s %s\n", r.Proto, r.Method, r.URL.Path)
}
func start(w http.ResponseWriter, r *http.Request){
fmt.Printf("%s %s %s\n", r.Proto, r.Method, r.URL.Path)
err := json.NewDecoder(r.Body).Decode(¤tGame)
if err != nil {
fmt.Println(err)
return
}
InitGameState(currentGame, &gameState)
// fmt.Println("after init food chosen ", gameState.FoodTarget)
}
func move(w http.ResponseWriter, r *http.Request) {
fmt.Printf("%s %s %s\n", r.Proto, r.Method, r.URL.Path)
err := json.NewDecoder(r.Body).Decode(¤tGame)
if err != nil {
fmt.Println(err)
return
}
UpdateGameState(currentGame, &gameState)
fmt.Println("my tail", gameState.Tail)
nextMove := ""
nextMove = GetBestMoveToFood(currentGame.You.Head, currentGame.Board, currentGame.You, &gameState, false)
if nextMove == "nopath" {
gameState.FoodTarget = gameState.Tail
nextMove = GetBestMoveToFood(currentGame.You.Head, currentGame.Board, currentGame.You, &gameState, true)
}
var move Move
move.Move = nextMove
moveJSON, _ := json.Marshal(&move)
fmt.Println("sending move ", move)
w.Header().Set("Content-Type", "application/json")
w.Write(moveJSON)
}
func end(w http.ResponseWriter, r *http.Request) {
fmt.Printf("%s %s %s\n", r.Proto, r.Method, r.URL.Path)
}
func main(){
http.HandleFunc("/", entry)
http.HandleFunc("/start", start)
http.HandleFunc("/move", move)
http.HandleFunc("/end", end)
fmt.Println("started listening on port 3000")
log.Fatal(http.ListenAndServe(":3000", nil))
// log.Print("sf")
// board := [][]int{
// {0,0,0,0,0},
// {0,0,0,0,0},
// {0,0,0,1,0},
// {0,0,0,0,0},
// }
// dx := [4]int{0, 1, 0 ,-1}
// dy := [4]int{-1, 0, 1 ,0}
// for i := range dx {
// fmt.Println(dx[i])
// fmt.Println(dy[i])
// }
}