css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              @import 'https://fonts.googleapis.com/css?family=Noto+Sans:400,700';

$tiny: 1.5rem;
$small: 12vmin;
$large: 65vmin;

* {	
	box-sizing: border-box;
	font-family: 'Noto Sans';
}

body {
	margin: 0;
	font-size: calc(14px + .5vw);
	line-height: 1.2;
	background: #fafafa;
	color: #333;
}

#app {
	width: 100vw;
	min-height: 100vh;
	display: grid;
	grid-template-columns: 1fr;
	grid-template-rows: auto;
	justify-content: center;
	align-items: center;
	button {
		padding: .6rem 1rem;
		justify-self: center;
		border-radius: 100px;
		border: 8px solid #fafafa;
		background-color: #999;
		color: #fff;
		font-size: 1rem;
		&:focus {
			box-sizing: border-box!important;
			border: 8px double #fafafa;
			border-radius: 200px;
			outline: none;
		}
	}
}

#gameBoard {
	display: grid;
	width: $large;
	height: $large;
	box-sizing: initial;
	justify-self: center;
}

#optionsBar {
	display: flex;
	min-width: 65vmin;
	justify-content: space-between;
	color: #666;
	justify-self: center;
	align-items: center;
	min-height: 72px;
	label {
		font-size: .8em;
		margin: 0 .5em;
		display: flex;
		text-align: right;
		justify-content: flex-end;
		align-items: center;
	}
	input[type=checkbox]{
		position: absolute;
		left: -200vmax;
		top: -200vmax;
		&:checked ~ label .toggle-outer{
			background-color: rgb(51, 153, 68);
			border-color: rgb(51, 153, 68);
			& .toggle-inner {
				left: calc(100% - 1.5rem);
			}
		}
	}
	.toggle {
		display: flex;
		align-items: center;
		.toggle-outer {
			position: relative;
			transition: all .3s ease;
			box-sizing: initial;
			height: $tiny;
			width: $tiny * 2;
			border-radius: 100px;
			border: none;
			background-color: #999;
			border: 4px solid #999;
			color: #fff;
			font-size: 1rem;
			display: flex;
			align-items: center;
			justify-content: flex-start;
			padding: 0;
			margin-left: .5rem;
		}
		.toggle-inner {
			transition: all .3s ease;
			width: $tiny;
			height: $tiny;
			border-radius: 2rem;
			background-color: #ccc;
			position: absolute;
			left: 0;
		}
	}
}

#buttonBoard {
	display: flex;
	min-width: 65vmin;
	justify-self: center;
	justify-content: space-between;
	margin: 2rem;
}

.tile {
	box-sizing: border-box;
	display: flex;
	justify-content: center;
	align-items: center;
	text-align: center;
	overflow: hidden;
	border-left: 1px solid #fafafa;
	border-top: 1px solid #fafafa;
	&.absorbed {
		border: none!important;
	}
	span {
		pointer-events: none;
		transform: translateY(100%);
		transition: all .4s cubic-bezier(0.57,-0.1, 0, 1.38);
		opacity: 0;
		&.pop-in {
			transform: translateY(0%);
			opacity: 1;
		}
	}
	&.buttonTile {
		width: $small;
		height: $small;
		border-radius: $small;
	}
}
            
          
!
            
              //CHANGE THIS NUMBER to alter the board size! ❤️🔷⭐️🍀
const size = 12;



const ColorButton = (props) => {
	return (
	<button	className="tile buttonTile" style={{backgroundColor: props.color.rgb}} onClick={props.handleClick}>
		 <span className={props.colorblind ? 'pop-in' : undefined}>{props.color.symbol}</span>
		</button>
	);
}

const ColorblindToggle = (props) => {
	return(
		<div className="toggle">
			<input type="checkbox" id="colorblindMode" checked={props.colorblind}/>
			<label htmlFor="colorblindMode" onClick={props.handleColorblindToggle}>Colorblind<br/>Mode
				<div className="toggle-outer">
					<div className="toggle-inner"></div>
				</div>
			</label>
		</div>
	);
}

const Tile = (props) => {
	return (
		<div className={props.absorbed ? 'absorbed tile' : 'tile'} style={{backgroundColor: props.color.rgb}} data-key={props.tileKey}>
			<span className={props.colorblind ? 'pop-in' : undefined}>{props.color.symbol}</span>
		</div>
	);
}

class GameBoard extends React.Component {
	constructor(props){
		super(props);
			this.state = {
				boardSize: size,
				board: [],
				clicks: 0,
				colorblindMode: false,
				//You could theoretically add more colors here
				colors: [
					{
						rgb: 'rgb(255, 50, 50)', //red 
						symbol: '❤️'
					},
					{
						rgb: 'rgb(24, 187, 255)', //blue 
						symbol: '🔷'
					},
					{
						rgb: 'rgb(255, 224, 17)', //yellow 
						symbol: '⭐️'
					},
					{
						rgb: 'rgb(51, 153, 68)', //green 
						symbol: '🍀'
					},	
					
					// UNCOMMENT to add more (completely arbitrary)colors! 🍊🎀
					
					// {
					// 	rgb: 'rgb(235, 168, 11)', //orange 
					// 	symbol: '🍊'
					// },
					// {
					// 	rgb: 'rgb(255, 105, 180)', //pink 
					// 	symbol: '🎀'
					// },
				],
				currentColor: ''
			}
    }
	
