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.

            
              <body>


	<div class="container" ng-app="scoreBoard">
		
		<div ng-controller="tictacCtrl as scoreBoard">
			   <div class="row">
      <div class="col-span">
        <div class="text-center">
		<h1>tic - tac - toe</h1>
					    </div>
      </div>
    </div>

    <div class="row">
      <div class="col-span text-center text-container">
        <h3 ng-animate-swap="scoreBoard.words" class="animation absolute">{{scoreBoard.words}}</h3>
      </div>
    </div>
		 <div class="row">
      <div class="col-span">
        <div class="auto-margin">
          <table class="table tic-tac-toe-table text-center">
            <tr>
              <td>
                <button ng-click="scoreBoard.begin(0)" class="btn btn-default board-button">{{scoreBoard.display[0]}}</button>
              </td>
              <td>
                <button ng-click="scoreBoard.begin(1)" class="btn btn-default board-button">{{scoreBoard.display[1]}}</button>
              </td>
              <td>
                <button ng-click="scoreBoard.begin(2)" class="btn btn-default board-button">{{scoreBoard.display[2]}}</button>
              </td>
            </tr>
            <tr>
              <td>
                <button ng-click="scoreBoard.begin(3)" class="btn btn-default board-button">{{scoreBoard.display[3]}}</button>
              </td>
              <td>
                <button ng-click="scoreBoard.begin(4)" class="btn btn-default board-button">{{scoreBoard.display[4]}}</button>
              </td>
              <td>
                <button ng-click="scoreBoard.begin(5)" class="btn btn-default board-button">{{scoreBoard.display[5]}}</button>
              </td>
            </tr>
            <tr>
              <td>
                <button ng-click="scoreBoard.begin(6)" class="btn btn-default board-button">{{scoreBoard.display[6]}}</button>
              </td>
              <td>
                <button ng-click="scoreBoard.begin(7)" class="btn btn-default board-button">{{scoreBoard.display[7]}}</button>
              </td>
              <td>
                <button ng-click="scoreBoard.begin(8)" class="btn btn-default board-button">{{scoreBoard.display[8]}}</button>
              </td>
            </tr>
          </table>
        </div>
      </div>
    </div>
			

		  <div class="row">
      <div class="auto-margin">
        <button ng-click="scoreBoard.pcFirstPlayer()" class='btn btn-default left'>PLAY AS O</button>
        <button ng-click="scoreBoard.reset()" class='btn btn-default right'>NEW GAME</button>
				</div> 

    </div>
  </div>
</div>
</body>
            
          
!
            
              .container {
  min-width: 320px;
}

body {
	background: url('https://images.unsplash.com/photo-1467825683276-01f37b99c328?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&s=874285b396775ed12b889a66813f481b');
	  font-family:'Oswald', sans-serif;
  font-weight: 400;
}



h1 {
	font-family: 'Pacifico', cursive;
	text-align: center;
	color: white;
	font-size: 60px;
}

h3 {
	font-family: 'Pacifico', cursive;
	text-align: center;
	color: black;
	font-size: 30px;
	
}


.buttons {
	font-family: 'Oswald', sans-serif;
	font-size: 20px;
	color: red;

	
}




.tic-tac-toe-table {
  width: 300px;
}

.right {
  float: right;
  width: 42%;
	background-color: black;
	opacity: 0.6;
	color: white;
}

.left {
  width: 57%;
		background-color: black;
	opacity: 0.6;
	color: white;
}

.auto-margin {
  width: 310px;
  margin: 2px auto !important;
  float: none;
}

.board-piece {
  height: 75px;
  width: 75px;
  padding: 0px;
  background-color: rgb(20, 20, 20);
	
}

.board-button {
  height: 60px;
  width: 60px;
	color: red;
	font: 'Oswald', sans-serif;
}


.text-container {
  position: relative;
  height: 4em;
  
}
.animation.ng-enter {
  transition: 0.5s linear all;
}
.animation.ng-enter {
  opacity:0;
}
.animation.ng-enter-active {
  opacity:1;
}
.animation.ng-leave {
  opacity:1;
}
.animation.ng-leave-active {
  opacity:0;
}

@media only screen and (max-width: 570px) {
  h3 {
    font-size: 1.3em;
  }
		body {
			background: url('https://images.unsplash.com/photo-1467825683276-01f37b99c328?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&s=874285b396775ed12b889a66813f481b');
			 background-position: center;
			 background-attachment: fixed;
	}}
            
          
