JSFiddle - React, Tailwind, and code Playground

by Patrick Meade

HTML

<script src="https://github.com/ondras/rot.js/raw/master/rot.min.js"></script>
<pre id="layout">Generating...</pre>

CoffeeScript

exports = {}

# layout.coffee
# Copyright 2014 Patrick Meade. All rights reserved.
#----------------------------------------------------------------------

BG_COLORS = [
  "#72b14b",     # green
  "#d5df7c",     # yellow
  "#84c5cc"      # cyan
]

exports.FORTRESS_SIZE = FORTRESS_SIZE = { width:9, height:6 }
exports.TUNNEL_TYPE = ['1', '2', '3']

exports.create = ->
  layoutWidth = ((FORTRESS_SIZE.width-1)*2)+3
  layoutHeight = ((FORTRESS_SIZE.height-1)*2)+3
  (('#' for i in [0..layoutWidth-1]) for j in [0..layoutHeight-1])

exports.initialize = (layout) ->
  newLayout = cloneLayout layout
  layoutWidth = ((FORTRESS_SIZE.width-1)*2)+3
  layoutHeight = ((FORTRESS_SIZE.height-1)*2)+3
  for i in [1..layoutHeight-2]
    for j in [1..layoutWidth-2]
      newLayout[i][j] = 'X'
  for i in [0..FORTRESS_SIZE.height-2]
    for j in [0..FORTRESS_SIZE.width-2]
      newLayout[i*2+2][j*2+2] = ' '
  return newLayout

exports.addEntryRoom = (layout) ->
  newLayout = cloneLayout layout
  newLayout[1][1] = 'E'
  return newLayout

exports.addRoom = (layout, src, dir, tunType, roomType) ->
  newLayout = cloneLayout layout
  layoutX = src.x*2+1
  layoutY = src.y*2+1
  tunnelX = layoutX+dir.x*1
  tunnelY = layoutY+dir.y*1
  newLayout[tunnelY][tunnelX] = tunType
  roomX = layoutX+dir.x*2
  roomY = layoutY+dir.y*2
  newLayout[roomY][roomX] = roomType
  return newLayout

exports.countRooms = (layout) ->
  roomCount = 0
  for i in [0..FORTRESS_SIZE.height-1]
    for j in [0..FORTRESS_SIZE.width-1]
      roomCount++ if layout[i*2+1][j*2+1] is 'E'
      roomCount++ if layout[i*2+1][j*2+1] is 'R'
      roomCount++ if layout[i*2+1][j*2+1] is 'P'
  return roomCount

exports.expandibleRooms = (layout) ->
  expandibles = []
  for i in [0..FORTRESS_SIZE.height-1]
    for j in [0..FORTRESS_SIZE.width-1]
      layoutX = j*2+1
      layoutY = i*2+1
      roomType = layout[layoutY][layoutX]
      if (roomType is 'E') or (roomType is 'R')
        north = layout[layoutY-1][layoutX]
        if (north...