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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <header> Tic-Tac-Toe </header>
<div class="container">
	<div class="row">
		<div class="col" data-y="0" data-x="0"></div>
		<div class="col" data-y="0" data-x="1"></div>
		<div class="col" data-y="0" data-x="2"></div>
	</div>
	<div class="row">
		<div class="col" data-y="1" data-x="0"></div>
		<div class="col" data-y="1" data-x="1"></div>
		<div class="col" data-y="1" data-x="2"></div>
	</div>
	<div class="row">
		<div class="col" data-y="2" data-x="0"></div>
		<div class="col" data-y="2" data-x="1"></div>
		<div class="col" data-y="2" data-x="2"></div>
	</div>
</div>
<footer><div class="winner"></div><div class="btn">New Game</div></footer>
              
            
!

CSS

              
                body{
	background: #8e44ad;
}
header, footer{
	margin: 50px auto;
	text-align: center;
	font-family: 'Barlow Semi Condensed', sans-serif;
	font-size: 2.5em;
	color: #fff;
	#cross{
		padding: 0px 9px 0px 5px;
	}
	#naught{
		padding: 0px 5px 0px 7px;
	}
	#cross, #naught{
		cursor: pointer;
		font-size: 1.2em;
		display: inline-block;
		height: 75px;
		&:hover{
			border-bottom: 3px solid;
		}
	}
	.btn{
		margin: 20px auto;
		background: #833ea2;
		color: #fff;
		font-size: 0.6em;
		width: 200px;
		visibility: hidden;
		&.active{
			visibility: visible;
		}
		&:hover{
			background: #9b59b6;
		}
	}
}


.container{
	width: 300px;
	height: 300px;
	padding: 0px;
	margin: 0 auto;
	.row{
		.col{
			margin: 6px;
			width: 85px;
			height: 95px;
			text-align: center;
			cursor: pointer;
			background: #9b59b6;
			font-size: 3em;
			color: #fff;
			font-family: 'Gloria Hallelujah', sans-serif;
			line-height: 85px;
			&:hover{
				background: #833ea2;
			}
		}
	}
}


              
            
!

JS

              
                const spaces = document.querySelectorAll('.col');
const winnerText = document.querySelector('.winner');
const reset = document.querySelector('.btn');
let player = 'X'; 
let computer = 'O';
let winner = '';
let grid = [ 
		['', '', ''],
		['', '', ''],
		['', '', '']
	];
let gameEnd = false;
let moves = 0;

//function for the computers turn logic -basic dumb ai for now - need to fix for minmax
function aiLogic(){
	//loop over the grid to find an empty space
	for(var i = 0; i < 3; i++){
		for(var j = 0; j < 3; j++){
			if(grid[i][j] === ''){
	//if an empty space is found create and object and assign y and x with grid index
				return {
					yPos: i,
					xPos: j,
				};
			}
		}
	}
}

//function for ai turn
function aiTurn(){
	const ai = aiLogic();
		grid[ai.yPos][ai.xPos] = computer;
		document.querySelector('.col[data-y="' + ai.yPos + '"][data-x="' + ai.xPos + '"]').innerText = computer;
	  moves ++;
}


//function to check for winning combos!
function isGameWon(){
	//horizontal
	for(var i = 0; i < 3; i++){
		for(var j = 0; j < 3; j++){
			if(grid[i][0] !== '' && grid[i][0] === grid[i][1] && grid[i][0] === grid[i][2]){
				if(grid[i][0] === 'O'){
					winner = 'Computer Wins';
				}else{
					winner = 'You Win';
				}
				document.querySelector('.col[data-y="' + [i] + '"][data-x="' + [0] + '"]').style.background = '#833ea2';
				document.querySelector('.col[data-y="' + [i] + '"][data-x="' + [1] + '"]').style.background = '#833ea2';
				document.querySelector('.col[data-y="' + [i] + '"][data-x="' + [2] + '"]').style.background = '#833ea2';
				return grid[i][0];
			}
		}
	}	
	//vertically
	for(var i = 0; i < 3; i++){
		for(var j = 0; j < 3; j++){
			if(grid[0][j] !== '' && grid[0][j] === grid[1][j] && grid[0][j] === grid[2][j]){
				if(grid[0][j] === 'O'){
					winner = 'Computer Wins';
				}else{
					winner = 'You Win';
				}
				document.querySelector('.col[data-y="' + [0] + '"][data-x="' + [j] + '"]').style.background = '#833ea2';
				document.querySelector('.col[data-y="' + [1] + '"][data-x="' + [j] + '"]').style.background = '#833ea2';
				document.querySelector('.col[data-y="' + [2] + '"][data-x="' + [j] + '"]').style.background = '#833ea2';
				return grid[0][j];
			}
		}
	}	
	//diagonally t-l - b-r
	if(grid[0][0] !== '' && grid[0][0] === grid[1][1] && grid[0][0] === grid[2][2]){
		if(grid[0][0] === 'O'){
			winner = 'Computer Wins';
		}else{
			winner = 'You Win';
		}
		document.querySelector('.col[data-y="' + [0] + '"][data-x="' + [0] + '"]').style.background = '#833ea2';
		document.querySelector('.col[data-y="' + [1] + '"][data-x="' + [1] + '"]').style.background = '#833ea2';
		document.querySelector('.col[data-y="' + [2] + '"][data-x="' + [2] + '"]').style.background = '#833ea2';
		return grid[0][0];
	}	

	//diagonally b-l - t-r
	if(grid[2][0] !== '' && grid[2][0] === grid[1][1] && grid[2][0] === grid[0][2]){
		if(grid[2][0] === 'O'){
			winner = 'Computer Wins';
		}else{
			winner = 'You Win';
		}
		document.querySelector('.col[data-y="' + [2] + '"][data-x="' + [0] + '"]').style.background = '#833ea2';
		document.querySelector('.col[data-y="' + [1] + '"][data-x="' + [1] + '"]').style.background = '#833ea2';
		document.querySelector('.col[data-y="' + [0] + '"][data-x="' + [2] + '"]').style.background = '#833ea2';
		return grid[2][0];
	}
}


//when player clicks a square
spaces.forEach(function(input){ 
	input.addEventListener('click', function playMove() {
		//using dataset we can use these values to edit the grid array
		if(gameEnd){
			return;
		}
		const yPos = input.dataset.y;
		const xPos = input.dataset.x;
		grid[yPos][xPos] = player;
		input.innerText = player;
		moves ++;
		
		//check if its a tie
		if(!isGameWon() && moves === 9){
			gameEnd = true;
			winner = 'Its a tie';
			winnerText.innerText = winner;
			reset.classList.add('active');
			return;
		}
		//console.log(grid[yPos][xPos]);
		//check for game win after player turn
		if(isGameWon()){
			gameEnd = true;
			winnerText.innerText = winner;
			reset.classList.add('active');
			return;
		}
		//run ai turn
		aiTurn();
		
		//check for game win again after ai turn
		if(isGameWon()){
			gameEnd = true;
			winnerText.innerText = winner;
			reset.classList.add('active');
			return;
		}
		
	})
})

reset.addEventListener('click', function gameReset(){
	gameEnd = false;
	moves = 0;
	winner = '';
	winnerText.innerText = '';
	reset.classList.remove('active');
	grid = [ 
		['', '', ''],
		['', '', ''],
		['', '', '']
	];
	for(var i = 0; i < spaces.length; i ++){
		spaces[i].innerText = '';
		spaces[i].style.background = '#9b59b6';
	}
});





              
            
!
999px

Console