Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.collection.mutable.{ArrayBuffer, Buffer}
- import scala.util.Random
- class Square(grid: WaveFunctionCollapse, val location: GridPos):
- var tile: Option[String] = None
- val boundary = Buffer.fill[Option[Char]](8)(None)
- var plausibleTiles = ArrayBuffer[String]() ++ AllTiles.allStrings
- /**
- * Fixes a single element in the boundary and removes incompatible tiles.
- * @param placeOnBoundary index of the element on the boundary
- * @param value value to be set in the boundary
- */
- def fix(placeOnBoundary: Int, value: Char): Unit =
- this.boundary(placeOnBoundary) = Some(value)
- this.plausibleTiles = this.plausibleTiles.filter( _(placeOnBoundary) == value)
- end fix
- /**
- * Calculates the entropy of the tile, that is - the number of plausible tiles.
- * @return the number of plausible tiles
- */
- def entropy: Int = plausibleTiles.size
- /**
- * Selects a random tile from all plausible tiles. Updates neighboring tiles if necessary.
- */
- def collapse(): Unit =
- val chosenTile = Random.shuffle(this.plausibleTiles).head
- this.tile = Some(chosenTile)
- var index = 0
- while index <= 7 do
- propagateChange(index, this.tile.get(index))
- index += 1
- end collapse
- /**
- * Propagates the change in this square's boundary to boundaries and plausible tiles of neighboring squares.
- * @param boundaryIndex index in this squares boundary
- * @param fixedValue the value to be set in the boundary
- */
- def propagateChange(boundaryIndex: Int, fixedValue: Char): Unit =
- for neighbor <- neighborBoundaries do
- if neighbor.mine == boundaryIndex && !(this.location.x == 0 && neighbor.dir == West) && !(this.location.x == 9 && neighbor.dir == East) && !(this.location.y == 0 && neighbor.dir == North) && !(this.location.y == 9 && neighbor.dir == South) then
- this.grid.elementAt(this.location.neighbor(neighbor.dir)).fix(neighbor.theirs, fixedValue)
- end propagateChange
- end Square
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement