Jelajahi Sumber

optimize board generation a bit

Harry de Boer 10 tahun lalu
induk
melakukan
3db57ddf50
3 mengubah file dengan 35 tambahan dan 36 penghapusan
  1. 16 27
      board/board.go
  2. 17 9
      board/board_test.go
  3. 2 0
      urlrouter.go

+ 16 - 27
board/board.go

@@ -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

+ 17 - 9
board/board_test.go

@@ -3,6 +3,7 @@ package board
 import (
 	"math/rand"
 	"testing"
+	"time"
 )
 
 var testBoard = `~~~~~~~~▲▲▲~▲▲~~*~*~~**~*▲****▲▲***▲~▲***~~▲~~▲~▲~~~~~▲~
@@ -21,9 +22,9 @@ var testBoard = `~~~~~~~~▲▲▲~▲▲~~*~*~~**~*▲****▲▲***▲~▲***~~
 ▲~▲~~▲▲~~▲~~~▲~~~~********▲▲▲▲***▲*~***▲▲~~~~▲~~~~~~~▲~~
 ~▲~~~~▲▲~~~~~▲~~▲*~*▲▲*▲****▲▲******▲*~~*~~~~▲~~~~~~~▲~~
 ~~~~~~~~~~~~~~▲▲~▲~******▲*▲*▲**~*~*~**~*~~~~~~~~▲▲~~~~▲
-~~▲▲~~~~~~~~~~~▲***▲~*▲**▲*~***▲*********~~~~▲▲~~~~~▲~~~
-~~▲~~~~~~▲▲▲~~~~*~~▲~*~▲*▲*▲*****▲**▲~*▲*~▲~~~~▲▲~▲~~~~
-~~~~~~~▲~~~▲▲~~~*~▲**~**▲*▲*▲***▲**▲*****~~~~▲~~~~~~~▲~~
+~~▲▲~~~~~~~~~~~▲***▲~*▲**▲*~***▲*********~~~~▲▲~~~~~▲~~
+~~▲~~~~~~▲▲▲~~~~*~~▲~*~▲*▲*▲*****▲**▲~*▲*~▲~~~~▲▲~▲~~~~~
+~~~~~~▲~~~▲▲~~~~*▲▲**~**▲*▲*▲***▲**▲*****~~~~▲~~~~~~~▲~~
 ~~▲~~~~▲~~~~~▲~~~*~********▲**▲*~*▲*▲**▲~~~~~~~~~~~~~~▲~
 ~~~~~~~~~~~~~~▲~*~*~~****~~**▲*****▲**~*~~~▲~▲~~▲~~~▲~~▲
 ~▲~~~▲~▲▲~~~~~~~~*~**~~*▲**▲*▲▲***~******~▲~~~~~~▲~~~~~~
@@ -43,8 +44,8 @@ var testBoard = `~~~~~~~~▲▲▲~▲▲~~*~*~~**~*▲****▲▲***▲~▲***~~
 `
 
 func TestNewBoard(t *testing.T) {
-	rand.Seed(0)
-	b := New(56, 35)
+	r := rand.New(rand.NewSource(0))
+	b := new(56, 35, r)
 
 	if testBoard != b.String() {
 		t.Fatalf("Incorrect board:\n %v", b)
@@ -54,7 +55,6 @@ func TestNewBoard(t *testing.T) {
 
 func BenchmarkNew(b *testing.B) {
 	for i := 0; i < b.N; i++ {
-		rand.Seed(0)
 		New(56, 35)
 	}
 }
@@ -62,10 +62,18 @@ func BenchmarkNew(b *testing.B) {
 func BenchmarkRnd(b *testing.B) {
 
 	for i := 0; i < b.N; i++ {
-		rand.Seed(0)
-
-		for j := 0; j < 56*35; j++ {
+		for j := 0; j < 50*50; j++ {
 			rand.Intn(10)
 		}
 	}
 }
+
+func BenchmarkR(b *testing.B) {
+	r := rand.New(rand.NewSource(time.Now().UnixNano()))
+
+	for i := 0; i < b.N; i++ {
+		for j := 0; j < 50*50; j++ {
+			r.Intn(10)
+		}
+	}
+}

+ 2 - 0
urlrouter.go

@@ -60,6 +60,8 @@ func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 	yString := ps.ByName("rows")
 	var game *games.Game
 
+	log.Println("Creating new game...")
+
 	if xString != "" && yString != "" {
 		x, _ := strconv.Atoi(xString)
 		y, _ := strconv.Atoi(yString)