Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="root"></div>
              
            
!

CSS

              
                
.recipe-container {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
}

.recipe {
  background: #f4edb2;  
  padding: 15px;
  margin: 15px;
  width: 300px;
  box-shadow: 2px 4px 11px 0px rgba(85, 85, 0, 0.55);
  font-family: 'Architects Daughter', cursive;
  font-size: 1.2em;
}
input {
  border-radius: 6px;
  padding: 2px;
  margin: 2px;
}
.cooking-instructions {
  background: lemonchiffon;
  padding: 5px;
}

h1 {
  font-family: Comfortaa, cursive;
  font-size: 3em;
  margin-top: 25px;
  letter-spacing: 10px;
}

.recipe > span {
	position: absolute;
	bottom: 2px;
	right: 2px;
	opacity: 0;
	transition: opacity .25s linear;
}
.recipe:hover > span {
	opacity: 1;
}
header > button {
  opacity: 0;
  transition: opacity .25s linear;
}

header:hover > button {
  opacity: 1;
}

button  {
  background: orange;
  border-radius: 6px;
  margin:2px 2px;
}
.remove {
  background: orangered;
  color: white;
  border-radius: 6px;
}

label {
	width:25%;
}

.error {
  border: 2px solid red;
  background-color: #f2daba;
}
.message {
  font-family: Comfortaa, cursive;
  font-size: 0.8em;
}

.list-group > li { 
  padding: 5px;
  list-style:none;
}

.row button.float-right {
  float: right;
}

.ingredientFields input {
  width: 50%;
}
.ingredientBtn {
  float: right;
}

              
            
!

JS

              
                
let rooms = []
let items = []

        
class World extends React.Component {
    constructor(props){
        super(props)

        let startrows =40
        let startcols = 60
        
        let arr = Array(startrows).fill().map(()=>Array(startcols).fill(0))
        this.generateRooms(arr, startrows, startcols, rooms, items)

  
        //this.generateObjects(arr, rooms, entities)

        this.state=({
            world: arr,
            height: startrows,
            width: startcols
        })

    }

    //*****Legend*******
    //*0 Wall
    //*1 Empty (room or corridor)
    //*2 Player
    //*3 Enemy (Trick)
    //*4 Treats (Health)
    //*5 Exit
    //***********************
          
//        this.generateRooms(arr, startrows, startcols, rooms, items)

componentWillMount() {
  //let arr = Array(40).fill().map(()=>Array(60).fill(0))
        this.drawRooms(this.state.world)
        this.drawItems(this.state.world)
        this.drawCorridors(this.state.world)

}
    generateRooms(arr, startrows, startcols, rooms, items) {
      console.log('--Generating rooms--')
        let roomWidth, roomHeight, x, y, itemX, itemY
        let numRooms = (Math.floor(Math.random() * 4) + 4)
        let newRoom = {}
        let newItem = {}

        //for (let i=0; i< numRooms; i++) {
        let i=0
        while (i<numRooms) {
            console.log("Building room: " + (i+1) + " of " + numRooms)
            roomWidth = Math.floor(Math.random() * 6) + 6                //width
            roomHeight = Math.floor(Math.random() * 6) + 6               //height
            x = Math.floor(Math.random() * (startcols - roomWidth))     // col origin
            y = Math.floor(Math.random() * (startrows - roomHeight))    // row origin

            itemX = x + Math.floor(Math.random()*roomWidth)
            itemY = y + Math.floor(Math.random()*roomHeight)

            newRoom = {
                "left": x,
                "top": y,
                "right": x + roomWidth,
                "bottom": y + roomHeight,
                "hcenter": x + (Math.floor(roomWidth / 2)),
                "vcenter": y + (Math.floor(roomHeight / 2))
            }
          
            let failed = false
            if (rooms != []) {
              rooms.forEach((room)=> {
                  if (this.checkIntersections(newRoom, room)) {
                      failed = true
                  }
              })

              if (!failed) {
                  rooms.push(newRoom)
                // added as this was missing
                newItem.x = itemX;
                newItem.y = itemY;
                  items.push(newItem)
                  i += 1
                
              }  
            } else {
              // first room
              rooms.push(newRoom)
              items.push(newItem)
            }
            
        }
      
    }

    checkIntersections(r1, r2) {
       return (r1.left <= r2.right && r1.right >= r2.left &&
                r1.top <= r2.bottom && r2.bottom >= r2.top)
    }

    drawRooms (arr) {
        rooms.forEach((room)=>{
            console.log("creating Room", room)

            for (let i=room.top; i<room.bottom; i++) {
                for (let j=room.left; j<room.right; j++) {
                    arr[i][j] = 1
                }
            }

        })
    }

    drawItems(arr) {
  
        items.forEach((item)=>{
            console.log("creating item")
            console.log(item)
            let x = item.x
            let y = item.y
            arr[x][y] = 1
            console.log(arr[x][y])
        })
    }

    drawCorridors(arr) {
        for (let i = 0; i< rooms.length-1; i++){
            let x = rooms[i]['hcenter']
            let y = rooms[i]['vcenter']
            let nextX = rooms[i+1]['hcenter']
            let nextY = rooms[i+1]['vcenter']
            let x1  = Math.min(rooms[i]['hcenter'], rooms[i+1]['hcenter'])
            let x2  = Math.max(rooms[i]['hcenter'], rooms[i+1]['hcenter'])
            let y1  = Math.min(rooms[i]['vcenter'], rooms[i+1]['vcenter'])
            let y2  = Math.max(rooms[i]['vcenter'], rooms[i+1]['vcenter'])
            //console.log(x1, x2, y1, y2)
            if ((Math.random() < 0.5)){
                    this.createVCorridor(arr, x, y1, y2)
                    this.createHCorridor(arr, nextY, x1, x2)
               } else {
                    this.createHCorridor(arr, y, x1, x2)
                    this.createVCorridor(arr, nextX, y1, y2)
               }
            }
    }

    createVCorridor(arr, x, y1, y2){
        for (let j = y1; j <= y2; j++) {
            arr[j][x]= 1
        }
    }

    createHCorridor(arr, y, x1, x2){
        for (let k = x1 ; k <= x2; k++) {
            arr[y][k]= 1
        }
    }


    render(){
        let fullBoard = []
        let Object_row = []
        for (var i = 0; i < this.state.height; i++) {
            Object_row = []
            for (var j= 0; j<this.state.width; j++) {
                if (this.state.world[i][j]===0){
                    Object_row.push(
                      <td  key={j + i*10}
                           className='component-tile wall'>
                      <span></span>
                      </td>
                    )
                    } else if (this.state.world[i][j]===1){
                    Object_row.push(
                      <td  key={j + i*10}
                           className='component-tile floor'>
                          <span></span>
                      </td>
                    )
                } else if (this.state.world[i][j]===2) {
                    Object_row.push(
                      <td  key={j + i*10}
                           className='component-tile char'>
                          <span></span>
                      </td>
                    )
                }
            }
            fullBoard.push(<tr key={i}>{Object_row}</tr>)
        }




        return (
            <div className='component-world'>
                <table>
                    <tbody>
                    {fullBoard}
                    </tbody>
                </table>
            </div>
        )
    }
}

World.propTypes = {

};

ReactDOM.render(
  <World />,
  document.getElementById('root')
);
              
            
!
999px

Console