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. 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

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="game">

	<div id="winner-burst" :class="[currentTurnColor, gameOver && 'burst']"></div>

	<div class="flex">
		<p v-html="message" role="status"></p>
		<button :disabled="!areThereMoves" @click="startNewGame">New game</button>
	</div>

	<div id="button-board">
		<div v-for="i in 8" :data-column="i - 1" @click.stop="dropPiece">
			<button :data-column="i - 1" @click.stop="dropPiece" @keydown="handleMoveCursor" @mouseover="handleHover" :aria-label="`Drop ${currentTurnColor} piece in column ${i}`" :tabindex="gameOver || isADraw ? '-1' : '0'">
				<span></span>
			</button>
			<div class="slot" :hidden="gameOver || isADraw" aria-hidden="true">
				<div :class="currentTurnColor">
					{{ getPieceIcon(currentTurnColor) }}
				</div>
			</div>
		</div>
	</div>

	<div id="board">
		<template v-for="row, rowNumber in grid">
			<template v-for="slot, slotNumber in row">
				<div class="slot shadow" @click="dropPiece" @mouseover="handleSlotHover" :style="slot.color && `animation: drop 0.05s forwards steps(6)`" :data-row="rowNumber" :data-column="slotNumber" :key="`${rowNumber}${slotNumber}`" :aria-label="`${slot.color && slot.color + ' piece' || 'Blank slot'} in column ${slotNumber +1} of 8, row ${rowNumber +1} of 6.` ">
					<div v-if="slot.color" :class="[slot.color, gameOver && currentTurnColor != slot.color && 'loser']" aria-hidden="true">
						{{ getPieceIcon(slot.color) }}
					</div>
				</div>
			</template>
		</template>
	</div>
</div>
              
            
!

CSS

              
                $yellow: #ffd100;
$orange: #ff6a13;
$lightBlue: #7ba7bc;
$lightGray: #a7a8aa;
$darkGray: #53565a;
$white: #fff;
$red: #e4002b;

$breakpoint: 40rem;
$unit: 3vmin;

*,
*:before,
*:after {
	box-sizing: inherit;
	font-family: inherit;
	color: inherit;
	position: inherit;
}

*:focus {
	outline: 0.15rem dotted $orange;
	outline-offset: 0.15rem;
}

[hidden] {
	display: none !important;
}

body {
	font-family: "Baloo Chettan 2";
	margin: 0;
	min-height: 100vh;
	box-sizing: border-box;
	color: $darkGray;
	font-size: 2rem;
	position: relative;
}

.red,
.yellow {
	text-transform: capitalize;
	margin-right: 0.1em;
}

.red {
	color: $red;
}

.yellow {
	color: $yellow;
}

button {
	cursor: pointer;
}

#game {
	width: 100%;
	padding: 1rem;
	margin: auto;
	min-height: 100vh;

	@media (min-width: $breakpoint) {
		max-width: 76vmin;
		padding: 0;
		padding-top: 10vmin;
		min-height: calc(100vh - 10vmin);
	}

	.flex {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 1rem;

		p {
			font-size: calc(1rem + 3vmin);
			margin: 0;
		}

		button {
			font-size: 1rem;
			padding: 0.25rem 0.5rem;
			border-radius: 4px;
			border: 2px solid $lightGray;

			&:disabled {
				opacity: 0.5;
				cursor: not-allowed;
			}

			&:not(:disabled):hover {
				filter: brightness(0.9);
			}
		}
	}
}

#button-board {
	display: none;
	grid-template-columns: repeat(8, 1fr);
	grid-gap: $unit * 0.666;
	padding: $unit $unit 0;

	@media (min-width: $breakpoint) {
		display: grid;
	}

	[data-column] {
		position: relative;
	}

	button {
		padding: $unit 0;
		border: 0;
		text-align: center;
		width: 100%;
		display: flex;
		align-items: flex-end;
		justify-content: center;
		background: transparent;

		span {
			height: 0;
			border-left: 1vmin solid transparent;
			border-right: 1vmin solid transparent;
			border-bottom: none;
			border-top: 1vmin solid $lightGray;
			text-align: center;
			pointer-events: none;
			display: block;

			&::after {
				content: "";
				width: 4vmin;
				height: 4vmin;
				border-radius: 4vmin;
				border: 3px solid $lightGray;
				left: -2vmin;
				top: -2.7vmin;
				display: block;
				position: absolute;
			}
		}

		&:focus,
		&:hover {
			outline: none;

			& + .slot {
				display: block;
				left: 0;
				z-index: 2;
				pointer-events: none;
				position: absolute;
				bottom: $unit * 0.333;

				&:before {
					content: "👇";
					position: absolute;
					font-size: calc(1rem + 3vmin);
					width: 90%;
					left: 0;
					top: -0.8em;
					text-align: center;
					z-index: 2;
				}

				div {
					animation: none;
					z-index: 1;
					box-shadow: -0.1rem -0.1rem 1px rgba($white, 0.2) inset;

					&:before {
						box-shadow: -0.1rem -0.1rem 1px rgba($white, 0.2) inset,
							0.2rem 0.2rem 0.3rem rgba($darkGray, 0.2) inset;
					}
				}
			}
		}
	}

	.slot {
		position: absolute;
		top: 0;
		display: none;
	}
}

