|
@@ -1,6 +1,7 @@
|
|
|
package main
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "math"
|
|
|
"bytes"
|
|
"bytes"
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
"fmt"
|
|
"fmt"
|
|
@@ -129,8 +130,8 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type viewport struct {
|
|
type viewport struct {
|
|
|
- x,y,width,height int
|
|
|
|
|
- Board *board.Board
|
|
|
|
|
|
|
+ x, y, width, height int
|
|
|
|
|
+ Board *board.Board
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func getViewPort(x, y, width, height int, gameId int64, bs *board.Board) *viewport {
|
|
func getViewPort(x, y, width, height int, gameId int64, bs *board.Board) *viewport {
|
|
@@ -138,10 +139,10 @@ func getViewPort(x, y, width, height int, gameId int64, bs *board.Board) *viewpo
|
|
|
x = min(x, bs.Width)
|
|
x = min(x, bs.Width)
|
|
|
y = max(y, 0)
|
|
y = max(y, 0)
|
|
|
y = min(y, bs.Height)
|
|
y = min(y, bs.Height)
|
|
|
- if x + width > bs.Width {
|
|
|
|
|
|
|
+ if x+width > bs.Width {
|
|
|
width = bs.Width - x
|
|
width = bs.Width - x
|
|
|
}
|
|
}
|
|
|
- if y + height > bs.Height {
|
|
|
|
|
|
|
+ if y+height > bs.Height {
|
|
|
height = bs.Height - x
|
|
height = bs.Height - x
|
|
|
}
|
|
}
|
|
|
url := "http://localhost:8080/games/board/" + strconv.FormatInt(gameId, 10) + "?x=" + strconv.Itoa(x) + "&y=" + strconv.Itoa(y) + "&rows=" + strconv.Itoa(width) + "&cols=" + strconv.Itoa(height)
|
|
url := "http://localhost:8080/games/board/" + strconv.FormatInt(gameId, 10) + "?x=" + strconv.Itoa(x) + "&y=" + strconv.Itoa(y) + "&rows=" + strconv.Itoa(width) + "&cols=" + strconv.Itoa(height)
|
|
@@ -149,17 +150,17 @@ func getViewPort(x, y, width, height int, gameId int64, bs *board.Board) *viewpo
|
|
|
resp, _ := http.Get(url)
|
|
resp, _ := http.Get(url)
|
|
|
board := board.ReadJSON(x, y, resp.Body)
|
|
board := board.ReadJSON(x, y, resp.Body)
|
|
|
return &viewport{
|
|
return &viewport{
|
|
|
- x:x,
|
|
|
|
|
- y:y,
|
|
|
|
|
- width:width,
|
|
|
|
|
- height:height,
|
|
|
|
|
- Board:board,
|
|
|
|
|
|
|
+ x: x,
|
|
|
|
|
+ y: y,
|
|
|
|
|
+ width: width,
|
|
|
|
|
+ height: height,
|
|
|
|
|
+ Board: board,
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (v viewport) Get(x, y int) board.TileType {
|
|
func (v viewport) Get(x, y int) board.TileType {
|
|
|
fmt.Printf("retrieving from viewport x %v y %v position in viewport x %v y %v", x, y, x-v.x, y-v.y)
|
|
fmt.Printf("retrieving from viewport x %v y %v position in viewport x %v y %v", x, y, x-v.x, y-v.y)
|
|
|
- return v.Board.Get(x-v.x,y-v.y)
|
|
|
|
|
|
|
+ return v.Board.Get(x-v.x, y-v.y)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const viewPortWidth = 32
|
|
const viewPortWidth = 32
|
|
@@ -182,10 +183,10 @@ func min(a, b int) int {
|
|
|
func move(gameId int64, p *player.Player, players []*player.Player, bs *board.Board, north bool) {
|
|
func move(gameId int64, p *player.Player, players []*player.Player, bs *board.Board, north bool) {
|
|
|
//determine move
|
|
//determine move
|
|
|
|
|
|
|
|
- viewPortX := p.Pos.X-1
|
|
|
|
|
- viewPortY := p.Pos.Y-1
|
|
|
|
|
|
|
+ viewPortX := p.Pos.X - 1
|
|
|
|
|
+ viewPortY := p.Pos.Y - 1
|
|
|
if north {
|
|
if north {
|
|
|
- viewPortX = viewPortX-viewPortHeight
|
|
|
|
|
|
|
+ viewPortX = viewPortX - viewPortHeight
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
viewPort := getViewPort(viewPortX, viewPortY, viewPortWidth, viewPortHeight, gameId, bs)
|
|
viewPort := getViewPort(viewPortX, viewPortY, viewPortWidth, viewPortHeight, gameId, bs)
|
|
@@ -197,10 +198,34 @@ func move(gameId int64, p *player.Player, players []*player.Player, bs *board.Bo
|
|
|
if viewPort.Get(p.Pos.X+1, p.Pos.Y) != board.Rock {
|
|
if viewPort.Get(p.Pos.X+1, p.Pos.Y) != board.Rock {
|
|
|
direction = "E"
|
|
direction = "E"
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+ var igloY int
|
|
|
|
|
+ if bs.Finish.Y > viewPort.y {
|
|
|
|
|
+ //iglo ten noorden van ons
|
|
|
|
|
+ igloY = 0
|
|
|
|
|
+ /*for viewPort.Get(viewPort.x+viewPortWidth, igloY) == board.Rock && igloY <= viewPort.y+viewPortHeight {
|
|
|
|
|
+ igloY++
|
|
|
|
|
+ }*/
|
|
|
|
|
+ } else if(bs.Finish.Y < viewPort.y+viewPortHeight) {
|
|
|
|
|
+ //iglo ten zuiden van ons
|
|
|
|
|
+ igloY = viewPort.Board.Height-1
|
|
|
|
|
+ /*for viewPort.Get(viewPort.x+viewPortWidth, igloY) == board.Rock && igloY >= viewPort.y+viewPortHeight {
|
|
|
|
|
+ igloY--
|
|
|
|
|
+ }*/
|
|
|
|
|
+ } else {
|
|
|
|
|
+ igloY = bs.Finish.Y
|
|
|
|
|
+ }
|
|
|
|
|
+ igloX := viewPort.Board.Width
|
|
|
|
|
+ calcDist(igloX, igloY, viewPort)
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ smallestDist := math.MaxInt32
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
//send move
|
|
//send move
|
|
|
url := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10) + "/move/" + p.Id + "/" + direction
|
|
url := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10) + "/move/" + p.Id + "/" + direction
|
|
|
- fmt.Println("move:>", url)
|
|
|
|
|
|
|
+ fmt.Println("move:>", url, smallestDist)
|
|
|
|
|
|
|
|
http.Post(url, "text/plain", nil)
|
|
http.Post(url, "text/plain", nil)
|
|
|
|
|
|
|
@@ -218,6 +243,73 @@ func move(gameId int64, p *player.Player, players []*player.Player, bs *board.Bo
|
|
|
fmt.Printf("new pos x=%v y=%v\n", p.Pos.X, p.Pos.Y)
|
|
fmt.Printf("new pos x=%v y=%v\n", p.Pos.X, p.Pos.Y)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+func toXy(index, boardWidth int) (x, y int) {
|
|
|
|
|
+ x = index % boardWidth
|
|
|
|
|
+ y = (index-x)/boardWidth
|
|
|
|
|
+ return x,y
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func toIndex(x,y , boardWidth int) (index int) {
|
|
|
|
|
+ return y*boardWidth+x
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func calcDist(igloX, igloY int, viewPort *viewport) map[int]int {
|
|
|
|
|
+ distance := make(map[int]int)
|
|
|
|
|
+
|
|
|
|
|
+ s := stack{}
|
|
|
|
|
+ index := toIndex(igloX, igloY, viewPort.Board.Width)
|
|
|
|
|
+ s.Put(index)
|
|
|
|
|
+ distance[index] = -1
|
|
|
|
|
+ for !s.Empty() {
|
|
|
|
|
+ i := s.Pop()
|
|
|
|
|
+ x, y := toXy(i, viewPort.Board.Width)
|
|
|
|
|
+ if(x+1 < viewPort.width) {
|
|
|
|
|
+ newI := toIndex(x+1, y, viewPort.Board.Width)
|
|
|
|
|
+
|
|
|
|
|
+ if(distance[newI] == 0 && viewPort.Board.Get(x+1, y) != board.Rock) {
|
|
|
|
|
+ distance[newI] = distance[toIndex(x, y, viewPort.Board.Width)] + 1
|
|
|
|
|
+ s.Put(newI)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if(x-1 > 0) {
|
|
|
|
|
+ newI := toIndex(x-1, y, viewPort.Board.Width)
|
|
|
|
|
+ if(distance[newI] == 0 && viewPort.Board.Get(x+1, y) != board.Rock) {
|
|
|
|
|
+ distance[newI] = distance[toIndex(x, y, viewPort.Board.Width)] + 1
|
|
|
|
|
+ s.Put(newI)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if(y+1 < viewPort.height) {
|
|
|
|
|
+ newI := toIndex(x, y+1, viewPort.Board.Width)
|
|
|
|
|
+ if(distance[newI] == 0 && viewPort.Board.Get(x+1, y) != board.Rock) {
|
|
|
|
|
+ distance[newI] = distance[toIndex(x, y, viewPort.Board.Width)] + 1
|
|
|
|
|
+ s.Put(newI)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(y-1 > 0) {
|
|
|
|
|
+ newI := toIndex(x, y-1, viewPort.Board.Width)
|
|
|
|
|
+ if(distance[newI] == 0 && viewPort.Board.Get(x+1, y) != board.Rock) {
|
|
|
|
|
+ distance[newI] = distance[toIndex(x, y, viewPort.Board.Width)] + 1
|
|
|
|
|
+ s.Put(newI)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return distance
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+type stack []int
|
|
|
|
|
+
|
|
|
|
|
+func (s stack) Empty() bool { return len(s) == 0 }
|
|
|
|
|
+func (s stack) Peek() int { return s[len(s)-1] }
|
|
|
|
|
+func (s *stack) Put(i int) { (*s) = append((*s), i) }
|
|
|
|
|
+func (s *stack) Pop() int {
|
|
|
|
|
+ d := (*s)[len(*s)-1]
|
|
|
|
|
+ (*s) = (*s)[:len(*s)-1]
|
|
|
|
|
+ return d
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func playerJoin(p *player.Player) {
|
|
func playerJoin(p *player.Player) {
|
|
|
|
|
|
|
|
}
|
|
}
|