Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update velha.html #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 38 additions & 53 deletions velha.html
Original file line number Diff line number Diff line change
Expand Up @@ -158,62 +158,43 @@ <h3>Próximas Possibilidades</h3>
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);

if (checkWinner(board, currentPlayer)) {
highlightWinningCells(board, currentPlayer);
alert(currentPlayer + ' venceu!');
gameActive = false;
} else if (board.includes("")) {
currentPlayer = "O";
generateNextMoves(board, currentPlayer, treeRoot);
}
} else if (currentPlayer === "O") {
// Jogada do jogador O via clique do mouse
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 = "X";
generateNextMoves(board, currentPlayer, treeRoot);
}
makeMove(id, currentPlayer, e.target);
if (checkWinner(board, currentPlayer)) {
highlightWinningCells(board, currentPlayer);
alert(currentPlayer + ' venceu!');
gameActive = false;
} else if (board.includes("")) {
currentPlayer = "O";
generateNextMoves(board, currentPlayer, treeRoot);
}
}
}

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);

if (checkWinner(board, currentPlayer)) {
highlightWinningCells(board, currentPlayer);
alert(currentPlayer + ' venceu!');
gameActive = false;
} else if (board.includes("")) {
currentPlayer = "X";
generateNextMoves(board, currentPlayer, treeRoot);
} else {
alert('Empate!');
gameActive = false;
}
const bestMove = minimax(board, currentPlayer).index;
makeMove(bestMove, currentPlayer, cells[bestMove]);

if (checkWinner(board, currentPlayer)) {
highlightWinningCells(board, currentPlayer);
alert(currentPlayer + ' venceu!');
gameActive = false;
} else if (!board.includes("")) {
alert('Empate!');
gameActive = false;
} else {
currentPlayer = "X";
generateNextMoves(board, currentPlayer, treeRoot);
}
}
}

function makeMove(index, player, cell) {
board[index] = player;
cell.textContent = player;
cell.classList.add(player);
}

function resetGame() {
board.fill("");
cells.forEach(cell => {
Expand All @@ -231,7 +212,7 @@ <h3>Próximas Possibilidades</h3>
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
];
return winPatterns.find(pattern => {
return winPatterns.some(pattern => {
return pattern.every(index => board[index] === player);
});
}
Expand All @@ -254,7 +235,7 @@ <h3>Próximas Possibilidades</h3>
}

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);

Expand All @@ -265,8 +246,7 @@ <h3>Próximas Possibilidades</h3>
const li = document.createElement('li');
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) => {
Expand All @@ -278,7 +258,12 @@ <h3>Próximas Possibilidades</h3>
li.appendChild(miniBoard);
});
}
</script>

</body>
</html>
function minimax(newBoard, player) {
const availableSpots = newBoard.map((val, index) => val === "" ? index : null).filter(val => val !== null);

if (checkWinner(newBoard, "X")) {
return { score: -10 };
} else if (checkWinner(newBoard, "O")) {
return { score: 10 };
}