cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
                  <div class="container">
      <p>Choose</p>
      <span class="pick">O</span>
      <span class="pick">X</span>
      
      <div id="board">
	<div id="flash_message"></div>    	
	<div id="0" class="block"></div>
	<div id="1" class="block"></div>
	<div id="2" class="block"></div>
	<div id="3" class="block"></div>
	<div id="4" class="block"></div>
	<div id="5" class="block"></div>
	<div id="6" class="block"></div>
	<div id="7" class="block"></div>
	<div id="8" class="block"></div>      
      </div>
      
      <button id="reset">Reset</button>
    </div>    
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Roboto');

body, html {
    margin: 0;
    width: 100%;
}

body {
    background-color: #777;
    font-family: 'Roboto', sans-serif;

}

p {
    font-size: 32px;
    color: #ccc;
    text-align: center;
    margin-top: 30px;
    margin-bottom: 10px;
}

em {
    font-weight: none;
    font-size: 48px;
    color: #ccc;
    text-align: center;
    margin-top: 100px;
}

.container {
    text-align: center;
}

.pick {
    font-size: 18px;
    
    padding: 10px;    
    width: 50px;
    height: 50px;
    line-height: 50px;
    margin: 0 10px;
    
    color: #ccc;

    border: solid 2px #ccc;
}

.pick:hover, .active {
    background-color: #ccc;
    color: #777;

    cursor: pointer;
    cursor: hand;
}

#reset {
    background-color: rgb(150, 70, 60);
    color: #ccc;

    width: 92px;
    height: 48px;

    margin: 10px auto 10px auto;    

    border: none;
    border-radius: 2px;

    text-align: center;
    font-size: 24px;
}

#board {
    width: 480px;
    height: 480px;

    margin: 10px auto 0 auto;
    text-align: center;
}

.block {
    background-color: #444;
    float: left;
    
    width: 150px;
    height: 150px;

    border: solid 5px #747474;

    line-height: 150px;
    color: #ccc;
    font-size: 92px;
}

#flash_message {
    display: none;
    padding: 10px 0;
    background-color: rgba(0, 0, 0, 0.8);
    position: absolute;

    text-align: center;
    color: #eee;

    width: 480px;
    height: 460px;
    line-height: 460px;
}
            
          
!
            
              $(document).ready(function() {
    
    var board = [0, 1, 2, 3, 4, 5, 6, 7, 8];
    var human, bot;
        
    $('.pick').click(function() {
	if ($(this).text() == 'O')
	{
	    $(this).addClass('active');
	    $(this).siblings().removeClass('active');
	    human = 'O';
	    bot = 'X';
	    reset();
	}
	else
	{
	    $(this).addClass('active');
	    $(this).siblings().removeClass('active');	    
	    human = 'X';
	    bot = 'O';
	    reset();
	}
	
    });	
    var moves = 0;
    $('.block').click(function() {
	
	if ($(this).text() == '')
	{
	    $(this).html(human);
	    board[parseInt($(this).attr('id'))] = human;
	    moves++;
	    
	    var botTurn = setTimeout(function() {
		
		var move = botMove(board, bot);
		$('#' + move).html(bot);
		board[move] = bot;
		moves++;
		
		if (winning(board, bot))
		{
		    verdict('You lost :(');
		}
		
	    }, 500);
	}
	
	if (winning(board, human))
	{
	    verdict('You Won! :)');
	}
	else if (tie(board))
	{
	    verdict('It\'s a tie!');
	}
	
    });
    
    $("#reset").click(reset);

    /******************
	   HELPERS
    ******************/

    function winning(board, player) {
	if ((board[0] == player && board[1] == player && board[2] == player) ||
	    (board[3] == player && board[4] == player && board[5] == player) ||
	    (board[6] == player && board[7] == player && board[8] == player) ||
	    (board[0] == player && board[4] == player && board[8] == player) ||
	    (board[2] == player && board[4] == player && board[6] == player) ||
	    (board[0] == player && board[3] == player && board[6] == player) ||
	    (board[1] == player && board[4] == player && board[7] == player) ||
	    (board[2] == player && board[5] == player && board[8] == player))
	    return true;

	return false;
    }
    
    function emptyIndices(board) {
	return board.filter(val => val != 'O' && val != 'X');
    }

    /*function minimax(board, player) {
      
      }*/

    function tie(board) {
	var availSpots = emptyIndices(board);
	if (availSpots.length > 0)
	    return false;
	else
	    return true;
    }

    function reset() {
	board = [0, 1, 2, 3, 4, 5, 6, 7, 8];

	for (var i = 0; i < 9; i++)
	{
	    $("#" + i).html('');
	}

    }

    function botMove(board, bot) {
	var emptyBlocks = emptyIndices(board);
	var generated = Math.floor(Math.random() * (emptyBlocks[emptyBlocks.length-1] - emptyBlocks[0])) + emptyBlocks[0];

	var move;
	for (var i = emptyBlocks.length; i >= 0; i--)
	{
	    if (generated > emptyBlocks[i])
	    {
		move = emptyBlocks[i];
		break;
	    }
	    else
		move = generated;
	}

	    return move;
    }

    function verdict(message) {

	$('#flash_message').html('<em>' + message + '</em>');
	$('#flash_message').fadeIn('slow');
	var fadeOut = setTimeout(function() {
	    $('#flash_message').fadeOut('slow');
	    reset();
	}, 2000);

    }

});
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console