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 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="wrapper">
		<div class="block banner">TIC TAC TOE 
			<p class="option" id="about" onclick="aboutGame()">About</p>
		</div>
		<div class="board">
			<div class="block" id="0" onclick="boardBtn(this.id)"></div>
			<div class="block" id="1" onclick="boardBtn(this.id)"></div>
			<div class="block" id="2" onclick="boardBtn(this.id)"></div>
			<div class="block" id="3" onclick="boardBtn(this.id)"></div>
			<div class="block" id="4" onclick="boardBtn(this.id)"></div>
			<div class="block" id="5" onclick="boardBtn(this.id)"></div>
			<div class="block" id="6" onclick="boardBtn(this.id)"></div>
			<div class="block" id="7" onclick="boardBtn(this.id)"></div>
			<div class="block" id="8" onclick="boardBtn(this.id)"></div>
		</div>
		<div class="block menu">
			<p class="disabled" onclick="gameMode(1)">1P</p> |
			<p class="option" onclick="gameMode(2)">2P</p> |
			<p class="disabled" onclick="gameMode(3)">DEMO</p> |
		</div>
		
	</div>
              
            
!

CSS

              
                *  {
    margin: 0;
    padding: 0;
}

body {
	margin: 25px;
	display: flex;
	justify-content: center;
}

.wrapper {
  background-color: blue;
  border-radius: 20px;
}

.banner {
	font-size: 0.5em;
	display: flex;
	grid-column: 1 / 4;
	background-color: red;
}

.board {
	display: grid;
	/*grid-template-columns: 150px 150px 150px;*/
	grid-template-columns: repeat(3, 125px);
	/*grid-template-rows: 150px 150px 150px;*/
	grid-template-rows: repeat(3, 125px);
	background-color: blue;
}


.block {
	/* letters on blocks are bigger than font elsewhere */
	display: flex;
	cursor: pointer;
	background-color: red;
	font-size: 2em;
	border-radius: 10px;
	border: 1px solid #bbbbbb;
	align-items: center;
	justify-content: center;
	box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
	margin: 20px 20px 20px 20px;
}

.winning_block {
	/* decided not to use this in this game */
	/* letters on blocks are bigger than font elsewhere */
	display: flex;
	cursor: pointer;
	background-color: pink;
	font-size: 3em;
	border-radius: 4px;
	border: 1px solid #bbbbbb;
	align-items: center;
	justify-content: center;
	box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
	margin: 20px 20px 20px 20px;
}

.menu {
	font-size: 1em;
	display: flex;
	grid-column: 1 / 3;
	background-color: red;
}

.option {
	display: flex;
	cursor: pointer;
	background-color: blue;
	font-size: 20px;
	border-radius: 5px;
	border: 1px solid #bbbbbb;
	align-items: center;
	justify-content: center;
	box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
	margin: 10px 10px 10px 10px;
}

.disabled {
	display: flex;
	cursor: pointer;
	background-color: gray;
	font-size: 20px;
	border-radius: 5px;
	border: 1px solid #bbbbbb;
	align-items: center;
	justify-content: center;
	box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
	margin: 20px 20px 20px 20px;
}
              
            
!

JS

              
                /////////////////
		// MODEL LAYER //
		/////////////////

		var buttons = ['-','-','-','-','-','-','-','-','-'];
		var player = "X";
		var number_of_players = 0;
		var winning_buttons = 0;

		///////////////////
		// CONTROL LAYER //
		///////////////////
		function boardBtn(id) {
			// prevent player from selecting button that was already clicked
			// if( buttons[id] != undefined) { return; }
			if( buttons[id] != "-") { return; }

			// button registered with player
			buttons[id] = player;
			
			// display newly clicked button
			changeBoard("click", id);
			
			if( isWinner()) { 
				// prevent buttons from being clicked
				lockBoard();
				// change color of winning buttons
				for(var i=0; i<winning_buttons.length; i++) {
					changeBoard("win", winning_buttons[i]);
				}
			} else if( isBoardFull()) {
				lockBoard();
				for(i in buttons) {
					changeBoard("tie", i);
				}
			}

			// hand control to other player
			if (player == "X") { player = "O"; } else { player = "X"; }
		}

		function lockBoard() {
			// winner has been declared so lock buttons that were not clicked
			for(var i=0; i<9; i++) {
				if(buttons[i] == '-') {
					changeBoard("lock", i);
				}
			}
		}

		function isBoardFull() {
			for( i of buttons) {
				if( i == '-') {
					return false;
				}

			}
			return true;
		}

		function clearBoard() {
			// set recorded moves to character other than X or O
			buttons = ['-','-','-','-','-','-','-','-','-'];

			winning_buttons = 0;
			number_of_players = 0;
			for(i = 0; i < 9; i++) {
				changeBoard("clear", i);
			}
			
		}

		function gameMode(p) {
			switch(p) {
				// // case 1:
				// // alert("Player vs CPU [disabled]");
				// break;
				case 2:
				clearBoard();
				alert("Player vs Player");
				break;
				// case 3:
				// alert("Demo [disabled]");
				// break;
			}

		}

		function aboutGame() {
			alert("This game was created with HTML, CSS and Javascript\n" +
				"Contact me at kristynagel@mail.com\n" +
				String.fromCharCode(169)  + "2019");
		}

		function quitGame() {
			clearBoard();
			alert("Thank you for playing this Tic Tac Toe game.\n" +
				"I have been learning through the Zero to Mastery Web Developer course on Udemy\n" +
				String.fromCharCode(169)  + "2019");
		}

		function PvsCPU() {
			// empty
		}

		function CPUvsCPU() {
			// empty
		}

		////////////////
		// VIEW LAYER //
		////////////////

		function changeBoard(action, i) {
			var d = document.getElementById(i);
			switch(action) {
				case "click":
					document.getElementById(i).innerHTML = player;
					break;
				case "tie":
					d.outerHTML = '<div class="winning_block" id=' + i + ">-</div>";
					break;
				case "win":
					d.outerHTML = '<div class="winning_block" id=' + i + ">" + player + "</div>";
					break;
				case "lock":
					d.outerHTML = '<div class="block" id=' + i + "></div>";
					break;
				case "clear":
					d.outerHTML = '<div class="block" id=' + i + ' onclick="boardBtn(this.id)"></div>';
					break;
			}
			return;
		}
		////////////////
		// GAME LOGIC //
		////////////////
		function isWinner() {
			/*
			v1: complicated nested if/else conditions
			v2: multiple && conditions with multiple if conditions
			v3: loop through collection of winning combinations, checking for winning combination
			*/
			var winning_combinations = ["012", "345", "678", "036", "147", "258", "048", "246"];

			for( var i of winning_combinations) {
				if(buttons[i[0]] == player && buttons[i[1]] == player && buttons[i[2]] == player) {
					winning_buttons = i;
					return true;
				}
			}
			return false;
		}

		function showStats(id) {
			// empty
		}

		function simulateGame(id) {
			// empty
		}
              
            
!
999px

Console