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

            
              <div id="container">
		<div id="container2">
		<div id="game-box">
			<div class="fields first-row first-column" id="a1"></div>
			<div class="fields first-row" id="a2"></></div>
			<div class="fields first-row" id="a3"></></div>
			<div class="fields first-column" id="b1"></></div>
			<div class="fields" id="b2"></></div>
			<div class="fields" id="b3"></></div>
			<div class="fields first-column" id="c1"></></div>
			<div class="fields" id="c2"></></div>
			<div class="fields" id="c3"></></div>
			<div id="choose-symbol-box">
				<div id="choose-symbol-title">Please choose your symbol:
				</div><!--choose-symbol-title-->
				<div id="choose-symbol-choice">
					<div class="symbols" id="X">X</div>
					<div class="symbols" id="O">O</div>
				</div><!--number-of-players-choice-->
			</div><!--choose-symbol-box-->
			<div id="you-win">You Won</div>
			<div id="cpu-win">CPU Won</div>
			<div id="player-one-win">Player One Won</div>
			<div id="player-two-win">Player Two Won</div>
			<div id="draw">Draw</div>
			
		</div><!--game-box-->
		<div id="scoreboard">
		<div id="score-title">Score</div>
		<div class="player-score">Player One:</div>
		<div class="player-score">Player Two:</div>
		<div class="score" id="score-player-one">0</div>
		<div class="score" id="score-player-two">0</div>
		
		</div>
		<div id="restart-frame">
			
				<button type="button" id="restart">Restart</button>
		
		</div><!--restart-frame-->
		</div><!--container2-->
	</div><!--container-->
            
          
!
            
              

html, body {
	position:fixed;
    top:0;
    bottom:0;
    left:0;
    right:0;
	background: url(http://i.imgur.com/NquyExr.jpg);

	
}
#container1 {

	
    height: 700px;
	background: url(http://i.imgur.com/NquyExr.jpg);
	width: 100%;
	margin-left: auto;
	margin-right: auto;
	
}

#container2 {
	
	margin-left: auto;
	margin-right: auto;
	
    height: 700px;
	background: url(http://i.imgur.com/NquyExr.jpg);
	width: 900px;
	
}

#game-box {
	height: 600px;
	width: 548px;
	background-color: white;
	margin-top: 50px;
	margin-left: auto;
	margin-right: auto;
	-webkit-box-shadow: 7px 10px 5px -2px rgba(0,0,0,0.47);
	-moz-box-shadow: 7px 10px 5px -2px rgba(0,0,0,0.47);
	box-shadow: 7px 10px 5px -2px rgba(0,0,0,0.47);
	background: url(http://i.imgur.com/yFjuY9K.png);
	
	

}

.fields {
	width: 170px;
	height: 170px;
	color: white;
	float: left;
	text-align: center;
	font-size: 194px;
	line-height: 180px;
	font-family: 'Indie Flower', cursive;
	cursor:pointer;
	
	
	
}

.first-row {
		margin-top:30px;
}

.first-column {
	margin-left:13px;
}


#choose-symbol-box {
	background-color: black;
	height: 600px;
	width: 548px;
	margin:auto;
	position: relative;
	display:block;
	background: url(http://i.imgur.com/yFjuY9K.png);
	-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
	
	
}

#choose-symbol-title {
	position: absolute;
	color:white;
	text-align: center;
	margin-left: 15px;
	padding-top:60px;
	font-size: 34px;
	font-family: 'Rock Salt', cursive;
}

.symbols {
	position: relative;
	color: white;
	font-family: 'Indie Flower', cursive;
	float: left;
	font-size: 74px;
	margin-left: 150px;
	top: -310px;
}

.symbols:hover {
	cursor:pointer;	
	color:gray;
}

#you-win {
font-family: 'Anton', sans-serif;
	color: gold;
	font-size: 80px;
	text-align:center;
	margin-left:auto;
	margin-right:auto;
	position:relative;
	top:-320px;
	width:300px;
	z-index:5;
	display:none;
}

