board.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package board
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "math/rand"
  8. "time"
  9. )
  10. var setMask [4]byte = [4]byte{0xFC, 0xF3, 0xCF, 0x3F}
  11. var getMask [4]byte = [4]byte{0x03, 0x0C, 0x30, 0xC0}
  12. type Board struct {
  13. Width int `json:"cols"`
  14. Height int `json:"rows"`
  15. Finish Coordinate `json:"finish"`
  16. data []byte `json:"-"`
  17. }
  18. const maxIceWidth = 26
  19. // Create a new randomly generated board.
  20. func New(width, height int) *Board {
  21. return NewPartial(0, 0, width, height, width, height)
  22. }
  23. func NewPartial(startX, startY, width, height, totalWidth, totalHeight int) *Board {
  24. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  25. return newPartial(startX, startY, width, height, totalWidth, totalHeight, r)
  26. }
  27. func (b *Board) Set(x, y int, t TileType) {
  28. index := uint(y*b.Width + x)
  29. i := index / 4
  30. p := index % 4
  31. b.data[i] = (b.data[i] & setMask[p]) | byte(t)<<(p<<1)
  32. }
  33. func (b *Board) Get(x, y int) TileType {
  34. index := y*b.Width + x
  35. i := index / 4
  36. p := index % 4
  37. return TileType((b.data[i] & getMask[p]) >> uint(p<<1))
  38. }
  39. func (b *Board) WriteData(w io.Writer) {
  40. n, err := w.Write(b.data)
  41. if err != nil {
  42. fmt.Errorf("Error writing board after %v bytes: %v", n, err)
  43. }
  44. }
  45. func (b *Board) WriteJSON(w io.Writer, startCol, startRow, cols, rows int) {
  46. sc, sr, cols, rows := sanitizeViewPort(b, startCol, startRow, cols, rows)
  47. fmt.Fprintf(w, "{\"x\":%v,\"y\":%v,\"rows\":%v,\"cols\":%v,\"tiles\":[", sc, sr, cols, rows)
  48. for y := startRow; y < sr+rows; y++ {
  49. for x := startCol; x < sc+cols; x++ {
  50. if !(x == startCol && y == startRow) {
  51. w.Write([]byte{','})
  52. }
  53. t := &jsonTile{x, y, b.Get(x, y).Name(), ""}
  54. bs, _ := json.Marshal(t)
  55. w.Write(bs)
  56. }
  57. }
  58. fmt.Fprintf(w, "]}")
  59. }
  60. func (b *Board) String() string {
  61. var buffer bytes.Buffer
  62. for y := 0; y < b.Height; y++ {
  63. for x := 0; x < b.Width; x++ {
  64. buffer.WriteString(b.Get(x, y).String())
  65. }
  66. buffer.WriteString(fmt.Sprintln())
  67. }
  68. return buffer.String()
  69. }
  70. func new(width, height int, r *rand.Rand) *Board {
  71. return newPartial(0, 0, width, height, width, height, r)
  72. }
  73. func newPartial(startX, startY, width, height, totalWidth, totalHeight int, r *rand.Rand) *Board {
  74. b := &Board{
  75. Width: width,
  76. Height: height,
  77. data: make([]byte, width*height),
  78. }
  79. leftLimit := (totalWidth-maxIceWidth)/2 - startX
  80. rightLimit := totalWidth/2 + maxIceWidth/2 - startX
  81. mid := totalWidth/2 - startX
  82. for y := 0; y < height; y++ {
  83. for x := 0; x < width; x++ {
  84. switch {
  85. case r.Intn(10) > 7:
  86. b.Set(x, y, Rock)
  87. case x > leftLimit && x < rightLimit && r.Intn(maxIceWidth) >= abs(mid-x):
  88. b.Set(x, y, Ice)
  89. default:
  90. b.Set(x, y, Water)
  91. }
  92. }
  93. }
  94. return b
  95. }
  96. func sanitizeViewPort(b *Board, startCol, startRow, cols, rows int) (int, int, int, int) {
  97. if startCol > b.Width {
  98. startCol = b.Width
  99. }
  100. if startCol < 0 {
  101. cols += startCol
  102. startCol = 0
  103. }
  104. if startRow > b.Height {
  105. startRow = b.Height
  106. }
  107. if startRow < 0 {
  108. rows += startRow
  109. startRow = 0
  110. }
  111. if startCol+cols > b.Width {
  112. cols = b.Width - startCol
  113. }
  114. if startRow+rows > b.Height {
  115. rows = b.Height - startRow
  116. }
  117. if cols < 0 {
  118. cols = 0
  119. }
  120. if rows < 0 {
  121. rows = 0
  122. }
  123. return startCol, startRow, cols, rows
  124. }
  125. func abs(a int) int {
  126. if a < 0 {
  127. return -a
  128. }
  129. return a
  130. }