#board {
	margin: auto;
	display: grid;
	grid-gap: $unit;
	grid-template-columns: repeat(8, 1fr);
	justify-content: center;
	overflow: hidden;
	border-radius: 0.25rem;
	padding: $unit;
	background: $lightBlue;
	mix-blend-mode: multiply;
	box-shadow: 0.2rem 0.3rem 0.4rem rgba($darkGray, 0.3),
		-0.25rem -0.25rem 0.25rem rgba(darken($lightBlue, 10%), 0.5) inset,
		0.25rem 0.25rem 0.25rem rgba(lighten($lightBlue, 10%), 0.8) inset;

	@media (min-width: $breakpoint) {
		grid-gap: $unit * 0.666;
	}
}

.slot {
	width: $unit * 2.666;
	height: $unit * 2.666;
	background: $white;
	border-radius: $unit * 2.666;
	padding: 0;
	border: none;
	margin: auto;

	@media (min-width: $breakpoint) {
		width: $unit * 2.333;
		height: $unit * 2.333;
	}

	&.shadow {
		box-shadow: 0.15rem 0.15rem 1px rgba(lighten($lightBlue, 15%), 0.5),
			-0.15rem -0.15rem 1px rgba(darken($lightBlue, 15%), 0.5),
			0.2rem 0.2rem 0.2rem rgba($darkGray, 0.3) inset;
	}

	.red,
	.yellow {
		width: inherit;
		height: inherit;
		border-radius: inherit;
		border: none;
		pointer-events: none;
		box-shadow: -0.1rem -0.1rem 1px rgba($white, 0.2) inset,
			0.1rem 0.1rem 1px rgba($darkGray, 0.2) inset,
			0.2rem 0.2rem 0.3rem rgba($darkGray, 0.3) inset;
		position: relative;
		font-size: 4vmin;
		line-height: 7.2vmin;
		text-align: center;
		color: rgba(0, 0, 0, 0.4);

		&.loser {
			filter: opacity(0.7) grayscale(60%) contrast(0.4);
		}

		&:before {
			content: "";
			width: 70%;
			height: 70%;
			border-radius: inherit;
			box-shadow: inherit;
			left: 15%;
			top: 15%;
			position: absolute;
		}
	}

	.red {
		background: $red;
	}

	.yellow {
		background: $yellow;
	}
}

#winner-burst {
	$timing: 0.7s;
	transition: all $timing ease;
	width: 150vmax;
	height: 150vmax;
	border-radius: 75vmax;
	position: absolute;
	left: calc(50% - 75vmax);
	z-index: 3;
	top: calc(50% - 75vmax);
	transform-origin: center;
	transform: scale(0);
	pointer-events: none;

	&.red {
		background-color: $red;
	}

	&.yellow {
		background-color: $yellow;
	}

	&.burst {
		transform: scale(1);
		animation: fadeOut $timing ease forwards;
		animation-delay: $timing;
	}
}

@keyframes drop {
	from {
		transform: translateY(-54vmin);
	}
	to {
		transform: translateY(0);
	}
}

@keyframes fadeOut {
	from {
		opacity: 1;
	}
	to {
		opacity: 0;
	}
}

              
            
!

JS

              
                //Tried auto-generating this grid to keep the syntax shorter,
//but ran into issues with object duplication (when one updates, all update),
//so I just stuck with this ¯\_(ツ)_/¯
const emptyGrid = [
	[{}, {}, {}, {}, {}, {}, {}, {}],
	[{}, {}, {}, {}, {}, {}, {}, {}],
	[{}, {}, {}, {}, {}, {}, {}, {}],
	[{}, {}, {}, {}, {}, {}, {}, {}],
	[{}, {}, {}, {}, {}, {}, {}, {}],
	[{}, {}, {}, {}, {}, {}, {}, {}]
];