#cpu-win {
font-family: 'Anton', sans-serif;
	color: gold;
	font-size: 80px;
	text-align:center;
	margin-left:auto;
	margin-right:auto;
	position:relative;
	top:-320px;
	width:300px;
	z-index:5;
	display:none;
}

#player-one-win {
font-family: 'Anton', sans-serif;
	color: gold;
	font-size: 80px;
	text-align:center;
	margin-left:auto;
	margin-right:auto;
	position:relative;
	top:-320px;
	width:500px;
	z-index:5;
	display:none;
}

#player-two-win {
font-family: 'Anton', sans-serif;
	color: gold;
	font-size: 80px;
	text-align:center;
	margin-left:auto;
	margin-right:auto;
	position:relative;
	top:-320px;
	width:500px;
	z-index:5;
	display:none;
}


#draw {
font-family: 'Anton', sans-serif;
	color: gold;
	font-size: 80px;
	text-align:center;
	margin-left:auto;
	margin-right:auto;
	position:relative;
	top:-320px;
	width:300px;
	z-index:5;
	display:none;
}

#scoreboard {
	height: 400px;
	width: 250px;
	position: relative;
	top: -550px;
	left: -200px;
	background: url(http://i.imgur.com/ZVlVzG2.png);
	-ms-transform: rotate(-2deg); /* IE 9 */
    -webkit-transform: rotate(-2deg); /* Chrome, Safari, Opera */
    transform: rotate(-2deg);
}

#score-title {
	text-align: center;
	margin-top: 30px;
	font-family: 'Indie Flower', cursive;
	font-size: 44px;
	color: blue;
	text-decoration: underline;
}

.player-score{
	font-family: 'Indie Flower', cursive;
	float: left;
	margin-left:26px;
	font-size: 18px;
	color: blue;
}

.score {
	font-family: 'Indie Flower', cursive;
	float: left;
	margin-left:65px;
	font-size: 58px;
	color: blue;
}

#restart-frame {
	height: 70px;
	width: 130px;
	top: -800px;
	position:relative;
	background-color: #F0FFFF;
	left: 900px;
	border-radius: 5px;
	-webkit-box-shadow: 2px 2px 5px 0px rgba(0,0,0,0.75);
-moz-box-shadow: 2px 2px 5px 0px rgba(0,0,0,0.75);
box-shadow: 2px 2px 5px 0px rgba(0,0,0,0.75);
}



#restart {
	background-color: rgb(244,67,54);
	position: relative;
	border-radius: 5px;
	width: 100px;
	height: 50px;
	margin-left: 15px;
	margin-right: 15px;
	margin-top:10px;
	font-family: 'Anton', sans-serif;
	font-size: 20px;
	-webkit-box-shadow: 2px 2px 5px 0px rgba(0,0,0,0.75);
-moz-box-shadow: 2px 2px 5px 0px rgba(0,0,0,0.75);
box-shadow: 2px 2px 5px 0px rgba(0,0,0,0.75);

}

