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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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 class="body">
  <!-- Border -->
		<div class="table" data-toggle="tooltip" data-placement="left" title="Follow the prompt instructions. Click the white spaces to play against the evil Voodoo Doll AI opponent. ">
		<!-- Squares area -->
			<div class="vBar1"></div>
			<div class="vBar1"></div>
            <div class="vBar2"></div>
            <div class="hBar1"></div>   
            <div class="hBar2"></div>
             
            <div class="s1"></div>
            <div class="s2"></div>
            <div class="s3"></div>
            <div class="s4"></div>
            <div class="s5"></div>
            <div class="s6"></div>
            <div class="s7"></div>
            <div class="s8"></div>
            <div class="s9"></div>
             
            <div class="o ox1"></div>
            <div class="o ox2"></div>
            <div class="o ox3"></div>
            <div class="o ox4"></div>
            <div class="o ox5"></div>
            <div class="o ox6"></div>
            <div class="o ox7"></div>
            <div class="o ox8"></div>
            <div class="o ox9"></div>
             
            <div class="x ox1"></div>
            <div class="x ox2"></div>
            <div class="x ox3"></div>
            <div class="x ox4"></div>
            <div class="x ox5"></div>
            <div class="x ox6"></div>
            <div class="x ox7"></div>
            <div class="x ox8"></div>
            <div class="x ox9"></div>
			<!-- Score area -->
			<div class="score">
				<table>
					<tr>
						<td align="right">Total Games: </td>
						<td><div id="total"></div></td>
					</tr>
					<tr>
						<td align="right">Goofy Face wins: </td>
						<td><div id="pWinCount"></div></td>
					</tr>
					<tr>
						<td align="right">Voodoo Doll wins: </td>
						<td><div id="cpuWinCount"></div></td>
					</tr>
					<tr>
						<td align="right">Draw: </td>
						<td><div id="drawCount"></div></td>
					</tr>
				</table>						
			</div>
		</div>
		</div>

              
            
!

