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

              
                <center><div id="dialog">
  <p>X xor O </p>
  </div>
<table>
  <tr>
    <td id="0"></td>
    <td id="1"></td>
    <td id="2"></td>
  </tr>
  <tr>
    <td id="3"></td>
    <td id="4"></td>
    <td id="5"></td>
  </tr>
  <tr>
    <td id="6"></td>
    <td id="7"></td>
    <td id="8"></td>
  </tr>
  </table></center>
              
            
!

CSS

              
                body {
  background-color:black;
}

p {
  color:red;
}
table {
  width:300px;
  height:300px;  
}
#dialog {
  background-color:blue;
}

td{
  border-style:solid;
  border-color:white;
  background-color:cyan;
  text-align:center;
  width:100px;
  height:100px;
}

.no-close, .ui-dialog-titlebar-close {
  display: none;
}
              
            
!

JS

              
                $(document).ready(function(){
  var arr1=[0,0,0,0,0,0,0,0,0]; //mang quan ly o dc danh
  var arr2=[];   // mang quan ly ng choi
  var arr3=[];  // mang quan ly computer
  var flag="O"; // may danh
  var round=0;
  var winObj=[[0,1,2],
              [3,4,5],
              [6,7,8],
              [0,3,6],
              [1,4,7],
              [2,5,8],
              [0,4,8],
              [2,4,6]];  
  
  
  $( "#dialog" ).dialog({    
    dialogClass:"noClose",
  buttons: [
    {
      text: "X",
      icons: {
        primary: "ui-icon-heart"
      },
      click: function() {
        flag="O"; 
        $( this ).dialog( "close" );
        computer();
      }      
    },
    {
      text: "O",
      icons: {
        primary: "ui-icon-heart"
      },
      click: function() {
        flag="X"; 
        $( this ).dialog( "close" )
        computer();
      }      
    }
  ]
});
  
  
  function checkArr2() {
    
    for (var m=0;m<8;m++)
      { 
        let k=0;        
        for (var n=0;n<3;n++) 
            if (arr2.indexOf(winObj[m][n])!=-1) k++;         
            //else alert(arr2+ "   "+winObj[m][n]  + "     "+arr2.indexOf(winObj[m][n]));} //loi cu chuoi
                   
        
        if (k===3) return true;        
      }
    return false;
  }
  
  function checkArr3() {        
    for (var j=0;j<winObj.length;j++)
      { 
        let t=0;        
        for (var i=0;i<winObj[j].length;i++) if (arr3.indexOf(winObj[j][i])!==-1)
        {
          t++;  
          //alert(arr3+ "   "+winObj[j][i]  + "     "+arr3.indexOf(winObj[j][i]));
                   
        }
        if (t===3) return true;        
      }
    return false;
  }
  
  function computer() {
    round=2;
    let randN=11;
    while (arr1[randN]!==0)
      {
        randN=Math.floor(Math.random()*9);
      }
    arr1[randN]=1;
    arr3.push(randN);
    if (flag==="X") $("td[id="+randN+"]").text("X");
    else $("td[id="+randN+"]").text("O");
    if (checkArr3()) restartG(2);
  }
  
  function restartG(a) {
    if (a===1) alert("ban thang");
    else alert("may thang");
    arr1=[0,0,0,0,0,0,0,0,0];
    $("td").text("");
    arr2=[];
    arr3=[];
    computer();
  }
  
  
  
  
  $("td").on("click",function(){
    round=1;
    let yourVar=parseInt($(this).prop("id"),10);
    if (arr1[yourVar]!==0) return false;
    if (flag==="X") $(this).text("O");
    else $(this).text("X");
    arr1[yourVar]=1;
    arr2.push(yourVar);
    var checkVar=checkArr2();
    if (checkVar) restartG(1); //nhay tu day
    else computer();
  })
})
              
            
!
999px

Console