board.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. if (x != startCol) {
  52. w.Write([]byte{','})
  53. }
  54. t := &jsonTile{x, y, b.Get(x, y).Name(), ""}
  55. bs, _ := json.Marshal(t)
  56. w.Write(bs)
  57. }
  58. }
  59. fmt.Fprintf(w, "}}")
  60. }
  61. func sanitizeViewPort(b *Board, startCol, startRow, cols, rows int) (int, int, int, int) {
  62. if startCol > b.Cols {
  63. startCol = b.Cols
  64. }
  65. if startCol < 0 {
  66. cols += startCol
  67. startCol = 0
  68. }
  69. if startRow > b.Rows {
  70. startRow = b.Rows
  71. }
  72. if startRow < 0 {
  73. rows += startRow
  74. startRow = 0
  75. }
  76. if startCol+cols > b.Cols {
  77. cols = b.Cols - startCol
  78. }
  79. if startRow+rows > b.Rows {
  80. rows = b.Rows - startRow
  81. }
  82. if cols < 0 {
  83. cols = 0
  84. }
  85. if rows < 0 {
  86. rows = 0
  87. }
  88. return startCol, startRow, cols, rows
  89. }
  90. func (b *Board) randomizeTile(x, y int) {
  91. switch {
  92. case b.isIglo(x, y):
  93. b.Set(x, y, Iglo)
  94. b.Finish.X = x
  95. b.Finish.Y = y
  96. case b.isRock(x, y):
  97. b.Set(x, y, Rock)
  98. case b.isIce(x, y):
  99. b.Set(x, y, Ice)
  100. default:
  101. b.Set(x, y, Water)
  102. }
  103. }
  104. func (b *Board) isIglo(x, y int) bool {
  105. return x == (b.Cols-1) && y == (b.Rows-1)/2
  106. }
  107. func (b *Board) isRock(x, y int) bool {
  108. return rand.Intn(10) > 7
  109. }
  110. func (b *Board) isIce(x, y int) bool {
  111. leftLimit := (b.Cols - maxIceWidth) / 2
  112. rightLimit := b.Cols/2 + maxIceWidth/2
  113. return x > leftLimit && x < rightLimit && rand.Intn(maxIceWidth) >= abs((b.Cols/2)-x)
  114. }
  115. func abs(a int) int {
  116. if a < 0 {
  117. return -a
  118. }
  119. return a
  120. }