|
@@ -8,17 +8,24 @@ import (
|
|
|
"math/rand"
|
|
"math/rand"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+type BoardSummary struct {
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
type Board struct {
|
|
type Board struct {
|
|
|
- Cols int `json:"cols"`
|
|
|
|
|
- Rows int `json:"rows"`
|
|
|
|
|
- Tiles []Tile `json:"tiles"`
|
|
|
|
|
|
|
+ Cols int `json:"cols"`
|
|
|
|
|
+ Rows int `json:"rows"`
|
|
|
|
|
+ Finish Coordinate `json:"finish"`
|
|
|
|
|
+ Tiles []Tile `json:"-"`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const maxIceWidth = 26
|
|
const maxIceWidth = 26
|
|
|
|
|
|
|
|
// Create a new randomly generated board.
|
|
// Create a new randomly generated board.
|
|
|
func New(cols, rows int) *Board {
|
|
func New(cols, rows int) *Board {
|
|
|
- b := &Board{cols, rows, make([]Tile, rows*cols)}
|
|
|
|
|
|
|
+ b := &Board{
|
|
|
|
|
+ Cols: cols,
|
|
|
|
|
+ Rows: rows,
|
|
|
|
|
+ Tiles: make([]Tile, rows*cols)}
|
|
|
|
|
|
|
|
for y := 0; y < rows; y++ {
|
|
for y := 0; y < rows; y++ {
|
|
|
for x := 0; x < cols; x++ {
|
|
for x := 0; x < cols; x++ {
|
|
@@ -30,16 +37,16 @@ func New(cols, rows int) *Board {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Set the tile in column x and row y to val.
|
|
// Set the tile in column x and row y to val.
|
|
|
-func (b Board) Set(x, y int, val Tile) {
|
|
|
|
|
|
|
+func (b *Board) Set(x, y int, val Tile) {
|
|
|
b.Tiles[y*b.Cols+x] = val
|
|
b.Tiles[y*b.Cols+x] = val
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Get the tile in column x and row y.
|
|
// Get the tile in column x and row y.
|
|
|
-func (b Board) Get(x, y int) Tile {
|
|
|
|
|
|
|
+func (b *Board) Get(x, y int) Tile {
|
|
|
return b.Tiles[y*b.Cols+x]
|
|
return b.Tiles[y*b.Cols+x]
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (b Board) String() string {
|
|
|
|
|
|
|
+func (b *Board) String() string {
|
|
|
var buffer bytes.Buffer
|
|
var buffer bytes.Buffer
|
|
|
|
|
|
|
|
for y := 0; y < b.Rows; y++ {
|
|
for y := 0; y < b.Rows; y++ {
|
|
@@ -59,7 +66,7 @@ type jsonTile struct {
|
|
|
Player string `json:"player,omitempty"`
|
|
Player string `json:"player,omitempty"`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (b Board) WriteJSON(w io.Writer, startCol, startRow, cols, rows int) {
|
|
|
|
|
|
|
+func (b *Board) WriteJSON(w io.Writer, startCol, startRow, cols, rows int) {
|
|
|
sc, sr, cols, rows := sanitizeViewPort(b, startCol, startRow, cols, rows)
|
|
sc, sr, cols, rows := sanitizeViewPort(b, startCol, startRow, cols, rows)
|
|
|
|
|
|
|
|
for y := startRow; y < sr+rows; y++ {
|
|
for y := startRow; y < sr+rows; y++ {
|
|
@@ -74,7 +81,7 @@ func (b Board) WriteJSON(w io.Writer, startCol, startRow, cols, rows int) {
|
|
|
fmt.Fprintf(w, "}}")
|
|
fmt.Fprintf(w, "}}")
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func sanitizeViewPort(b Board, startCol, startRow, cols, rows int) (int, int, int, int) {
|
|
|
|
|
|
|
+func sanitizeViewPort(b *Board, startCol, startRow, cols, rows int) (int, int, int, int) {
|
|
|
if startCol > b.Cols {
|
|
if startCol > b.Cols {
|
|
|
startCol = b.Cols
|
|
startCol = b.Cols
|
|
|
}
|
|
}
|
|
@@ -104,11 +111,13 @@ func sanitizeViewPort(b Board, startCol, startRow, cols, rows int) (int, int, in
|
|
|
return startCol, startRow, cols, rows
|
|
return startCol, startRow, cols, rows
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (b Board) randomizeTile(x, y int) {
|
|
|
|
|
|
|
+func (b *Board) randomizeTile(x, y int) {
|
|
|
|
|
|
|
|
switch {
|
|
switch {
|
|
|
case b.isIglo(x, y):
|
|
case b.isIglo(x, y):
|
|
|
b.Set(x, y, Iglo)
|
|
b.Set(x, y, Iglo)
|
|
|
|
|
+ b.Finish.X = x
|
|
|
|
|
+ b.Finish.Y = y
|
|
|
case b.isRock(x, y):
|
|
case b.isRock(x, y):
|
|
|
b.Set(x, y, Rock)
|
|
b.Set(x, y, Rock)
|
|
|
case b.isIce(x, y):
|
|
case b.isIce(x, y):
|
|
@@ -119,15 +128,15 @@ func (b Board) randomizeTile(x, y int) {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (b Board) isIglo(x, y int) bool {
|
|
|
|
|
|
|
+func (b *Board) isIglo(x, y int) bool {
|
|
|
return x == (b.Cols-1) && y == (b.Rows-1)/2
|
|
return x == (b.Cols-1) && y == (b.Rows-1)/2
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (b Board) isRock(x, y int) bool {
|
|
|
|
|
|
|
+func (b *Board) isRock(x, y int) bool {
|
|
|
return rand.Intn(10) > 7
|
|
return rand.Intn(10) > 7
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (b Board) isIce(x, y int) bool {
|
|
|
|
|
|
|
+func (b *Board) isIce(x, y int) bool {
|
|
|
leftLimit := (b.Cols - maxIceWidth) / 2
|
|
leftLimit := (b.Cols - maxIceWidth) / 2
|
|
|
rightLimit := b.Cols/2 + maxIceWidth/2
|
|
rightLimit := b.Cols/2 + maxIceWidth/2
|
|
|
return x > leftLimit && x < rightLimit && rand.Intn(maxIceWidth) >= abs((b.Cols/2)-x)
|
|
return x > leftLimit && x < rightLimit && rand.Intn(maxIceWidth) >= abs((b.Cols/2)-x)
|