From b15002e184b7c7e51f7d57a9b76afb635af2e3c4 Mon Sep 17 00:00:00 2001 From: tiago-ferrari <69017840+tiago-ferrari@users.noreply.github.com> Date: Sun, 15 Sep 2024 01:57:06 -0300 Subject: [PATCH 1/3] Update puzzle8.html --- puzzle8.html | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-) diff --git a/puzzle8.html b/puzzle8.html index 824aff4..401ba17 100644 --- a/puzzle8.html +++ b/puzzle8.html @@ -1,5 +1,5 @@ - + @@ -76,15 +76,59 @@ transition: background-color 0.3s; } + .fifteen-puzzle-button { + position: absolute; + bottom: 10px; + right: 10px; + padding: 10px 20px; + font-size: 16px; + cursor: pointer; + background-color: #9c21f3; + color: white; + border: none; + border-radius: 5px; + box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3); + transition: background-color 0.3s; + } + + .fifteen-puzzle-button:hover{ + background-color: #9c21f3de; + } + .shuffle-button:hover { background-color: #45a049; } + .blank-space { + margin-top: 10px; + font-size: 16px; + color: #555; + } + .move-count { margin-top: 10px; font-size: 16px; color: #555; } + + .wrong-pieces-count { + margin-top: 10px; + font-size: 16px; + color: #555; + } + + .manhatan-distance { + margin-top: 10px; + font-size: 16px; + color: #555; + } + + .linha-extra { + margin-top: 10px; + font-size: 16px; + color: #555; + } + @@ -108,20 +152,46 @@
Inversões: 0 (Paridade: par)
+
+ Linha inicial do espaço vazio: 0 (De baixo para cima: ) +
Número de jogadas: 0
+
+ Peças fora do lugar: 0 +
+
+ Array da distancia manhantan de cada peça + Total +
+ +
+ Linha extra +
+ + + From 998e785019bf593d32ed1bb3d84c4fb6f5a2d508 Mon Sep 17 00:00:00 2001 From: tiago-ferrari <69017840+tiago-ferrari@users.noreply.github.com> Date: Mon, 30 Sep 2024 11:15:51 -0300 Subject: [PATCH 3/3] Update velha.html Implementado Bot para jogar contra --- velha.html | 114 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 74 insertions(+), 40 deletions(-) diff --git a/velha.html b/velha.html index a4c96b1..409cba5 100644 --- a/velha.html +++ b/velha.html @@ -158,57 +158,90 @@

Próximas Possibilidades

function handleClick(e) { const id = e.target.id; if (board[id] === "" && gameActive) { - if (currentPlayer === "X") { - // Jogada do jogador X - board[id] = currentPlayer; - e.target.textContent = currentPlayer; - e.target.classList.add(currentPlayer); + board[id] = currentPlayer; + e.target.textContent = currentPlayer; + e.target.classList.add(currentPlayer); - if (checkWinner(board, currentPlayer)) { - highlightWinningCells(board, currentPlayer); - alert(currentPlayer + ' venceu!'); - gameActive = false; - } else if (board.includes("")) { - currentPlayer = "O"; - generateNextMoves(board, currentPlayer, treeRoot); + if (checkWinner(board, currentPlayer)) { + highlightWinningCells(board, currentPlayer); + alert(currentPlayer + ' venceu!'); + gameActive = false; + } else if (board.includes("")) { + currentPlayer = "O"; + generateNextMoves(board, currentPlayer, treeRoot); + } + } + } + + function minimax(board, depth, isMaximizing) { + const scores = { X: -10, O: 10, tie: 0 }; + + const result = checkWinner(board); + if (result !== null) { + return scores[result]; + } + + if (isMaximizing) { + let bestScore = -Infinity; + for (let i = 0; i < board.length; i++) { + if (board[i] === "") { + board[i] = "O"; + let score = minimax(board, depth + 1, false); + board[i] = ""; + bestScore = Math.max(score, bestScore); + } + } + return bestScore; + } else { + let bestScore = Infinity; + for (let i = 0; i < board.length; i++) { + if (board[i] === "") { + board[i] = "X"; + let score = minimax(board, depth + 1, true); + board[i] = ""; + bestScore = Math.min(score, bestScore); } - } else if (currentPlayer === "O") { - // Jogada do jogador O via clique do mouse - board[id] = currentPlayer; - e.target.textContent = currentPlayer; - e.target.classList.add(currentPlayer); + } + return bestScore; + } + } - if (checkWinner(board, currentPlayer)) { - highlightWinningCells(board, currentPlayer); - alert(currentPlayer + ' venceu!'); - gameActive = false; - } else if (board.includes("")) { - currentPlayer = "X"; - generateNextMoves(board, currentPlayer, treeRoot); + function findBestMove(board) { + let bestScore = -Infinity; + let move; + + for (let i = 0; i < board.length; i++) { + if (board[i] === "") { + board[i] = "O"; + let score = minimax(board, 0, false); + board[i] = ""; + if (score > bestScore) { + bestScore = score; + move = i; } } } + return move; } function aiMove() { if (currentPlayer === "O" && gameActive) { - const emptyCells = board.map((val, index) => val === "" ? index : null).filter(val => val !== null); - if (emptyCells.length > 0) { - const randomIndex = emptyCells[Math.floor(Math.random() * emptyCells.length)]; - board[randomIndex] = currentPlayer; - cells[randomIndex].textContent = currentPlayer; - cells[randomIndex].classList.add(currentPlayer); + const move = findBestMove(board); + if (move !== undefined) { + board[move] = currentPlayer; + cells[move].textContent = currentPlayer; + cells[move].classList.add(currentPlayer); if (checkWinner(board, currentPlayer)) { highlightWinningCells(board, currentPlayer); alert(currentPlayer + ' venceu!'); gameActive = false; - } else if (board.includes("")) { - currentPlayer = "X"; - generateNextMoves(board, currentPlayer, treeRoot); - } else { + } else if (!board.includes("")) { alert('Empate!'); gameActive = false; + } else { + currentPlayer = "X"; + generateNextMoves(board, currentPlayer, treeRoot); } } } @@ -225,15 +258,17 @@

Próximas Possibilidades

treeRoot.innerHTML = ''; } - function checkWinner(board, player) { + function checkWinner(board) { const winPatterns = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6] ]; - return winPatterns.find(pattern => { - return pattern.every(index => board[index] === player); - }); + for (const pattern of winPatterns) { + if (pattern.every(index => board[index] === "X")) return "X"; + if (pattern.every(index => board[index] === "O")) return "O"; + } + return board.includes("") ? null : "tie"; } function highlightWinningCells(board, player) { @@ -254,7 +289,7 @@

Próximas Possibilidades

} function generateNextMoves(board, player, parent) { - parent.innerHTML = ''; // Limpar o conteúdo atual + parent.innerHTML = ''; const availableMoves = board.map((val, index) => val === "" ? index : null).filter(val => val !== null); @@ -266,7 +301,6 @@

Próximas Possibilidades

li.textContent = `Jogador ${player} -> Movimento ${move}`; parent.appendChild(li); - // Cria um mini tabuleiro para mostrar o estado do jogo após o movimento const miniBoard = document.createElement('div'); miniBoard.className = 'mini-board'; newBoard.forEach((val, index) => {