|
|
@@ -5,50 +5,108 @@ import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
+ "log"
|
|
|
"math/rand"
|
|
|
+ "net/http"
|
|
|
)
|
|
|
|
|
|
type Board struct {
|
|
|
Cols int `json:"cols"`
|
|
|
Rows int `json:"rows"`
|
|
|
Finish Coordinate `json:"finish"`
|
|
|
- Tiles []Tile `json:"-"`
|
|
|
+ Tiles []byte `json:"-"`
|
|
|
}
|
|
|
|
|
|
-const maxIceWidth = 26
|
|
|
+type req struct {
|
|
|
+ x, y, width, height int
|
|
|
+}
|
|
|
+
|
|
|
+type response struct {
|
|
|
+ boardTile io.ReadCloser
|
|
|
+ x, y int
|
|
|
+}
|
|
|
|
|
|
// Create a new randomly generated board.
|
|
|
-func New(cols, rows int) *Board {
|
|
|
+func New(width, height int) *Board {
|
|
|
b := &Board{
|
|
|
- Cols: cols,
|
|
|
- Rows: rows,
|
|
|
- Tiles: make([]Tile, rows*cols)}
|
|
|
+ Cols: width,
|
|
|
+ Rows: height,
|
|
|
+ Tiles: make([]byte, height*width)}
|
|
|
+
|
|
|
+ log.Printf("Start creating board %v x %v", width, height)
|
|
|
+
|
|
|
+ regionWidth := 8
|
|
|
+ regionHieght := 8
|
|
|
+
|
|
|
+ regionWidthNum := width / regionWidth
|
|
|
+ regionHeightNum := height / regionHieght
|
|
|
+
|
|
|
+ returnChan := make(chan response)
|
|
|
+
|
|
|
+ for i := 0; i < regionWidthNum; i++ {
|
|
|
+ for j := 0; j < regionHeightNum; j++ {
|
|
|
+ x := i * regionWidth
|
|
|
+ y := j * regionHieght
|
|
|
|
|
|
- for y := 0; y < rows; y++ {
|
|
|
- for x := 0; x < cols; x++ {
|
|
|
- b.randomizeTile(x, y)
|
|
|
+ gbt := req{
|
|
|
+ x: x,
|
|
|
+ y: y,
|
|
|
+ width: regionWidth,
|
|
|
+ height: regionHieght,
|
|
|
+ }
|
|
|
+ go genRegion(gbt, returnChan)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ answers := 0
|
|
|
+ for regionWidthNum+regionHeightNum > answers {
|
|
|
+
|
|
|
+ result := <-returnChan
|
|
|
+
|
|
|
+ n :=-1
|
|
|
+
|
|
|
+ for n != 0 {
|
|
|
+ index := result.x*b.Cols+result.y
|
|
|
+ ba := b.Tiles[index:index+regionWidth]
|
|
|
+ n, _ = result.boardTile.Read(ba)
|
|
|
}
|
|
|
+ answers++
|
|
|
}
|
|
|
|
|
|
- igloY := rand.Intn(rows-1) + 1
|
|
|
+ igloY := rand.Intn(height-2) + 1
|
|
|
|
|
|
- b.Set(cols-2, igloY, Iglo)
|
|
|
- b.Finish.X = cols - 2
|
|
|
+ b.Set(width-2, igloY, Iglo)
|
|
|
+ b.Finish.X = width - 2
|
|
|
b.Finish.Y = igloY
|
|
|
- b.Set(cols-2, igloY-1, Water)
|
|
|
- b.Set(cols-2, igloY+1, Water)
|
|
|
+ b.Set(width-2, igloY-1, Water)
|
|
|
+ b.Set(width-2, igloY+1, Water)
|
|
|
+ log.Printf("Finished creating board %v x %v\n", width, height)
|
|
|
|
|
|
return b
|
|
|
}
|
|
|
|
|
|
+func genRegion(req req, responseChan chan response) {
|
|
|
+ requestUrl := fmt.Sprintf("http://localhost:8080/&width=%v&height=%v&x=%v&y=%v", req.width, req.height, req.x, req.y)
|
|
|
+ log.Printf("Requested generation of tile with url: %v\n", requestUrl)
|
|
|
+ resp, _ := http.Get(requestUrl)
|
|
|
+
|
|
|
+ result := response{
|
|
|
+ x: req.x,
|
|
|
+ y: req.y,
|
|
|
+ boardTile: resp.Body,
|
|
|
+ }
|
|
|
+
|
|
|
+ responseChan <- result
|
|
|
+}
|
|
|
+
|
|
|
// Set the tile in column x and row y to val.
|
|
|
func (b *Board) Set(x, y int, val Tile) {
|
|
|
- b.Tiles[y*b.Cols+x] = val
|
|
|
+ b.Tiles[y*b.Cols+x] = byte(val)
|
|
|
}
|
|
|
|
|
|
// Get the tile in column x and row y.
|
|
|
func (b *Board) Get(x, y int) Tile {
|
|
|
- return b.Tiles[y*b.Cols+x]
|
|
|
+ return Tile(b.Tiles[y*b.Cols+x])
|
|
|
}
|
|
|
|
|
|
func (b *Board) String() string {
|
|
|
@@ -111,33 +169,3 @@ func sanitizeViewPort(b *Board, startCol, startRow, cols, rows int) (int, int, i
|
|
|
}
|
|
|
return startCol, startRow, cols, rows
|
|
|
}
|
|
|
-
|
|
|
-func (b *Board) randomizeTile(x, y int) {
|
|
|
-
|
|
|
- switch {
|
|
|
- case b.isRock(x, y):
|
|
|
- b.Set(x, y, Rock)
|
|
|
- case b.isIce(x, y):
|
|
|
- b.Set(x, y, Ice)
|
|
|
- default:
|
|
|
- b.Set(x, y, Water)
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-func (b *Board) isRock(x, y int) bool {
|
|
|
- return rand.Intn(10) > 7
|
|
|
-}
|
|
|
-
|
|
|
-func (b *Board) isIce(x, y int) bool {
|
|
|
- leftLimit := (b.Cols - maxIceWidth) / 2
|
|
|
- rightLimit := b.Cols/2 + maxIceWidth/2
|
|
|
- return x > leftLimit && x < rightLimit && rand.Intn(maxIceWidth) >= abs((b.Cols/2)-x)
|
|
|
-}
|
|
|
-
|
|
|
-func abs(a int) int {
|
|
|
- if a < 0 {
|
|
|
- return -a
|
|
|
- }
|
|
|
- return a
|
|
|
-}
|