#restart:hover {
	background-color: rgb(255,0,0);
	cursor: pointer;
	
}
            
          
!
            
              $(document).ready(function (){
	'use strict';
	var	playerOneSymbol = ""; //symbol used by player one 'X' or 'O'
	var playerTwoSymbol = ""; //symbol used by player two 'X' or 'O'
	var	controlNumber = 0;	//holding information about whose turn is it, 0 for player one and 1 for player two
	var	clickedField = "";	//holding information about field name(number) clicked by player
	var playerOneFields = []; //fields selected by player one
	var playerTwoFields = []; //fields selected by player two
	var playerOneScore = 0;
	var playerTwoScore = 0;
	var fieldsClicked = 0; // number of fields clicked
	var result = "";
	var	emptyFields = ["a1","a2","a3","b1","b2","b3","c1","c2","c3"];
	
	var winConditions = [	//cominations of wining fields
		["a1","a2","a3"],
		["a1","b1","c1"],
		["a1","b2","c3"],
		["b1","b2","b3"],
		["c1","b2","a3"],
		["a2","b2","c2"],
		["a3","b3","c3"],
		["c1","c2","c3"]
	];
	
	

	

	choseSymbol();
	
	function choseSymbol(){ //chiosing player symbol 'X' or 'O'
		$('.symbols').click(function(event){	//
			playerOneSymbol = event.target.id;
			if(playerOneSymbol === 'X'){
				playerTwoSymbol ='O';
			} else if( playerOneSymbol === 'O'){
				playerTwoSymbol = 'X';
			}
			$("#choose-symbol-box").css('display','none');
			$(".fields").css('border','1px solid white');
			game();	
			
			
		});
	}
	
	
	function game(){ //easy difficulty single player game
			
				$('.fields').click(function(event){
			
					clickedField = event.target.id;
					playerOneFields.push(clickedField);
					emptyFields.splice(emptyFields.indexOf(clickedField), 1);
					$('#'+clickedField).html(playerOneSymbol).css('pointer-events', 'none');
					checkIfWin(playerOneFields);
					fieldsClicked++;
					console.log("fieldsClicked: "+fieldsClicked);
					checkIfDraw();
					controlNumber = 1; //player one turn ends, changing controlNumber to one to starts CPU turn
					
				let randomField = emptyFields[Math.floor((Math.random() * (emptyFields.length-1)) + 0)];
				setTimeout(function() {
					$('#'+randomField).html(playerTwoSymbol).css('pointer-events', 'none');
					playerTwoFields.push(randomField);
					emptyFields.splice(emptyFields.indexOf(randomField), 1);
					checkIfWin(playerTwoFields);
					fieldsClicked++;
					console.log("fieldsClicked: "+fieldsClicked);
					checkIfDraw();
					controlNumber = 0;
					}, 500);
				});
				
				
					
	}
	

	
	function checkIfWin(player){
		console.log("checkifwin: "+player);
		for(let i = 0; i < 8;i++){ //checking if winConditions and player(One/Two)Field have common elements
			var res = winConditions[i].filter(function(v){
				return player.indexOf(v) > -1;
			});
				
			if(res.length === 3){ //if all 3 elements from winConditions array are in player(One/Two)Field player wins
				
				for(let i = 0; i<=emptyFields.length; i++){
					$('#'+emptyFields[i]).css('pointer-events', 'none');
				}
					
				if(player === playerOneFields){
					$("#player-one-win").css('display','block');
					setTimeout(function() {
						$("#player-one-win").css('display','none');
						playerOneScore++;
						$('#score-player-one').html(playerOneScore);
						for(let j = 0;j<9;j++){
							$('#'+emptyFields[j]).css('pointer-events', 'none');
						}
						result = "win";
						restart();
						return;
					}, 2000);
					
				} else if(player === playerTwoFields) {
					$("#player-two-win").css('display','block');
					setTimeout(function() {
						$("#player-two-win").css('display','none');
						playerTwoScore++;
						$('#score-player-two').html(playerTwoScore);
						for(let j = 0;j<9;j++){
							$('#'+emptyFields[j]).css('pointer-events', 'none');
						}
						result = "win";
						restart();
						return;
						}, 2000);
					}
					
				result = "win";
				res = "";
					
			} else {
					
			}
		}	
	}
	
	function checkIfDraw(){
		if(fieldsClicked === 9 && result !="win"){	//if nobody wins and all field (9) has been clicked it is draw
			
			$("#draw").css('display','block');
						setTimeout(function() {
							$("#draw").css('display','none');
							restart();
						}, 2000);
						
						restart();
					
					var res = "";
		}
	}
	
	
	
	function restart(){
			
			result = "";
			playerOneFields = [];	
			playerTwoFields = [];
			controlNumber = 0;
			fieldsClicked = 0;
			emptyFields = ["a1","a2","a3","b1","b2","b3","c1","c2","c3"];
			
			console.log("restart");
			for(var i = 0; i<=emptyFields.length; i++){
				$("#"+emptyFields[i]).html("");
				$('#'+emptyFields[i]).css('pointer-events', 'auto');
			}
			
			
			
		}
		
	$('#restart').click(function(){
				window.location.href = window.location.href;
	});
	
	
		
	
});
            
          
!
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