board.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package board
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "math/rand"
  8. )
  9. type Board struct {
  10. Cols int `json:"cols"`
  11. Rows int `json:"rows"`
  12. Finish Coordinate `json:"finish"`
  13. Tiles []Tile `json:"-"`
  14. }
  15. const maxIceWidth = 26
  16. // Create a new randomly generated board.
  17. func New(cols, rows int) *Board {
  18. b := &Board{
  19. Cols: cols,
  20. Rows: rows,
  21. Tiles: make([]Tile, rows*cols)}
  22. for y := 0; y < rows; y++ {
  23. for x := 0; x < cols; x++ {
  24. b.randomizeTile(x, y)
  25. }
  26. }
  27. return b
  28. }
  29. // Set the tile in column x and row y to val.
  30. func (b *Board) Set(x, y int, val Tile) {
  31. b.Tiles[y*b.Cols+x] = val
  32. }
  33. // Get the tile in column x and row y.
  34. func (b *Board) Get(x, y int) Tile {
  35. return b.Tiles[y*b.Cols+x]
  36. }
  37. func (b *Board) String() string {
  38. var buffer bytes.Buffer
  39. for y := 0; y < b.Rows; y++ {
  40. for x := 0; x < b.Cols; x++ {
  41. buffer.WriteString(b.Get(x, y).String())
  42. }
  43. buffer.WriteString(fmt.Sprintln())
  44. }
  45. return buffer.String()
  46. }
  47. func (b *Board) WriteJSON(w io.Writer, startCol, startRow, cols, rows int) {
  48. sc, sr, cols, rows := sanitizeViewPort(b, startCol, startRow, cols, rows)
  49. for y := startRow; y < sr+rows; y++ {
  50. for x := startCol; x < sc+cols; x++ {
  51. t := &jsonTile{x, y, b.Get(x, y).Name(), ""}
  52. bs, _ := json.Marshal(t)
  53. w.Write(bs)
  54. w.Write([]byte{','})
  55. }
  56. }
  57. fmt.Fprintf(w, "}}")
  58. }
  59. func sanitizeViewPort(b *Board, startCol, startRow, cols, rows int) (int, int, int, int) {
  60. if startCol > b.Cols {
  61. startCol = b.Cols
  62. }
  63. if startCol < 0 {
  64. cols += startCol
  65. startCol = 0
  66. }
  67. if startRow > b.Rows {
  68. startRow = b.Rows
  69. }
  70. if startRow < 0 {
  71. rows += startRow
  72. startRow = 0
  73. }
  74. if startCol+cols > b.Cols {
  75. cols = b.Cols - startCol
  76. }
  77. if startRow+rows > b.Rows {
  78. rows = b.Rows - startRow
  79. }
  80. if cols < 0 {
  81. cols = 0
  82. }
  83. if rows < 0 {
  84. rows = 0
  85. }
  86. return startCol, startRow, cols, rows
  87. }
  88. func (b *Board) randomizeTile(x, y int) {
  89. switch {
  90. case b.isIglo(x, y):
  91. b.Set(x, y, Iglo)
  92. b.Finish.X = x
  93. b.Finish.Y = y
  94. case b.isRock(x, y):
  95. b.Set(x, y, Rock)
  96. case b.isIce(x, y):
  97. b.Set(x, y, Ice)
  98. default:
  99. b.Set(x, y, Water)
  100. }
  101. }
  102. func (b *Board) isIglo(x, y int) bool {
  103. return x == (b.Cols-1) && y == (b.Rows-1)/2
  104. }
  105. func (b *Board) isRock(x, y int) bool {
  106. return rand.Intn(10) > 7
  107. }
  108. func (b *Board) isIce(x, y int) bool {
  109. leftLimit := (b.Cols - maxIceWidth) / 2
  110. rightLimit := b.Cols/2 + maxIceWidth/2
  111. return x > leftLimit && x < rightLimit && rand.Intn(maxIceWidth) >= abs((b.Cols/2)-x)
  112. }
  113. func abs(a int) int {
  114. if a < 0 {
  115. return -a
  116. }
  117. return a
  118. }