CSS

              
                .body {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	overflow: auto;
	background: url(http://images.alphacoders.com/207/207111.jpg) no-repeat center center;
  background-attachment: fixed;
	margin: auto;
	z-index: 30;
	}
/* Border */
.table { 
    width: 300px; 
	  height: 410px;
    background: white; 
    border: outset 19px rgba(0,0,255,0.9);
    border-radius: 5%;  
    float: right; 
    position: relative; 
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: 170px;
	  z-index: 20;
   }
/* Score Board Area */
.score {
	width: 285px; 
	height: 97px;
  background: rgba(0,0,0,0.9); 
	position: absolute;
	top: 300px;
	color: white;
  border: outset 8px rgba(0,0,0,0.9);
	}
	/* Voodoo Doll */
.x{
    height:80px;
    width:80px;
    z-index: 10;
    background: url(https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRIjMTu42tclIrP8fFTUeV8xPcEO3RzzA071Oi3pmrB9rD3-gnKzA) no-repeat center center;
    
}

/* Goofy Face */

.o{
    height:80px;
    width:80px;
    z-index: 10;
	  background: url(https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTgQgMsaPapqgHayRvZddoL-N2309CV59VKr8CYGKaq2UzeUKSbPQ) no-repeat center center;
    
}



.ox1{
    position: absolute;
    top: 15px;
    left: 15px;
}

.ox2{
    position: absolute;
    z-index: 10;
    top: 15px;
    left: 115px;
}

.ox3{
    position: absolute;
    z-index: 10;
    top: 15px;
    left: 215px;
}

.ox4{
    position: absolute;
    z-index: 10;
    top: 115px;
    left: 15px;
}

.ox5{
    position: absolute;
    z-index: 10;
    top: 115px;
    left: 115px;
}

.ox6{
    position: absolute;
    z-index: 10;
    top: 115px;
    left: 215px;
}

.ox7{
    position: absolute;
    z-index: 10;
    top: 215px;
    left: 15px;
}

.ox8{
    position: absolute;
    z-index: 10;
    top: 215px;
    left: 115px;
}

.ox9{
    position: absolute;
    z-index: 10;
    top: 215px;
    left: 215px;
}

.s1{
    position: absolute;
    height: 100px;
    width: 100px;
    background-color: white; 
}

.s2{
    position: absolute;
    height: 100px;
    width: 100px;
    background-color: white; 
    left: 100px;
}

.s3{
    position: absolute;
    height: 100px;
    width: 100px;
    background-color: white; 
    left: 200px;
}

.s4{
    position: absolute;
    height: 100px;
    width: 100px;
    background-color: white; 
    top: 100px;
}

.s5{
    position: absolute;
    height: 100px;
    width: 100px;
    background-color: white; 
    top: 100px;
    left: 100px;
}

.s6{
    position: absolute;
    height: 100px;
    width: 100px;
    background-color: white; 
    top: 100px;
    left: 200px;
}

.s7{
    position: absolute;
    height: 100px;
    width: 100px;
    background-color: white; 
    top: 200px;
}

.s8{
    position: absolute;
    height: 100px;
    width: 100px;
    background-color: white; 
    top: 200px;
    left: 100px;
}

.s9{
    position: absolute;
    height: 100px;
    width: 100px;
    background-color: white; 
    top: 200px;
    left: 200px;
}
/* Vertical Bars */
.vBar1{
    position: absolute;
    height: 300px;
    width: 10px;
    background-color: rgba(0,0,128,0.6);
	left: 100px;
    z-index: 5;
}

.vBar2{
    position: absolute;
    height: 300px;
    width: 10px;
    background-color: rgba(0,0,128,0.8);
    left: 200px;
    z-index: 5;
}
/* End of Vertical Bars */

/* Horizontal Bars */

.hBar1{
    position: absolute;
    height: 10px;
    width: 300px;
    background-color: rgba(0,0,128,0.8); 
    top: 100px;
    z-index: 5;
}

.hBar2{
    position: absolute;
    height: 10px;
    width: 300px;
    background-color: rgba(0,0,128,0.8);  
    top: 200px;
    z-index: 5;
}
/* End of Horizontal Bars */

              
            
!

JS

              
                //TODO: This AI player was design to be unbeatable(according to the tutorial). Make a "Stupid" , "Smart" and "Genious" level AI.  
$(document).ready(function() {

    var spacesTaken = [false, false, false, false, false, false, false, false, false];
    var takenBy = [];
    var bestMove;
	var difficulty = 2;
	var total = 0; document.getElementById("total").innerHTML = total;
	var pWinCount = 0; document.getElementById("pWinCount").innerHTML = pWinCount;
	var cpuWinCount = 0; document.getElementById("cpuWinCount").innerHTML = cpuWinCount;
	var drawCount = 0; document.getElementById("drawCount").innerHTML = drawCount;
	var priority = true;

    function Game(){
    }

   
    Game.prototype.toArray = function(space, user){
        spacesTaken[space] = true;
        if(user){
            takenBy[space] = 'O';
        }
        else if(!user){
            takenBy[space] = 'X';
        }
    };

    Game.prototype.draw = function(space, isUser){
        if(isUser){
            switch(space){
                case 1:
                    $('div.ox1.o').show("fast");
                    break;
                case 2:
                    $('div.ox2.o').show("fast");
                    break;
                case 3:
                    $('div.ox3.o').show("fast");
                    break;
                case 4:
                    $('div.ox4.o').show("fast");
                    break;
                case 5:
                    $('div.ox5.o').show("fast");
                    break;
                case 6:
                    $('div.ox6.o').show("fast");
                    break;
                case 7:
                    $('div.ox7.o').show("fast");
                    break;
                case 8:
                    $('div.ox8.o').show("fast");
                    break;
                case 9:
                    $('div.ox9.o').show("fast");
                    break;
            }
        } 
        
        if(!isUser){
            switch(space){
                case 1:
                    $('div.ox1.x').show("fast");
                    break;
                case 2:
                    $('div.ox2.x').show("fast");
                    break;
                case 3:
                    $('div.ox3.x').show("fast");
                    break;
                case 4:
                    $('div.ox4.x').show("fast");
                    break;
                case 5:
                    $('div.ox5.x').show("fast");
                    break;
                case 6:
                    $('div.ox6.x').show("fast");
                    break;
                case 7:
                    $('div.ox7.x').show("fast");
                    break;
                case 8:
                    $('div.ox8.x').show("fast");
                    break;
                case 9:
                    $('div.ox9.x').show("fast");
                    break;
            }
        }
    };

    Game.prototype.checkWin = function(){
		
        if(takenBy[0] == 'X' && takenBy[1] == 'X' && takenBy[2] == 'X'){
            return "X";
        }
        else if (takenBy[3] == 'X' && takenBy[4] == 'X' && takenBy[5] == 'X'){
            return "X";
        }
        else if (takenBy[6] == 'X' && takenBy[7] == 'X' && takenBy[8] == 'X'){
            return "X";
        }
    
        if(takenBy[0] == 'X' && takenBy[3] == 'X' && takenBy[6] == 'X'){
            return "X";
        }
        else if (takenBy[1] == 'X' && takenBy[4] == 'X' && takenBy[7] == 'X'){
            return "X";
        }
        else if (takenBy[2] == 'X' && takenBy[5] == 'X' && takenBy[8] == 'X'){
            return "X";
        }
    
        if(takenBy[0] == 'X' && takenBy[4] == 'X' && takenBy[8] == 'X'){
            return "X";
        }
        else if (takenBy[2] == 'X' && takenBy[4] == 'X' && takenBy[6] == 'X'){
            return "X";
        }
     
        if(takenBy[0] == 'O' && takenBy[1] == 'O' && takenBy[2] == 'O'){
            return "O";
        }
        else if (takenBy[3] == 'O' && takenBy[4] == 'O' && takenBy[5] == 'O'){
            return "O";
        }
        else if (takenBy[6] == 'O' && takenBy[7] == 'O' && takenBy[8] == 'O'){
            return "O";
        }
    
        if(takenBy[0] == 'O' && takenBy[3] == 'O' && takenBy[6] == 'O'){
            return "O";
        }
        else if (takenBy[1] == 'O' && takenBy[4] == 'O' && takenBy[7] == 'O'){
            return "O";
        }
        else if (takenBy[2] == 'O' && takenBy[5] == 'O' && takenBy[8] == 'O'){
            return "O";
        }
    
        if(takenBy[0] == 'O' && takenBy[4] == 'O' && takenBy[8] == 'O'){
            return "O";
        }
        else if (takenBy[2] == 'O' && takenBy[4] == 'O' && takenBy[6] == 'O'){
            return "O";
        }
    
        var isAdraw = true;
        for(var i = 0; i < spacesTaken.length; i++){
            if(spacesTaken[i] === false){
                isAdraw = false;
            }
        }
        if(isAdraw){
            return "draw";
        }
    
        return "neither";
    };

    Game.prototype.doMove = function(space, isUser){
	  
      if(isUser){
			match.draw(space, true);
            match.toArray(space-1, true);
			match.check();
            match.cpuMove();
        }
        else if(!isUser){
			match.draw(space, false);
            match.toArray(space-1, false);
        }
        
        match.check();
    };

    Game.prototype.exit = function(){
        spacesTaken = [false, false, false, false, false, false, false, false, false];
		takenBy = [];
		total++;
		document.getElementById('total').innerHTML = (total);
		$('div.x').hide("fast");
		$('div.o').hide("fast");
		match.goFirstOrSecond();
	    $(document).reload();
		
		window.onload = function() {
    for (var i = 1; i < 10; i++) {
        document.getElementById('txt1').innerHTML = i;
    }
}
    };
	
	Game.prototype.goFirstOrSecond = function(){
	
		if(priority == false){
			priority = true;
			match.cpuMove();
			return;
		} else {
			alert("Go first!");
			priority = false;
			return;
		}
	};

    Game.prototype.check = function(){

        var won = match.checkWin();
		
        if(won == 'X' || won == 'x'){
			cpuWinCount++;
			document.getElementById('cpuWinCount').innerHTML = (cpuWinCount);
            alert("CPU win "+(cpuWinCount)+" time(s).");
            match.exit();
            return;
        }
        else if(won == 'O' || won == 'o'){
			pWinCount++;
			document.getElementById('pWinCount').innerHTML = (pWinCount);
            alert("You win "+(pWinCount)+" time(s).");
            match.exit();
            return;
        }
        else if(won == "draw"){
			drawCount++;
			document.getElementById('drawCount').innerHTML = (drawCount);
            alert("Draw "+(drawCount)+" time(s).");
            match.exit();
            return;
        }
    };
    
    Game.prototype.cpuMove = function(){
		var tempMove = 10;
        for(var i=0; i < spacesTaken.length; i++){
            if(spacesTaken[i] === false){
                bestMove = i + 1;
            }
        }
		
		if(difficulty > 2){
			if(spacesTaken[6] == false && spacesTaken[8] == false){
				bestMove = 7;
			}
		}
		
        if(spacesTaken[4] === false){
            bestMove = 5;
        }

		if(difficulty > 1){
			tempMove = match.canWin('O', true);
			if(tempMove != 10){
				bestMove = tempMove + 1;			
			}
			
			tempMove = match.canWin('X', false);
			if(tempMove != 10){
				bestMove = tempMove + 1;			
			}
		}

		match.draw(bestMove, false);
        match.doMove(bestMove, false);
    };
	
	Game.prototype.canWin = function(winningChar, isUser){
		var winPossible;
		var winningMove = 10;
		for(var i = 0; i < 8; i++){
			if(spacesTaken[i] === false){
				var priorValue = takenBy[i];
				match.toArray(i, isUser);
				winPossible = match.checkWin();
				if(winPossible == winningChar){
					winningMove = i;
					spacesTaken[i] = false;
					takenBy[i] = priorValue;
					return i;
				}
				spacesTaken[i] = false;
				takenBy[i] = priorValue;
			}
		}
		return 10;
	};

    var match = new Game();

    $('div.x').hide("fast");
    $('div.o').hide("fast");
    
    match.goFirstOrSecond();
    
    $('div.s1').click(function(){
        match.doMove(1, true);
    });
    $('div.s2').click(function(){
        match.doMove(2, true);  
    });
    $('div.s3').click(function(){
        match.doMove(3, true);   
    });
    $('div.s4').click(function(){
        match.doMove(4, true);  
    });
    $('div.s5').click(function(){
        match.doMove(5, true);     
    });
    $('div.s6').click(function(){
        match.doMove(6, true); 
    });
    $('div.s7').click(function(){
        match.doMove(7, true);     
    });
    $('div.s8').click(function(){
        match.doMove(8, true);     
    });
    $('div.s9').click(function(){
        match.doMove(9, true);    
    });
});
              
            
!
999px

Console