Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <html>
	<head>
		<link rel="stylesheet"
		      href="https://unpkg.com/@chrisoakman/chessboardjs@1.0.0/dist/chessboard-1.0.0.min.css"
		      integrity="sha384-q94+BZtLrkL1/ohfjR8c6L+A6qzNH9R2hBLwyoAfu3i/WCvQjzL2RQJ3uNHDISdU"
		      crossorigin="anonymous">
	</head>
	<body>
		<h1>32 Queens solution:</h1>
		<div id="solutions" style="width: 900px">
			<div style="float:left; width: 200px;">
				z: 0 <br />
				<div id="board0" style="width: 200px"></div>
			</div>
			<div style="float:left; width: 200px;">
				z: 1 <br />
				<div id="board1" style="width: 200px"></div>
			</div>
			<div style="float:left; width: 200px;">
				z: 2 <br />
				<div id="board2" style="width: 200px"></div>
			</div>
			<div style="float:left; width: 200px;">
				z: 3 <br />
				<div id="board3" style="width: 200px"></div>
			</div>
			<div style="float:left; width: 200px;">
				z: 4 <br />
				<div id="board4" style="width: 200px"></div>
			</div>
			<div style="float:left; width: 200px;">
				z: 5 <br />
				<div id="board5" style="width: 200px"></div>
			</div>
			<div style="float:left; width: 200px;">
				z: 6 <br />
				<div id="board6" style="width: 200px"></div>
			</div>
			<div style="float:left; width: 200px;">
				z: 7 <br />
				<div id="board7" style="width: 200px"></div>
			</div>
		</div>

		<script
		  src="https://code.jquery.com/jquery-3.5.1.min.js"
		  integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
		  crossorigin="anonymous"></script>

		<script src="https://unpkg.com/@chrisoakman/chessboardjs@1.0.0/dist/chessboard-1.0.0.min.js"
		        integrity="sha384-8Vi8VHwn3vjQ9eUHUxex3JSN/NFqUg3QbPyX8kWyb93+8AC/pPWTzj+nHtbC5bxD"
		        crossorigin="anonymous"></script>
	</body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                $( document ).ready(function() {			
      let board = Array(8).fill().map(() => Array(8).fill().map(() => Array(8).fill(false)));
			let boards = Array;
			let max_placed_queens = 0;
      let placed_queens = 0;

      function pieceTheme (piece) {
        return 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Chess_qdt45.svg/26px-Chess_qdt45.svg.png';
      }

      var config = {
        showErrors: true,
        pieceTheme: pieceTheme
      };

			for (let i = 0; i < 8; i++) {
				boards[i] = Chessboard('board' + i, config);
        update_board(boards[i], i);
			}

			setTimeout(function() { place_queen(0,0) }, 500);

			function place_queen(h, col) {

				// Found a solution
				if ((h * 8 + col) > 63)
					return true;

				// Next level
				if (col > 7) {
					h++;
					col -= 8;
				};

				// Place a queen
				for (let i = 0; i <= 8; i++) {
          if (i == 8 && (h * 8 + col + 1) < 64) {
            if ( place_queen(h,col+1) )
              return true;
          }
          
					if (i != 8 && valid_placement(h, i, col)) {
						board[h][i][col] = true;
            placed_queens++;
            
            console.log('Trying:', h, i, col, placed_queens);

						// Update the board with the best solution
						if ( max_placed_queens < placed_queens ) {
              console.log(placed_queens + ' Placed');

              for (let j = 0; j < 8; j++) {
                update_board(boards[j], j);
              }
              
              if (placed_queens > 33) {
                return true;
              }
              
							max_placed_queens = placed_queens;
						}

						// Place another queen
						if (place_queen(h,col+1))
							return true;
            
            placed_queens--;

						board[h][i][col] = false;
					}
				}

				// This branch has failed
				return false;
			}

			function valid_placement(height, row, col) {
				let i,j,k,o,a,b;

				for (i = 0; i < 8; i++) {
					if (board[height][row][i])
						return false;
 					if (board[height][i][col])
						return false;         
				}

        //o = (row > col) ? row : col;
        o = 8 - col - 1;
				for (i = row + o, j = col + o; i >=0 && j >= 0; i--, j--) {
          if (i >= 0 && j >= 0 && i < 8 && j < 8 && board[height][i][j]) 
						return false;
				}

				for (i = row - o,  j = col + o; i < 8 && j >= 0; i++, j--) {
          if (i >= 0 && j >= 0 && i < 8 && j < 8 && board[height][i][j])
					  return false;
				}

				for (k =  7; k >= 0; k--) {
					let range = height - k;
          if (range < 0)
            range = -range;
					let ranges = [-range, 0, range];

					for ( i = 0; i < 3; i++ ) {
						for ( j = 0; j < 3; j++ ) {
							a = row + ranges[i];
							b = col + ranges[j];

							// Out of bound;
							if (a >= 0 && b >= 0 && a < 8 && b < 8) {
								if (board[k][a][b])
									return false;
							}
						}
					}
				}

				return true;
			}

			function update_board(display_board, z) {
				let positions = {};

				for (let i = 0; i < 8; i++) {
					for (let j = 0; j < 8; j++) {
						if (board[z][i][j] == true) {
							let letter = (j + 10).toString(36);
							let number = i + 1;
							positions[letter + number] = 'bQ';
						}
					}
				}

        display_board.position(positions, false);
			}
});
              
            
!
999px

Console