|
|
@@ -6,6 +6,7 @@ import (
|
|
|
"fmt"
|
|
|
"io"
|
|
|
"math/rand"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type Board struct {
|
|
|
@@ -19,25 +20,36 @@ const maxIceWidth = 26
|
|
|
|
|
|
// Create a new randomly generated board.
|
|
|
func New(cols, rows int) *Board {
|
|
|
+ r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
+ return new(cols, rows, r)
|
|
|
+}
|
|
|
+
|
|
|
+func new(cols, rows int, r *rand.Rand) *Board {
|
|
|
b := &Board{
|
|
|
Cols: cols,
|
|
|
Rows: rows,
|
|
|
- Tiles: make([]Tile, rows*cols)}
|
|
|
+ Tiles: make([]Tile, rows*cols, rows*cols)}
|
|
|
|
|
|
for y := 0; y < rows; y++ {
|
|
|
for x := 0; x < cols; x++ {
|
|
|
- b.randomizeTile(x, y)
|
|
|
+ switch {
|
|
|
+ case r.Intn(10) > 7:
|
|
|
+ b.Set(x, y, Rock)
|
|
|
+ case x > (b.Cols-maxIceWidth)/2 && x < b.Cols/2+maxIceWidth/2 && r.Intn(maxIceWidth) >= abs((b.Cols/2)-x):
|
|
|
+ b.Set(x, y, Ice)
|
|
|
+ default:
|
|
|
+ b.Set(x, y, Water)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- igloY := rand.Intn(rows-1) + 1
|
|
|
+ igloY := r.Intn(rows-2) + 1
|
|
|
|
|
|
b.Set(cols-2, igloY, Iglo)
|
|
|
b.Finish.X = cols - 2
|
|
|
b.Finish.Y = igloY
|
|
|
b.Set(cols-2, igloY-1, Water)
|
|
|
b.Set(cols-2, igloY+1, Water)
|
|
|
-
|
|
|
return b
|
|
|
}
|
|
|
|
|
|
@@ -112,29 +124,6 @@ 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
|