Code viewer for World: Chat with GPT model (clone...
const API_KEYS = "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";
  const submitIcon = document.querySelector("#submit-icon");
  const inputElement = document.querySelector("input");
  const imageSection = document.querySelector(".images-section");

               // rotate angle starts at 0  




 
document.write ( `


  <div class="wrap-all">
      <p class="play-time">0</p>
      <ul class="puzzle-container"></ul>
      <button class="start-button">Start</button>
      <p class="game-text">Completed!</p>
    </div>
` );
$(document).ready(function() {
 
  $('.puzzle-container').css({
  "width": "10vw",
  "margin": "20px",
  "border-radius": "15px",
  "overflow": "hidden",
  "box-shadow": "0 20px 30px -10px rgba(38, 57, 77, 0.5)"
});

// Apply CSS styles to all elements with the class "wrap-all"
$('.wrap-all').css({
  "display": "flex",
  "justify-content": "center",
  "align-items": "center",
  "flex-direction": "column"
});
// Apply CSS styles to all elements with the class "play-time"
$('.play-time').css({
  "font-size": "3rem",
  "margin-top": "2rem",
  "color": "#3b5998"
});

// Apply CSS styles to all <ul> elements
$('ul').css({
  "margin-top": "1rem",
  "list-style": "none",
  "border": "2px solid #3b5998",
  "display": "grid",
  "grid-template-columns": "repeat(4, 1fr)"
});
$('li').css({
  "width": "100px",
  "height": "100px",
  "color": "white",
        "background": "url('https://info.sonicretro.org/images/b/b3/AllStarsRacing_512x512.png')"
});
$('.start-button').css({
  "background-color": "#3b5998",
  "color": "white",
  "border": "none",
  "padding": "1rem 2rem",
  "margin-top": "1rem"
});

// Apply CSS styles to all elements with the class "game-text"
$('.game-text').css({
  "position": "absolute",
  "font-size": "45px",
  "color": "#67c23a",
  "text-shadow": "1px 1px rgba(0, 0, 0, 0.5)",
  "display": "none"
});
for (let i = 0; i <= 15; i++) {
  $(`.list${i}`).css({
    "background-position-x": `${i % 4 * -100}px`,
    "background-position-y": `${Math.floor(i / 4) * -100}px`
  });
}

  
});

const container = document.querySelector(".puzzle-container");
const startButton = document.querySelector(".start-button");
const gameText = document.querySelector(".game-text");
const playTime = document.querySelector(".play-time");

const tileCount = 16;

let tiles = [];
const dragged = {
  el: null,
  class: null,
  index: null,
};

let isPlaying = false;
let timeInterval = null;
let time = 0;

//function
function checkStatus() {
  // 상태확인
  const currentList = [...container.children];
  const unMatchedList = currentList.filter(
    (child, index) => Number(child.getAttribute("data-index")) !== index
  );
  if (unMatchedList.length === 0) {
    // 게임끝
    gameText.style.display = "block";
    isPlaying = false;
    clearInterval(timeInterval);
  }
}

function setGame() {
  // 게임시작될때
  isPlaying = true;
  time = 0;
  container.innerHTML = "";
  gameText.style.display = "none";
  clearInterval(timeInterval);

  tiles = createImageTiles();
  tiles.forEach((tile) => container.appendChild(tile));
  setTimeout(() => {
    container.innerHTML = "";
    shuffle(tiles).forEach((tile) => container.appendChild(tile));

    timeInterval = setInterval(() => {
      playTime.innerText = time;
      time++;
    }, 1000);
  }, 5000);
}

function createImageTiles() {
  const tempArray = [];
  Array(16)
    .fill()
    .forEach((_, i) => {
      const li = document.createElement("li");
      li.setAttribute("data-index", i);
      li.setAttribute("draggable", "true");
      li.classList.add(`list${i}`);
      tempArray.push(li);
    });
  return tempArray;
}

function shuffle(array) {
  let index = array.length - 1;
  while (index > 0) {
    const randomIndex = Math.floor(Math.random() * (index + 1));
    [array[index], array[randomIndex]] = [array[randomIndex], array[index]];
    index--;
  }
  return array;
}

//events
container.addEventListener("dragstart", (event) => {
  // 드래그시작
  if (!isPlaying) return;
  const obj = event.target;
  dragged.el = obj;
  dragged.class = obj.className;
  dragged.index = [...obj.parentNode.children].indexOf(obj);
});
container.addEventListener("dragover", (event) => {
  //드래그 움직
  event.preventDefault();
});
container.addEventListener("drop", (event) => {
  //그대그놨을때
  if (!isPlaying) return;
  const obj = event.target;

  if (obj.className !== dragged.class) {
    let originPlace;
    let isLast = false;

    if (dragged.el.nextSibling) {
      originPlace = dragged.el.nextSibling;
    } else {
      originPlace = dragged.el.previousSibling;
      isLast = true;
    }
    const droppedIndex = [...obj.parentNode.children].indexOf(obj);
    dragged.index > droppedIndex
      ? obj.before(dragged.el)
      : obj.after(dragged.el);
    isLast ? originPlace.after(obj) : originPlace.before(obj);
  }
  checkStatus();
});

// startButton.addEventListener("click", () => {
//   setGame();
// });