!
            
              angular.module('scoreBoard', ['ngAnimate']).controller('tictacCtrl', function($timeout) {
	
	var scoreBoard = this;
	
	scoreBoard.words = "Let's Play a Game!";
	
	scoreBoard.displayArr = [
		' ', ' ', ' ',
		' ', ' ', ' ',
		' ', ' ', ' '
	];
	
	scoreBoard.display = [
		' ', ' ', ' ',
		' ', ' ', ' ',
		' ', ' ', ' '
	];
	
	
scoreBoard.moves = [];
	scoreBoard.reset = reset;
	
	scoreBoard.pcFirstPlayer = pcFirstPlayer;
	scoreBoard.begin = begin;
	
	var move = true;
	var humanFirst = true;
	
	function begin(num) {
		
		if (move && moveIsTrue(scoreBoard.displayArr, num)) {
			scoreBoard.displayArr[num] = 'X';
			scoreBoard.display[num] = humanFirst? 'X':'O';
			nextMove();
		} else {
			scoreBoard.words = "No";
			return;
		}
		
		var gameOver = winner(scoreBoard.displayArr, true, 0); 
		if (gameOver === -10) {
			scoreBoard.words = humanFirst ? "X is the winner!":"O is the winner!";
			
			$timeout(reset, 2000);
			
		} else if (gameOver === 10) {
			scoreBoard.words = humanFirst ? "O is the winner!":"X is the winner!";
			
			$timeout(reset, 2000);
		} else if (gameOver === 0) {
			scoreBoard.words = "It's a tie!";
			
			$timeout(reset, 2000);
		} else {
			var d = compTurn();
			scoreBoard.displayArr[d] = 'O';
			
			scoreBoard.display[d] = humanFirst? 'O':'X';
			
			if (winner(scoreBoard.displayArr) === 10) {
				scoreBoard.words = humanFirst? 'O won':'X won';
				
				$timeout(reset, 2000);
				
			}else if (winner(scoreBoard.displayArr) === 0) {
				scoreBoard.words = "It's a tie";
				$timeout(reset, 2000);
			} else {
				nextMove();
			}
		}
		
	}
	
	function pcFirstPlayer() {
		
		if (scoreBoard.displayArr.indexOf('X') !== -1 || 
				scoreBoard.displayArr.indexOf('O') !== -1) {
			scoreBoard.words = "Game has started!";
			return ;
		}
		
		humanFirst = false;
		
		scoreBoard.words = "Begin";
		nextMove();
		
		var d = Math.floor(Math.random() * (3) + 1);
		
		if (d === 1) {
			scoreBoard.displayArr[0] = 'O';
			scoreBoard.display[0] = 'X';
			
		} else if (d === 2) {
			scoreBoard.displayArr[2] = 'O';
			scoreBoard.display[2] = 'X';
			
		} else if ( d === 3) {
			scoreBoard.displayArr[6] = 'O';
			scoreBoard.display[6] = 'X';
			
		} else if (d = 4) {
			scoreBoard.displayArr[8] = 'O';
			scoreBoard.display[8] = 'X';
		}
		nextMove();
	}
	
	
	function compTurn() {
		scoreBoard.moves= [];
		var move = undefined;
		var max = -100;
		
		for (var i = 0; i < scoreBoard.displayArr.length; i++) {
			if (moveIsTrue(scoreBoard.displayArr, i)) {
				var array = copyArray(scoreBoard.displayArr);
				array[i] = 'O';
				
				var logic = min(array, 1);
				
				if (logic > max) {
					max = logic;
					move = i;
				}
				scoreBoard.moves.push([i, max]);
			}
		}
		return move;
	}
	
	
	
	function min (arr, moves) {
		if (winner(arr, false) === 0) {
			return 0;
		} else if (winner(arr, false) === 10) {
			return 10 - moves;
		} else {
			var newTurn = 1 + moves;
			var minScore = 100;
			
			for (var i = 0; i < arr.length; i++) {
				if (moveIsTrue(arr, i)) {
					var array = copyArray(arr);
					
					array[i] = 'X';
					
					var logic = max(array, newTurn);
					
					if (logic < minScore) {
						minScore = logic;
					}
				}
			}
			return minScore;
		}
	}
	
	 
	function max(arr, moves, firstTurn) {
		if (winner(arr, true) === -10) {
			return moves - 10;
			
		} else if (winner(arr, false) === 0) {
			return 0;
		} else {
			var newTurn = 1 + moves;
			var maxScore = -100;
			
			for (var i = 0; i < arr.length; i++) {
				if (moveIsTrue(arr, i)) {
					var array = copyArray(arr);
					
					array[i] = 'O';
					
					var logic = min(array, newTurn);
					
					if (logic > maxScore) {
						maxScore = logic;
					}
					
				}
			}
			return maxScore;
		}
	}
	
	function copyArray(arr) {
		return arr.slice();
	}
	
	function noMoves(arr){
		return arr.indexOf(' ') === -1 ? true : false;
	}
	
	function moveIsTrue(arr, moves) {
	
		return arr[moves] === ' ' ? true : false;
	}
	
	function nextMove() {
		move = !move;
	}
	
	function reset() {
		move = true;
		scoreBoard.displayArr = [
			' ', ' ', ' ',
			' ', ' ', ' ',
			' ', ' ', ' '
		];
		
		scoreBoard.display = [
			' ', ' ', ' ', 
			' ', ' ', ' ',
			' ', ' ', ' '
		];
		
		scoreBoard.moves = [];
		
		humanFirst = true;
		scoreBoard.words = "Let's play again!";
	}
	
	function winner(array, x) {
		var word = undefined;
		
		if (x) {
			word = 'X';
		} else {
			word = 'O';
		}
		
		if (
		array[0] === word &&
			array[1] === word &&
			array[2] === word ||
			
			array[3] === word &&
			array[4] === word &&
			array[5] === word ||
			
			array[6] === word && 
			array[7] === word &&
			array[8] === word ||
			
			array[0] === word &&
			array[3] === word &&
			array[6] === word ||
			
			array[1] === word &&
			array[4] === word &&
			array[7] === word ||
			
			array[2] === word &&
			array[5] === word &&
			array[8] === word ||
			
			array[0] === word &&
			array[4] === word &&
			array[8] === word ||
			
			array[2] === word &&
			array[4] === word &&
			array[6] === word
		
		) {
			
			if(x) {
				return -10;
			} else {
				return 10;
			}
		} else if (array.indexOf(' ') === -1) {
			return 0;
		} else {
			return 'No';
		}
	}
	
	
});




            
          
!
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