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.

Details

Privacy

Go PRO Window blinds lowered to protect code. Code Editor with window blinds (raised) and a light blub turned on.

Keep it secret; keep it safe.

Private Pens are hidden everywhere on CodePen, except to you. You can still share them and other people can see them, they just can't find them through searching or browsing.

Upgrade to PRO

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.

Template

Make Template?

Templates are Pens that can be used to start other Pens quickly from the create menu. The new Pen will copy all the code and settings from the template and make a new Pen (that is not a fork). You can view all of your templates, or learn more in the documentation.

Template URL

Any Pen can act as a template (even if you don't flip the toggle above) with a special URL you can use yourself or share with others. Here's this Pen's template URL:

Screenshot

Screenshot or Custom Thumbnail

Screenshots of Pens are shown in mobile browsers, RSS feeds, to users who chose images instead of iframes, and in social media sharing.

This Pen is using the default Screenshot, generated by CodePen. Upgrade to PRO to upload your own thumbnail that will be displayed on previews of this pen throughout the site and when sharing to social media.

Upgrade to PRO

HTML

              
                <div class="board">
	<div class="row">
		<div class="square js-x0y0"></div>
		<div class="square js-x1y0"></div>
		<div class="square js-x2y0"></div>
	</div>
	<div class="row">
		<div class="square js-x0y1"></div>
		<div class="square js-x1y1"></div>
		<div class="square js-x2y1"></div>
	</div>
	<div class="row">
		<div class="square js-x0y2"></div>
		<div class="square js-x1y2"></div>
		<div class="square js-x2y2"></div>
	</div>
</div>
<div class="clearfix"></div>

<div class="js-messages"></div>
              
            
!

CSS

              
                .square {
		width: 100px;
		height: 100px;
		background-color: #eee;
		float: left;
		margin: 5px;
		font-size: 90px;
		text-align: center;
	}
	.row {
		clear: both;
	}
	.clearfix {
		clear: both;
	}
              
            
!

JS

              
                (function() {

	function Player(piece, ai) {
		this.piece = piece;
		this.controller = ai ? 'ai' : 'human';
	}

	Player.prototype.GetMove = function(board) {
		var square;
		switch (this.controller) {
			case 'human':
				square = board.GetLastClicked();
				break;
			case 'ai':
				square = this.analyseBoard(board);
				break;
		}
		return new Move(square, this);
	}

	Player.prototype.analyseBoard = function(board) {
		var validMoves = [];
		for(var x = 0; x <= 2; x++) {
			for(var y = 0; y <= 2; y++) {
				if(board.squares[x][y].owner === null) {
					validMoves.push(board.squares[x][y]);
				}
			}
		}
		return validMoves[Math.floor(validMoves.length * Math.random())];
	}

	function Move(square, player) {
		this.square = square;
		this.player = player;
	}

	function Square(domElement, board) {
		this.domElement = domElement;
		this.board = board;
		var t = this;
		domElement.addEventListener('click', function() {
			board.SetLastClicked(t);
		});
		this.owner = null; // Player
	}

	Square.prototype.Activate = function(player) {
		this.owner = player;
		this.domElement.innerHTML = player.piece;
	}

	Square.prototype.Reset = function() {
		this.owner = null;
		this.domElement.innerHTML = '';
	}

	function Board() {
		this.squares = [[null, null, null],[null, null, null],[null, null, null]];
		for(var x = 0; x <= 2; x++) {
			for(var y = 0; y <= 2; y++) {
				this.squares[x][y] = new Square(document.querySelector('.js-x' + x + 'y' + y), this);
			}
		}
		this.lastClicked = null;
	}

	Board.prototype.SetLastClicked = function(lastClicked) {
		this.lastClicked = lastClicked;
	}

	/* Retrieve the last clicked square */
	Board.prototype.GetLastClicked = function() {
		var temp = this.lastClicked;
		this.lastClicked = null;
		return temp;
	}

	Board.prototype.Reset = function() {
		this.lastClicked = null;
		for(var y = 0; y <= 2; y++) {
			for(var x = 0; x <= 2; x++) {
				this.squares[x][y].Reset();
			}
		}
	}

	Board.prototype.CheckThreeInRow = function() {
		var s = this.squares;
		for(var y = 0; y <= 2; y++) {
			if (s[0][y].owner !== null && s[0][y].owner === s[1][y].owner && s[1][y].owner === s[2][y].owner) {
				return s[0][y].owner;
			}
		}
		for(var x = 0; x <= 2; x++) {
			if (s[x][0].owner !== null && s[x][0].owner === s[x][1].owner && s[x][1].owner === s[x][2].owner) {
				return s[x][0].owner;
			}
		}
		if (s[0][0].owner !== null && s[0][0].owner === s[1][1].owner && s[1][1].owner === s[2][2].owner) {
			return s[0][0].owner;
		}
		if (s[2][0].owner !== null && s[2][0].owner === s[1][1].owner && s[1][1].owner === s[0][2].owner) {
			return s[2][0].owner;
		}
		return null;
	}

	Board.prototype.ApplyMove = function(move) {
		if(move.square !== null && move.square.owner === null) {
			move.square.Activate(move.player)
			return true;
		}
		return false;
	}

	function Game() {
		this.board = new Board();
		this.players = [new Player('O', true), new Player('X', false)];
		this.currentTurn = 0;
	}

	Game.prototype.ProcessTurn = function() {
		var currentPlayer = this.players[this.currentTurn % 2];
		var t = this;
		setTimeout(function() {
			var currentMove = currentPlayer.GetMove(t.board);
			var moveSuccess = t.board.ApplyMove(currentMove);
			if(moveSuccess) {
				t.Run();
			} else {
				t.ProcessTurn();
			}
		}, 100);	
	}

	Game.prototype.CheckWinner = function() {
		return this.board.CheckThreeInRow();
	}

	Game.prototype.CheckDraw = function() {
		return this.currentTurn > 8;
	}

	Game.prototype.Run = function() {
		if(!this.CheckWinner() && !this.CheckDraw()) {
			this.currentTurn++;
			this.ProcessTurn();
			return;
		}
		var winner = this.CheckWinner();
		if(winner === null) {
			document.querySelector('.js-messages').innerHTML = 'Draw!';
		} else {
			document.querySelector('.js-messages').innerHTML = 'Player ' + winner.piece + ' wins!';
		}
		var t = this;
		setTimeout(t.Reset.bind(t), 1618);
	}

	Game.prototype.Reset = function() {
		document.querySelector('.js-messages').innerHTML = '';
		this.board.Reset();
		this.currentTurn = 0;
		this.Run();
	}

	var game = new Game();
	game.Run();

})();
              
            
!
999px
Save your sass for CSS. Everywhere else be kind.

Console