123

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.

            
              
    <div class="modal">
        <h2></h2>
        <button id="exs"> &#88 </button>
        <button id="ohs"> &#79 </button>
       <!-- <h2>Press Start to play!</h2>
        <button>START</button> -->
    </div>
        
    <table>
        <tr>
            <td class="cells" id="0"></td>
            <td class="cells" id="1"></td>
            <td class="cells" id="2"></td>
        </tr>
        <tr>
            <td class="cells" id="3"></td>
            <td class="cells" id="4"></td>
            <td class="cells" id="5"></td>
        </tr>
        <tr>
            <td class="cells" id="6"></td>
            <td class="cells" id="7"></td>
            <td class="cells" id="8"></td>
        </tr>
    </table>

    <section>
        <h2></h2>
        <button>RESET</button>
    </section>

</body>
            
          
!
            
              /* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}

/* CSS START ////////////////////////////////////////////////  */

body{
	background-color: #A6A9ED;
}

/* .btnWrapper{
	margin: 10%;
	border: 1px solid black;
	text-align: center;
} */

/* button{
	width: 80px;
	margin: 0 5%;
	font-size: 45px;
	border: 1px solid black;
	text-decoration: none;
	background-color: transparent;
	border-radius: 5px;
	box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
} */

.modal{
	text-align: center;
	height: 305px;
	width:  81%;
	z-index: 100;
	display: block;
	border-radius: 30px;
	background: rgba(160,227,195, 1);
	position: absolute;
	transform: translateX(12%);
	box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
	margin-bottom: 0px;
}

.modal h2{
	color: black;
	margin-top: 10%;
	font-size: 20px;
	line-height: 20px;
	font-family: 'Bree Serif', serif;
	text-shadow: 2px 2px 4px #9D9D9D;
}

.modal button{
	background: #A0E3C3;
	border: 2px solid white;
	border-radius: 5px;
	height: 30px;
	width: 70px;
	margin-top: 50px;
	font-weight: bolder;
}

.modal button:hover{
	background: white;
	border: 2px solid #A0E3C3;
	transition-duration: 0.2s;
}


td{
	border: 2px solid black;
	height: 100px;
	width: 300px;
	cursor: pointer;
	font-size: 50px;
	line-height: 2;
	/* padding-top: 5px; */
}

table{
	width: 80%;
	position: relative;
	text-align: center;
	text-shadow: 2px 2px 4px #000000;
	font-family: 'Gloria Hallelujah', cursive;
	margin-bottom: 0px;
}


table tr:first-child td{
	border-top: none;
}

table tr:last-child td{
	border-bottom: none;
}

table tr td:first-child {
	border-left: none;
}

table tr td:last-child {
	border-right: none;
}


section{
	display: none;	
	text-align: center;
	width: 40%;
	transform: translateX(79%);
	margin-top: 5%;
	/* border: 2px solid black; */
	/* background: #A0E3C3; */
	/* height: 250px; */
	/* box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); */
}

section h2{
	/* border: 1px solid black; */
	font-size: 50px;
}

section button{
	border-radius: 5px;
	height: 30px;
	width: 70px;
	margin-top: 20px;
	font-weight: bolder;
	/* background: #A0E3C3; */
	/* border: 2px solid white; */
}

/* MEDIA QUERIES */
@media only screen and (max-width: 1000px) and (min-width: 300px) {

	.modal{
		text-align: center;
		height: 305px;
		width:  81%;
		z-index: 100;
		display: block;
		border-radius: 30px;
		background: rgba(160,227,195, 1);
		position: absolute;
		transform: translateX(12%);
		box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
		margin-bottom: 0px;
	}

	.modal h2{
		color: black;
		margin-top: 5%;
		font-size: 20px;
		line-height: 20px;
		font-family: 'Bree Serif', serif;
	}

	.modal button{
		background: #A0E3C3;
		border: 2px solid white;
		border-radius: 5px;
		height: 30px;
		width: 70px;
		margin-top: 50px;
		font-weight: bolder;
	}
	
	.modal button:hover{
		background: white;
		border: 2px solid #A0E3C3;
		transition-duration: 0.2s;
	}
	

	table{
		margin: 10%;
		text-align: center;
		text-shadow: 2px 2px 4px #000000;
		margin-bottom: 0px;
	}
	
}

@media only screen and (max-width: 2800px) and (min-width: 1000px) {

	.modal{
		text-align: center;
		height: 305px;
		width:  81%;
		z-index: 100;
		display: block;
		border-radius: 30px;
		background: rgba(160,227,195, 1);
		position: absolute;
		transform: translateX(12%);
		box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
		margin-bottom: 0px;
	}


	.modal h2{
		color: black;
		margin-top: 5%;
		font-size: 20px;
		line-height: 20px;
		font-family: 'Bree Serif', serif;
	}

	.modal button{
		background: #A0E3C3;
		border: 2px solid white;
		border-radius: 5px;
		height: 30px;
		width: 70px;
		margin-top: 50px;
		font-weight: bolder;
	}
	
	.modal button:hover{
		background: white;
		border: 2px solid #A0E3C3;
		transition-duration: 0.2s;
	}

	table{
		margin: 10% auto;
		text-align: center;
		text-shadow: 2px 2px 4px #000000;
		margin-bottom: 0px;
	}
}





            
          
!
            
              var huPlayer; 
var botPlayer;
var botHolderArray = []; //holds value of the cells 
var gameOver = false;
var winCombo = [ //winning combinations
     [0,1,2],
     [3,4,5],
     [6,7,8],

     [0,3,6],
     [1,4,7],
     [2,5,8],

     [0,4,8],
     [2,4,6]
];

var cells = document.getElementsByTagName('td'); //getting the the elements. return arrays

