Explorar o código

Added board summery when retrieving the game details

Ronald Peterson %!s(int64=10) %!d(string=hai) anos
pai
achega
4e4a74446c
Modificáronse 2 ficheiros con 23 adicións e 14 borrados
  1. 22 13
      board/board.go
  2. 1 1
      games/game.go

+ 22 - 13
board/board.go

@@ -8,17 +8,24 @@ import (
 	"math/rand"
 )
 
+type BoardSummary 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
 
 // Create a new randomly generated 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 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.
-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
 }
 
 // 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]
 }
 
-func (b Board) String() string {
+func (b *Board) String() string {
 	var buffer bytes.Buffer
 
 	for y := 0; y < b.Rows; y++ {
@@ -59,7 +66,7 @@ type jsonTile struct {
 	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)
 
 	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, "}}")
 }
 
-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 {
 		startCol = b.Cols
 	}
@@ -104,11 +111,13 @@ func sanitizeViewPort(b Board, startCol, startRow, cols, rows int) (int, int, in
 	return startCol, startRow, cols, rows
 }
 
-func (b Board) randomizeTile(x, y int) {
+func (b *Board) randomizeTile(x, y int) {
 
 	switch {
 	case b.isIglo(x, y):
 		b.Set(x, y, Iglo)
+		b.Finish.X = x
+		b.Finish.Y = y
 	case b.isRock(x, y):
 		b.Set(x, y, Rock)
 	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
 }
 
-func (b Board) isRock(x, y int) bool {
+func (b *Board) isRock(x, y int) bool {
 	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
 	rightLimit := b.Cols/2 + maxIceWidth/2
 	return x > leftLimit && x < rightLimit && rand.Intn(maxIceWidth) >= abs((b.Cols/2)-x)

+ 1 - 1
games/game.go

@@ -10,7 +10,7 @@ type Game struct {
 	Id        int64         `json:"id"`
 	StartTime int64         `json:"startTime"`
 	EndTime   int64         `json:"endTime"`
-	Board     *board.Board   `json:"-"`
+	Board     *board.Board   `json:"board"`
 	Winner    *player.Player `json:"winner,omitempty"`
 }