📎 BIENVENIDOS A MI BLOG ★ UN POCO DE TODO, DESDE: DISEÑO GRÁFICO, MÚSICA Y MÁS ⋆˚꩜。

lunes, 5 de octubre de 2026

Tutorial de ajedrez

 


Hola papus,pues como verán les traigo el tutorial de cómo poner un ajedrez totalmente funcional para su blogcito.

Pues la verdad es muy simple, copiando y pegando el código en un Gadget ya podría funcionar, pero en caso de que quieran cambiarle el color igual les estaré diciendo qué valores son los que tienen que cambiar.

Les dejo el código a continuación:

<style>
  /* --- CARCASA Y2K TRANSPARENTE (ESTILO ACRÍLICO / WINDOWS) --- */
  .y2k-chess-box, .y2k-chess-box * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    line-height: 1;
  }
  .y2k-chess-box {
    width: 220px;
    font-family: Arial, sans-serif;
    font-size: 11px;
    text-align: center;
    padding: 10px;
    border-radius: 18px;
    
    /* Efecto de cristal plástico transparente */
    background: rgba(255, 255, 255, 0.35);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.7);
    box-shadow: 
      inset 0 1px 2px rgba(255, 255, 255, 0.9),
      0 8px 20px rgba(0, 0, 0, 0.12);
      
    margin: 0 auto;
    user-select: none;
  }

  /* CONTROLES (Dificultad y Botón) */
  .y2k-controls {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    gap: 4px;
  }
  .y2k-controls select {
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 12px;
    border: 1px solid rgba(0, 0, 0, 0.2);
    background: rgba(255, 255, 255, 0.8);
    color: #333;
    height: 22px;
    outline: none;
  }
  .y2k-controls button {
    font-size: 10px;
    padding: 2px 8px;
    border-radius: 12px;
    border: 1px solid #0056b3;
    background: linear-gradient(180deg, #4dacff 0%, #007bff 50%, #0056b3 100%);
    color: #fff;
    font-weight: bold;
    cursor: pointer;
    height: 22px;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.5);
  }

  /* --- TABLERO ORIGINAL (Blanco y Azul Pastel) --- */
  #mini-board {
    width: 200px;
    height: 200px;
    display: grid;
    grid-template-columns: repeat(8, 25px);
    grid-template-rows: repeat(8, 25px);
    border: 1px solid #888;
    margin: 0 auto;
    overflow: hidden;
    border-radius: 3px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.15);
  }
  .sq {
    width: 25px;
    height: 25px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    cursor: pointer;
  }
  
  /* Tus colores originales */
  .sq.white { background-color: #PON TU COLOR; }
  .sq.blue { background-color: #PON TU COLOR; }
  .sq.selected { background-color: #PON TU COLOR !important; }

  /* ESTADO E HISTORIAL ESTILO Y2K */
  #mini-status {
    font-size: 10px;
    font-weight: bold;
    margin: 8px 0 4px;
    color: #222;
    height: 14px;
    overflow: hidden;
    white-space: nowrap;
  }
  #mini-history {
    font-size: 9px;
    height: 18px;
    line-height: 18px;
    overflow: hidden;
    background: rgba(255, 255, 255, 0.55);
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: 6px;
    text-align: left;
    padding: 0 6px;
    color: #444;
    white-space: nowrap;
  }
</style>

<div class="y2k-chess-box">
  <div class="y2k-controls">
    <select id="diff">
      <option value="1" />Fácil
      <option value="2" selected />Normal
      <option value="3" />Difícil
    </select>
    <button onclick="resetMiniGame()">Reiniciar</button>
  </div>

  <div id="mini-board"></div>

  <div id="mini-status">Tu turno (Blancas)</div>
  <div id="mini-history">Historial: </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.3/chess.min.js"></script>

<script>
(function() {
  var game = new Chess();
  var selectedSq = null;

  var piecesMap = {
    'p': '\u265F', 'r': '\u265C', 'n': '\u265E', 'b': '\u265D', 'q': '\u265B', 'k': '\u265A',
    'P': '\u2659', 'R': '\u2656', 'N': '\u2658', 'B': '\u2657', 'Q': '\u2655', 'K': '\u2654'
  };

  function renderBoard() {
    var boardEl = document.getElementById('mini-board');
    boardEl.innerHTML = '';
    var board = game.board();

    for (var r = 0; r < 8; r++) {
      for (var c = 0; c < 8; c++) {
        var sqId = String.fromCharCode(97 + c) + (8 - r);
        var sqEl = document.createElement('div');
        var isBlue = (r + c) % 2 === 1;
        
        sqEl.className = 'sq ' + (isBlue ? 'blue' : 'white');
        sqEl.dataset.sq = sqId;

        if (selectedSq === sqId) {
          sqEl.classList.add('selected');
        }

        var piece = board[r][c];
        if (piece) {
          var key = piece.color === 'w' ? piece.type.toUpperCase() : piece.type.toLowerCase();
          sqEl.textContent = piecesMap[key] || '';
          sqEl.style.color = piece.color === 'w' ? '#000000' : '#222222';
        }

        sqEl.addEventListener('click', onSquareClick);
        boardEl.appendChild(sqEl);
      }
    }
  }

  function onSquareClick(e) {
    if (game.game_over() || game.turn() !== 'w') return;

    var sq = e.currentTarget.dataset.sq;

    if (!selectedSq) {
      var piece = game.get(sq);
      if (piece && piece.color === 'w') {
        selectedSq = sq;
        renderBoard();
      }
    } else {
      var move = game.move({
        from: selectedSq,
        to: sq,
        promotion: 'q'
      });

      selectedSq = null;
      renderBoard();

      if (move) {
        updateStatus();
        if (!game.game_over()) {
          setTimeout(makeAIMove, 250);
        }
      }
    }
  }

  function makeAIMove() {
    var moves = game.moves({ verbose: true });
    if (moves.length === 0) return;

    var diff = parseInt(document.getElementById('diff').value);
    var move;

    if (diff === 1) {
      move = moves[Math.floor(Math.random() * moves.length)];
    } else if (diff === 2) {
      var caps = moves.filter(function(m) { return m.captured; });
      move = caps.length ? caps[Math.floor(Math.random() * caps.length)] : moves[Math.floor(Math.random() * moves.length)];
    } else {
      var priority = moves.filter(function(m) { return m.san.indexOf('+') !== -1 || m.captured; });
      move = priority.length ? priority[Math.floor(Math.random() * priority.length)] : moves[Math.floor(Math.random() * moves.length)];
    }

    game.move(move);
    renderBoard();
    updateStatus();
  }

  function updateStatus() {
    var statusEl = document.getElementById('mini-status');
    var historyEl = document.getElementById('mini-history');
    var status = '';

    if (game.in_checkmate()) {
      status = 'Jaque mate. ' + (game.turn() === 'w' ? 'Gana IA' : '¡Ganaste!');
    } else if (game.in_draw()) {
      status = 'Tablas';
    } else {
      status = game.turn() === 'w' ? 'Tu turno (Blancas)' : 'IA pensando...';
      if (game.in_check()) status += ' ¡Jaque!';
    }

    statusEl.textContent = status;
    historyEl.textContent = 'Historial: ' + game.history().slice(-3).join(', ');
  }

  window.resetMiniGame = function() {
    game.reset();
    selectedSq = null;
    renderBoard();
    updateStatus();
  };

  renderBoard();
  updateStatus();
})();
</script>



Y ya, eso sería todo.
Practicamente solo cambias los colores para tu tablero en esta parte:

 /* Tus colores originales */
  .sq.white { background-color: #PON TU COLOR; }
  .sq.blue { background-color: #PON TU COLOR; }
  .sq.selected { background-color: #PON TU COLOR !important; }

Hay muchas opciones para sacar el código del color que quieras pero acá les dejo un link para más práctico: CÓDIGOS DE COLOR .

IMPORTANTE:
Como ya se mencionó, el código funciona por si solo, por lo mismo NO QUITEN ESPACIOS, COMAS, LLAVES, NI COMILLAS, todo es importante, péguenlo tal cual y solo cambien los valores del color.
Por su atención, gracias.

2 comentarios:

read me

Yo y mi extraña obsesión por: Spider-Man

  No sé si les ha sucedido que siempre ha existido algo que los acompaña pero no le tomaban importancia hasta que comienza a ser importantes...

POPULARES