css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <html lang="en">

<head>
  <meta charset="utf-8">
  <meta httbutton-equiv="X-UA-Combuttonatible" content="IE=edge">
  <meta name="viewbuttonort" content="width=device-width, initial-scale=1">
  <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
  <title>Tic Tac Toe game</title>

  <!-- Bootstrabutton -->
  <!-- Latest combuttoniled and minified CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
  <link rel="stylesheet" href="custom.css">

  <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>

  <div id="board" class="container text-center">
    <h2>Tic Tac Toe game machine</h2>

    <div id="startbutton">
      <!-- Small modal -->
      <button type="button" id="start" class="btn btn-primary" data-toggle="modal" data-target=".startgame">Start Game</button>

      <div class="modal fade startgame" tabindex="-1" role="dialog" aria-labelledby="Please choose O or X">
        <div class="modal-dialog modal-sm">
          <div class="modal-content">
            <p>"Choose your symbol"</p>
            <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="setPlayerSymbol('X')">X</button>
            <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="setPlayerSymbol('O')">0</button>
          </div>
        </div>
      </div>

    </div>
    <div id="game" class="container">
      <div class="row">

        <p id="messages"></p>
        <p id="emptyFields"></p>
        <p id="winningCombo"></p>

        <table id="board">
          <tr>
            <td id="0" class="field"></td>
            <td id="1" class="field"></td>
            <td id="2" class="field"></td>
          </tr>
          <tr>
            <td id="3" class="field"></td>
            <td id="4" class="field"></td>
            <td id="5" class="field"></td>
          </tr>
          <tr>
            <td id="6" class="field"></td>
            <td id="7" class="field"></td>
            <td id="8" class="field"></td>
          </tr>

        </table>
      </div>
    </div>
    </div>


    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
    <script src="javascript.js"></script>
</body>

</html>
            
          
!
            
              
body {
    width: 100%;
    margin: 0 auto;
}

.disabled {
    display: none;
}

#board h2 {
    margin:30px;
}

table {
    margin: 30px auto;
}

td {
    border: 1px solid grey;
    width: 100px;
    height:100px;
    text-align: center;
    font-size: 70px;   
}
            
          
!
            
              var player ="";     // player symbol
var computer = ""; // computer symbol
var turn="player"; // whos is on turn
var move = "";
var idMove = "";
var gameOver = false;


//hold on empty fields on board

var boardState = ["E", "E", "E",
                  "E", "E", "E",
                  "E", "E", "E"];
 
function setPlayerSymbol(sym) {
    //set player to use selected symbol
	player = (sym);
    // remove Start button and start the game
    $("#start").css( "display", "none" );
    $("#messages").html("Game started!");
    
    if(sym == "X") {
        computer = "O";
    	turn="player";
	} 	else { 
        	computer= "X";
			turn="computer";
    	}
	gameStart();
	// start game
}

function gameStart() {
	
	if(turn=="computer") {
		CPUTurn();
	}
	
	
	playerTurn();
	
}


function getValidMoves(){
    var valid=[];
          for (var i=0;i<boardState.length;i++){
            if(boardState[i] === 'E'){
              valid.push(i);
            }
          }
          return valid;
}

function CPUTurn() {
    var validMoves = getValidMoves();
    move = validMoves[Math.floor(Math.random() * validMoves.length)];
    //update array of field
    $("#"+move).html(computer);
    boardState[move] = computer
    // return to player
	checkWin(computer);
	if(!gameOver) {return};
}
    
function playerTurn() {

	$( ".field" ).mouseup(function() {

		$(this).html(player);
        //update array of field
        boardState[$(this).attr('id')] = player;
		// next turn
		
		checkWin(player);
		if(!gameOver) {CPUTurn();}
		
		
    });
	
}


function checkWin(symbol)  {
		
    // if statement check entire board if there is winning position 
    if (boardState[0]==symbol && boardState[1]==symbol && boardState[2]==symbol || boardState[3]==symbol && boardState[4]==symbol && boardState[5]==symbol || boardState[6]==symbol && boardState[7]==symbol && boardState[8]==symbol || boardState[0]==symbol && boardState[3]==symbol && boardState[6]==symbol || boardState[1]==symbol && boardState[4]==symbol && boardState[7]==symbol || boardState[2]==symbol && boardState[5]==symbol && boardState[8]==symbol || boardState[0]==symbol && boardState[4]==symbol && boardState[8]==symbol || boardState[2]==symbol && boardState[4]==symbol && boardState[6]==symbol) {
        
        // there is winning position
        alert ("Win by " + symbol);
		gameOver = true;
		location.reload();
       
    }
	
	// draw
	else if(getValidMoves().length === 0) {
        alert("Draw");
		gameOver = true;
        location.reload();
    }
	    
    // game still on
    return false;
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console