Ronald Peterson преди 10 години
родител
ревизия
ea831ee9a0
променени са 2 файла, в които са добавени 142 реда и са изтрити 15 реда
  1. 35 0
      battlecamp-go-bot/distance_test.go
  2. 107 15
      battlecamp-go-bot/main.go

+ 35 - 0
battlecamp-go-bot/distance_test.go

@@ -0,0 +1,35 @@
+package main
+
+import (
+	"battlecamp-go-server/board"
+	"fmt"
+	"testing"
+)
+
+func TestDistance(t *testing.T) {
+
+	data := []byte{0x00, 0x00, 0xAA, 0x00,
+		0x00, 0x00, 0x00, 0x00,
+		0x00, 0xAA, 0xAA, 0xAA,
+		0x00, 0x00, 0x00, 0x00}
+	b := board.NewPreset(16, 4, data)
+	b.Finish = board.Coordinate{
+		X: 16,
+		Y: 0,
+	}
+
+	viewport := viewport{
+		x:      0,
+		y:      0,
+		width:  16,
+		height: 4,
+		Board:  b,
+	}
+	dist := calcDist(b.Finish.X, b.Finish.Y, &viewport)
+	for i := 0;i< b.Height;i++ {
+		for j := 0;j< b.Width;j++ {
+			fmt.Printf("%v ", dist[i*b.Height+j])
+		}
+		fmt.Printf("\n")
+	}
+}

+ 107 - 15
battlecamp-go-bot/main.go

@@ -1,6 +1,7 @@
 package main
 
 import (
+	"math"
 	"bytes"
 	"encoding/json"
 	"fmt"
@@ -129,8 +130,8 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate)
 }
 
 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 {
@@ -138,10 +139,10 @@ func getViewPort(x, y, width, height int, gameId int64, bs *board.Board) *viewpo
 	x = min(x, bs.Width)
 	y = max(y, 0)
 	y = min(y, bs.Height)
-	if x + width > bs.Width {
+	if x+width > bs.Width {
 		width = bs.Width - x
 	}
-	if y + height > bs.Height {
+	if y+height > bs.Height {
 		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)
@@ -149,17 +150,17 @@ func getViewPort(x, y, width, height int, gameId int64, bs *board.Board) *viewpo
 	resp, _ := http.Get(url)
 	board := board.ReadJSON(x, y, resp.Body)
 	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 {
 	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
@@ -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) {
 	//determine move
 
-	viewPortX := p.Pos.X-1
-	viewPortY := p.Pos.Y-1
+	viewPortX := p.Pos.X - 1
+	viewPortY := p.Pos.Y - 1
 	if north {
-		viewPortX = viewPortX-viewPortHeight
+		viewPortX = viewPortX - viewPortHeight
 	}
 
 	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 {
 		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
 	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)
 
@@ -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)
 }
 
+
+
+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) {
 
 }