Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>tic-tac-toe</title>
	<meta name="description" content="tic-tac-toe">
	<link rel="stylesheet" href="styles.css">
	<script type="text/javascript" src="scripts.js"></script>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
	<div id="game">
		<h1>tic-tac-toe</h1>
		<h2 id="player">player X</h2>
		<div class="main">
			<div class="square"></div>
			<button value="0" onclick="myFunction(this.value)"></button>
			<button value="1" onclick="myFunction(this.value)"></button>
			<button value="2" onclick="myFunction(this.value)"></button>
			<button value="3" onclick="myFunction(this.value)"></button>
			<button value="4" onclick="myFunction(this.value)"></button>
			<button value="5" onclick="myFunction(this.value)"></button>
			<button value="6" onclick="myFunction(this.value)"></button>
			<button value="7" onclick="myFunction(this.value)"></button>
			<button value="8" onclick="myFunction(this.value)"></button>
		</div>
		<button class="button-style" onclick="window.location.reload();">restart</button>
		<div id="grid"><a href="https://github.com/matikka96" target="blank">github</a></div>
	</div>
	<div id="message">
		<div id="message-box">
			<h1 id="message-text">hello</h1>
			<button class="button-style" id="message-button" onclick="messageClose()">ok</button>
		</div>
	</div>
</body>
</html>
              
            
!

CSS

              
                * {
	box-sizing: border-box;
	margin: 0;
	padding: 0;
	scroll-behavior: smooth;
	outline: 0px solid black;
	text-align: center;
	font-family: sans-serif;
}
h1 {padding-top: 2vh;}
h2 {
	padding: 1vh 0 2vh 0;
	color: grey;
}
body {
	display: flex;
	flex-direction: column;
	justify-content: center;
	height: 100vh;
}
#game {
	margin: 0 4vw;
	filter: blur(0px);
	animation-duration: 1s;
	transition-duration: 0.4s;
}
.main {
	width: 400px;
	margin: 0 auto;
	display: flex;
	flex-wrap: wrap;
}
.main button {
	height: 133px;
	width: 33.3%;
	line-height: 133px;
	font-size: 80px;
	display: block;
	background-color: lightgrey;
	outline: 4px solid black;
	cursor: pointer;
	transition-duration: 0.2s;
}
.main button:hover {opacity: 0.8;}
.button-style {
	display: inline-block;
	padding: 1.5vh 2vw;
	background-color: gold;
	border-radius: 5px;
	font-size: 1.5em;
	text-transform: uppercase;
	font-weight: bolder;
	cursor: pointer;
	border: 4px solid black;
	margin: 3vh 0 2vh 0;
	transition-duration: 0.2s;
}
.button-style:hover {padding: 1.5vh 3vw;}
#message {
	display: none;
	flex-direction: column;
	justify-content: center;
	position: fixed;
	top: 0;
	width: 100vw;
	height: 100vh;
}
#message-box {
	margin: auto;
	width: 300px;
	background-color: limegreen;
	border: 4px solid black;
	border-radius: 5px;
}
@media screen and (max-width: 500px) {
	.main {width: 100%;}
}
@media screen and (max-height: 650px) {
	.main button {
		height: 20vh;
		line-height: 20vh;
		font-size: 12vh;
	}
	body {display: block;}
}
              
            
!

JS

              
                
// Function scans 'grid' for possible winning combinations
function scanForStrike() {
	let result = 0;			// 0 = winning combination not found, 1 = found
	let num1, num2, num3;
	for (var i = 0; i < outcomes.length; i++) {
		num1 = outcomes[i][0]-1;
		num2 = outcomes[i][1]-1;
		num3 = outcomes[i][2]-1;
		if (grid[num1] == grid[num2] && grid[num1] == grid[num3] && grid[num1] != 0) {
			result = 1;
			continue;
		}
	}
	return result;
}

// Function displays notification. Parameters define message text and button text.
function messageOpen(messageText, messageButton) {
	document.getElementById("message-text").innerHTML = messageText;
	document.getElementById("message-button").innerHTML = messageButton;
	document.getElementById("message").style.display = "flex";
	document.getElementById("game").style.filter = "blur(15px)";
}
// Function closes notification
function messageClose(status) {
	if (document.getElementById("message-button").textContent == 'reload') {
		window.location.reload();
	}
	document.getElementById("message").style.display = "none";
	document.getElementById("game").style.filter = "blur(0px)";
}

// Main function, that handles all the visuals and game engine.
function myFunction(index) {
	if (grid[index] != 0) {
		messageOpen("no can do", "ok");
		return;
	}
	document.getElementsByTagName("button")[index].innerHTML = player;
	var tempPlayer = player;
	if (player == "X") {
		grid[index] = 1;
		document.getElementsByTagName("button")[index].style.backgroundColor = "tomato";
		player="O";
		document.getElementById("player").innerHTML = "player " + player;
	} else {
		grid[index] = 2;
		document.getElementsByTagName("button")[index].style.backgroundColor = "limegreen";
		player = "X";
		document.getElementById("player").innerHTML = "player " + player;
	}
	var result = scanForStrike();	// Check for winning combination
	if (result == 1) {
		messageOpen("player "+tempPlayer+" won!", "reload"); 
	}
	if (grid.includes(0) == false) {
		messageOpen("draw", "reload"); 
	}
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

// Game starts with player "X"
var player = "X";

// Empty grid
var grid = [0,0,0,0,0,0,0,0,0];

// All possible winning combinations, total of 8.
var outcomes = [[1,2,3], [4,5,6], [7,8,9], [1,4,7], 
				[2,5,8], [3,6,9], [1,5,9], [7,5,3]];

              
            
!
999px

Console