const game = new Vue({
	el: "#game",

	data: () => ({
		grid: emptyGrid,
		redTurn: true, //if false, it's yellow's turn. Better to use a boolean than strings.
		gameOver: false
	}),

	//Prevent users from navigating away from a game accidentally
	mounted() {
		window.onbeforeunload = () => {
			if (this.areThereMoves) return "Are you sure you want to leave this game?";
			//if we return nothing here (just calling return;) then there will be no pop-up question at all
		};
	},

	methods: {
		dropPiece(e) {
			if (this.gameOver) return; //No more plays once the game is done
			const column = e.target.dataset.column;

			for (let i = this.grid.length - 1; i >= 0; i--) {
				if (!this.grid[i][column].color) {
					this.grid[i][column].color = this.currentTurnColor;
					this.refreshGrid();
					const isGameOver = this.victoryCheck();

					if (isGameOver) {
						this.gameOver = true;
					} else {
						this.switchTurn();
					}
					break;
				}
			}
		},

		handleMoveCursor(e, x) {
			const currentBtn = parseInt(e.target.dataset.column);
			if (!e.key) return; //Do nothing if we don't have an identified key press

			if (["ArrowRight", "ArrowDown"].includes(e.key)) {
				this.focusAdjacentBtn(e, currentBtn, 1);
			} else if (["ArrowLeft", "ArrowUp"].includes(e.key)) {
				this.focusAdjacentBtn(e, currentBtn, -1);
			} // Do nothing if the key pressed was not an arrow key
		},

		//Move focus to next or previous button
		focusAdjacentBtn(e, currentBtn, direction) {
			e.preventDefault();
			if (!direction) return;
			const adjacentBtn = document.querySelector(
				`button[data-column="${currentBtn + direction}"]`
			);
			if (adjacentBtn) {
				adjacentBtn.focus();
			} else if (direction > 0) {
				document.querySelector(`button[data-column]`).focus(); //wrap to beginning if at the end
			} else {
				document.querySelector(`button[data-column="7"]`).focus(); //wrap to end if at the beginning
			}
		},

		//Helps prevent multiple "drop" cursors when hovering after a button has focus
		handleHover(e) {
			e.target.focus();
		},

		//Adds the "drop" indicator above a hovered column
		handleSlotHover(e) {
			const column = e.target.dataset.column;
			document
				.querySelector(`#button-board button[data-column="${column}"]`)
				.focus();
		},

		switchTurn() {
			this.redTurn = !this.redTurn;
		},

		//Necessary to get Vue to recognize changes, because changes to an object in an array of objects won't trigger a re-render
		refreshGrid() {
			this.grid = [...this.grid];
		},

		getPieceIcon(color) {
			return color == "yellow" ? "⭐" : "🔴";
		},

		//Runs through all possible win scenarios (and some impossible) to see if the game is over
		victoryCheck() {
			let isGameOver = false;
			//Loop over every victory method (some impossible, to keep the code simple; hence the optional chaining)
			this.grid.forEach((row, rowIndex) => {
				row.forEach((column, colIndex) => {
					const slotColor = column.color;
					//If we already know the game is over, get outta there
					if (!slotColor || isGameOver) return;
					if (
						//Horizontal row
						(slotColor == this.grid?.[rowIndex]?.[colIndex + 1]?.color &&
							slotColor == this.grid?.[rowIndex]?.[colIndex + 2]?.color &&
							slotColor == this.grid?.[rowIndex]?.[colIndex + 3]?.color) ||
						//Vertical column
						(slotColor == this.grid?.[rowIndex - 1]?.[colIndex]?.color &&
							slotColor == this.grid?.[rowIndex - 2]?.[colIndex]?.color &&
							slotColor == this.grid?.[rowIndex - 3]?.[colIndex]?.color) ||
						//Diagonal ascending
						(slotColor == this.grid?.[rowIndex - 1]?.[colIndex + 1]?.color &&
							slotColor == this.grid?.[rowIndex - 2]?.[colIndex + 2]?.color &&
							slotColor == this.grid?.[rowIndex - 3]?.[colIndex + 3]?.color) ||
						//Diagonal descending
						(slotColor == this.grid?.[rowIndex + 1]?.[colIndex + 1]?.color &&
							slotColor == this.grid?.[rowIndex + 2]?.[colIndex + 2]?.color &&
							slotColor == this.grid?.[rowIndex + 3]?.[colIndex + 3]?.color)
					) {
						isGameOver = true;
					}
				});
			});
			return isGameOver;
		},

		startNewGame() {
			if (this.areThereMoves) {
				const areYouSure = confirm(
					"Start a new game? This one will be lost four-ever :)"
				);
				if (!areYouSure) return;
			}
			this.grid.forEach(row => {
				row.forEach(column => {
					column.color = null;
				});
			});
			this.refreshGrid();
			this.gameOver = false;
			this.redTurn = true;
		}
	},

	computed: {
		currentTurnColor() {
			return this.redTurn ? "red" : "yellow";
		},

		message() {
			if (this.isADraw) {
				return `<b style="color: inherit">DRAW!</b>`;
			} else if (this.gameOver) {
				return `<b class="${this.currentTurnColor}">${this.currentTurnColor} WINS! 🎉</b>`;
			} else {
				return `<b class="${this.currentTurnColor}">${this.currentTurnColor}</b>’s turn`;
			}
		},

		//Monitor whether at least one move has been made
		areThereMoves() {
			let moves = false;
			this.grid.forEach(row => {
				row.forEach(column => {
					if (column.color) moves = true;
				});
			});
			return moves;
		},

		//Monitor whether the board is full and the game is not over
		isADraw() {
			let draw = true;
			this.grid.forEach(row => {
				row.forEach(column => {
					if (!column.color) draw = false;
				});
			});
			return draw;
		}
	}
});

              
            
!
999px

Console