	generateGameBoard = () => {
		const x = this.state.boardSize;
		const board = [];
		let key = 1;
		for(let i=0; i < x*x; i++){
				const randomColor = Math.floor(Math.random() * this.state.colors.length);
				board.push({ color: this.state.colors[randomColor], key: key, absorbed: false });
				key++;
		}
		this.setState({ 
			board: board,
			currentColor: board[0].color.rgb,
			clicks: 0,
		});
	}
	
	returnSymbol = (match) => {
		let symbol;
		this.state.colors.filter(color => {
			color.rgb == match ? symbol = color.symbol : null;
		});
		return symbol;
	}
	
	componentWillMount(){
		this.generateGameBoard();
	}

	componentDidMount(){
		this.handleClick();
	}

	getTheAbsorbed = () => {
		const size = this.state.boardSize;
		let currentPool = [];											
		(roundup = () => {
			this.state.board.forEach((tile, i) => {
				if(
					((tile.color.rgb == this.state.currentColor
						&& (
								 (i % size != 0 && currentPool.includes(this.state.board[i-1]))
							|| ((i +1) % size != 0 && currentPool.includes(this.state.board[i+1]))
							|| (this.state.board[i+size] && currentPool.includes(this.state.board[i+size]))
							|| (this.state.board[i-size] && currentPool.includes(this.state.board[i-size]))
						)	
					)||(
						i === 0
					))
				&& (!currentPool.includes(tile))){
						currentPool.push(tile);
						roundup();
				} 
			})
		})();
		return currentPool;
	}
	
	propagate = (e, arr) => {
		const size = this.state.boardSize;
		const color = e ? e.target.style.backgroundColor : this.state.currentColor;
		(roundup = () => {
			this.state.board.forEach((tile, i) => {
				if(
					((tile.color.rgb == color
						&& (
								 (i % size != 0 && arr.includes(this.state.board[i-1]))
							|| ((i +1) % size != 0 && arr.includes(this.state.board[i+1]))
							|| (this.state.board[i+size] && arr.includes(this.state.board[i+size]))
							|| (this.state.board[i-size] && arr.includes(this.state.board[i-size]))
						)	
					)||(
						i === 0
					))
				&& (!arr.includes(tile))){
						arr.push(tile);
						roundup();
				} 
			})
		})();
		return arr;
	}
	
	
	
	handleClick = (e) => {
		const clicked = e ? e.target.style.backgroundColor : this.state.currentColor;
		const size = this.state.boardSize;
		if(clicked != this.state.currentColor || !e){	
			
			const prevPool = this.getTheAbsorbed();			
			const currentPool = this.propagate(e, prevPool);
						
			const newBoard = this.state.board.map((tile, i) => {
				if(currentPool.includes(tile)){
					return { color: {rgb: clicked, symbol: this.returnSymbol(clicked)}, key: tile.key, absorbed: true }
				} else {
					return tile;
				}
			});
			this.setState({
				board: newBoard,
				currentColor: clicked,
				clicks: e ? this.state.clicks + 1 : this.state.clicks
			});					
						
			this.endgameCheck();

		} else {
			console.log('The corner is already the clicked color. Choose another.')
		}
	}
	
	endgameCheck(){
		const size = this.state.boardSize;
		setTimeout(()=>{
			const absorbed = this.state.board.filter(tile => {
				if (tile.color.rgb == this.state.currentColor){ 
					return tile.color.rgb;
				} else {
					return;
				}
			});
			if(absorbed.length === size * size){
				alert(`You won a ${size}x${size} board in ${this.state.clicks} clicks! (Hint: refresh to start a new game, or change the "size" constant at the top of the JS window to a different number!)`);
			}
		}, 200);
	}
	
	handleColorblindToggle = (e) => {
		e.preventDefault();
		this.setState({ colorblindMode: !this.state.colorblindMode });
	}
	
	render(){
		const style = {
			gridTemplateColumns: `repeat(${this.state.boardSize}, 1fr)`,
			gridTemplateRows: `repeat(${this.state.boardSize}, 1fr)`
		}
		return(
			<React.Fragment>
				<div id="optionsBar">
					<h2>{this.state.clicks}</h2>
					<ColorblindToggle handleColorblindToggle={this.handleColorblindToggle} colorblind={this.state.colorblindMode}/>
				</div>
				<div id="gameBoard" style={style}>
					{this.state.board.map(tile => {
						return( <Tile color={tile.color} key={tile.key} tileKey={tile.key} currentColor={this.state.currentColor} colorblind={this.state.colorblindMode} absorbed={tile.absorbed}/>);
					})}
				</div>
				<div id="buttonBoard">
					{this.state.colors.map(color => {
						return(<ColorButton color={color} handleClick={this.handleClick} key={color.rgb} size={this.state.size} colorblind={this.state.colorblindMode} />);
					})}
				</div>
			</React.Fragment>
		);
	}
}

ReactDOM.render(<GameBoard />, document.querySelector('#app'));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console