//assign the button to a new variable
var buttonStart = document.querySelector('button');
var resetBtn = document.querySelectorAll('section button')[0];


//selects all the button inside the modal
var btnSign = document.querySelectorAll('.modal button');

//adds event in all buttons
btnSign.forEach(function (){

    btnSign[0].addEventListener('click', function(){
        huPlayer = 'X';
        botPlayer = 'O';
        gameOn();
        document.getElementsByClassName('modal')[0].style.display = 'none';
    });

    btnSign[1].addEventListener('click', function(){
        huPlayer = 'O';
        botPlayer = 'X';
        gameOn();
        document.getElementsByClassName('modal')[0].style.display = 'none';
    })
})

//initialize first h2 element with some text
document.getElementsByTagName('h2')[0].innerHTML = 'HELLO! You are playing with my dumb bot! <br><br> PLEASE PICK A SIGN...';

// //added event click on start button
// buttonStart.addEventListener('click', reseter);

//btn on modal bottom
resetBtn.addEventListener('click', reseter);


//adding evenetlistener on every cells
function gameOn() {
    for(var i = 0; i<cells.length; i++){
        cells[i].addEventListener('click', turn);
        //sets the innerText to none.
        cells[i].innerText = '';
        cells[i].style.background = '#A6A9ED';
    }
}

//resets everything
function reseter(){
   
    document.getElementsByTagName('section')[0].style.display = 'none';

    //reinitialize gameOver to false so theres no winner again
    gameOver = false;

    //sets the array to empty to removes the previous pushed elements
    botHolderArray = [];

    //calls the gameOn function to start the game.
    gameOn();
}

//players initiate the game
//calls the writeCell function passes 2 arguments (id and player)
//calls another function bot turn to move
function turn() {
    writeCell(this.id, huPlayer);
    botHolderArray.push(parseInt(this.id, 10));
    botInput();  

    //removes the event click after the press of that cell.
    cells[this.id].removeEventListener('click', turn);  
}

//takes 2 parameters
//first the id of the cell (cellId)
//second is the player (player)
//writes players value X or O to a specified cell using cell id
function writeCell(cellId,player) {
    document.getElementById(cellId).innerText = player;
}

//bot turn
function botInput() {
    //takes the random number from cells length(9)
    var randCell =  Math.floor(Math.random() * cells.length); //output cell ID number 

    //    botHolderArray.indexOf(randCell) >= 0 ? botInput() : writeCell(randCell, botPlayer) && botHolderArray.push(cutIndex[0]);
   
   //checks if botHolderArray if its greater than 8, means theres no more turn for the bot 
   if(botHolderArray.length > 8) { 

        //if draw removes the event from all cells
        for(var c=0; c<cells.length; c++) 
        {  
            cells[c].removeEventListener('click', turn);
        }
        //changes h2 to DRAW if no one wins.    
        document.getElementsByTagName('h2')[0].innerText = 'DRAW!!!!';
        document.getElementsByClassName('modal')[0].style.display = 'block';
    
    } 

    else{   
        //the random value will be check if it is alredy in the botHolderArray
        //if true then call the same function
        if(botHolderArray.indexOf(randCell) >= 0){
            botInput();
        } 

        else {

            //calls writeCell fn and passes to arguments the random number and botplayer value;
            writeCell(randCell,botPlayer);
            
            //passes the random value to botHolderArray
            botHolderArray.push(randCell);

            //check game if theres a winner, flag will be true if theres a winner
            checkGame() 

            //if flag is false
            if(!gameOver){
                 //removes the event click after the bot chooses a random cell
                 cells[randCell].removeEventListener('click', turn);
            }         
        }
   }
}

//checks the game if theres a winner
function checkGame() {

    //assign the first H2 element to a new variable
    var winnerText = document.getElementsByTagName('h2')[0];

    //loops thru the winCombo array of arrays
    for (var i=0; i<winCombo.length; i++) {
        for (var j=0; j<winCombo[i].length; j++) {
            //takes the values or text of the cells 
            var uno = document.getElementById(winCombo[i][0]).textContent;
            var dos = document.getElementById(winCombo[i][1]).textContent;
            var tres = document.getElementById(winCombo[i][2]).textContent;
           
            //if theres no text on each cells
            if (uno === '' && dos === '' && tres === '') {
                break;
            } 
            //checks if the variables have the same text value
            else if (uno === dos && uno === tres) {
                
                //sets the gameOver to true and fires the event listener so the bot cant write
                gameOver = true;
                
                //Change innerText in H2 to who ever won
                // uno === huPlayer ? winnerText.innerText = 'Human YOU won' : winnerText.innerText = 'BOT WON!';
                
                //winCombo will return a cell id
                //cells[] will take a number or a cellID then give it a background color
                if(uno === huPlayer) {
                    cells[winCombo[i][0]].style.background = '#51FF45';
                    cells[winCombo[i][1]].style.background = '#51FF45';
                    cells[winCombo[i][2]].style.background = '#51FF45';
                    document.querySelectorAll('section h2')[0].innerHTML = 'HUMAN!! YOU WON!!';
                } else {
                    cells[winCombo[i][0]].style.background = '#FF0000';
                    cells[winCombo[i][1]].style.background = '#FF0000';
                    cells[winCombo[i][2]].style.background = '#FF0000';
                    document.querySelectorAll('section h2')[0].innerHTML = 'BOT WON!';
                }


                //displays the modal
                document.getElementsByTagName('section')[0].style.display = 'block';

                //if true remove the eventlistener then alart a winner
                for(var c=0; c<cells.length; c++) {  
                     cells[c].removeEventListener('click', turn);
                }

                //breaks out of the loop
                break;
            }
        }
    }
   
}

            
          
!
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.

Console