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

              
                <p>Output in DevTools console…</p>
              
            
!

CSS

              
                html {
	box-sizing: border-box;
}

*,
::after,
::before {
	box-sizing: inherit;
}

              
            
!

JS

              
                function arrayEquals(a, b) {
	return JSON.stringify(a) === JSON.stringify(b);
}

class Tile {
	constructor(letter, position) {
		this.letter = letter;
		this.position = position;
	}
}

class Board {
	constructor() {
		this.tiles = [];
	}
	
	print() {
		let minX = 0;
		let minY = 0;
		let maxX = 0;
		let maxY = 0;
		
		this.tiles.forEach((tile) => {
			minX = Math.min(minX, tile.position[0]);
			minY = Math.min(minY, tile.position[1]);
			maxX = Math.max(maxX, tile.position[0]);
			maxY = Math.max(maxY, tile.position[1]);
		});
		
		const grid = [];

		for (let x = minX; x <= maxX; x += 1) {
			for (let y = minY; y <= maxY; y += 1) {
				grid[x] = grid[x] || [];
				grid[x][y] = board.letterAt([x, y]);
			}
		}

		let str = '';

		for (let y = minY; y <= maxY; y += 1) {
			for (let x = minX; x <= maxX; x += 1) {
				str += `|${grid[x][y]}`;
			}
			str += '|\n';
		}
		
		console.log(str);

		return str;
	}
	
	letterAt(position) {
		const tile = this.tiles.find((t) => arrayEquals(t.position, position));
		return tile ? tile.letter : ' ';
	}

	// Add a word to the board, starting at a given position.
	placeWord(word, position, direction = 'row') {
		// Keep track of the next position as we add each letter.
		let nextPosition = [...position];

		// Create new tiles for each letter.
		const newTiles = word
			.toUpperCase()
			.split('')
			.map((letter, i) => {
				// Create a new tile for each letter.
				const tile = new Tile(letter, nextPosition);
				// Increment the next position's x or y depending on direction.
				nextPosition = [...nextPosition];
				nextPosition[direction === 'row' ? 0 : 1] += 1;
				// console.log('nextPosition', nextPosition.join(','));
				// Return final tile.
				return tile;
			});		

		// Add new tiles to the board.
		this.tiles = this.tiles.concat(newTiles);
		
		return true;
	}
	
	// Where can we put a new word so its builds off
	// another word already on the board?
	findPlacesToPut(word) {
		return word.toUpperCase().split('').reduce((result, letter, i) => {
			const tiles = this.tiles.filter((t) => t.letter === letter);

			tiles.forEach((tile) => {
				// Is there free space around this tile?
				const x = tile.position[0];
				const y = tile.position[1];
				const spaces = [
					board.letterAt([x - 1, y]),
					board.letterAt([x + 1, y]),
					board.letterAt([x, y - 1]),
					board.letterAt([x, y + 1]),
				];
				
				// TODO: Fix space detection.
				// if (spaces.some((letter) => letter !== ' ')) {
				// 	return;
				// }

				const direction = spaces[0] !== ' ' && spaces[1] !== ' '
					? 'row'
					: 'column';

				// Increment the next position's x or y depending on direction.
				const nextPosition = [...tile.position];
				nextPosition[direction === 'row' ? 0 : 1] -= i;

				result.push({
					letter,
					direction,
					position: nextPosition,
				});
			});

			return result;
		}, []);
	}
	
	// TODO: What words are on the board?
}

// ------------------------------------------------------------
// TRY IT OUT!!!
// ------------------------------------------------------------

const board = new Board();

board.placeWord('Tanner', [0, 0], 'row');
// board.print();

let places = board.findPlacesToPut('Brianne');
let place = places[0];
board.placeWord('Brianne', place.position, place.direction);
board.print();

// TODO: Fix space detection.
// places = board.findPlacesToPut('Jackson');
// place = places[0];
// board.placeWord('Jackson', place.position, place.direction);
// board.print();

// TODO: Fix space detection.
// places = board.findPlacesToPut('Ruby');
// place = places[0];
// board.placeWord('Ruby', place.position, place.direction);
// board.print();

// places.forEach((p, i) => {
// 	const tmp = new Board();
// 	console.log('\nPLACE ', i);
// 	console.log(...p.position);
// 	tmp.placeWord('Tanner', [0, 0], 'row');
// 	tmp.placeWord('Brianne', p.position, p.direction);
// 	tmp.print();
// });

              
            
!
999px

Console