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

Atv2 Matheus Bueno #17

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
133 changes: 65 additions & 68 deletions velha.html
Original file line number Diff line number Diff line change
Expand Up @@ -158,60 +158,83 @@ <h3>Próximas Possibilidades</h3>
function handleClick(e) {
const id = e.target.id;
if (board[id] === "" && gameActive) {
makeMove(id, currentPlayer);
if (currentPlayer === "X") {
// Jogada do jogador X
board[id] = currentPlayer;
e.target.textContent = currentPlayer;
e.target.classList.add(currentPlayer);
currentPlayer = "O";
aiMove();
}
}
}

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);
function makeMove(index, player) {
board[index] = player;
cells[index].textContent = player;
cells[index].classList.add(player);

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

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 bestMove = minimax(board, currentPlayer).index;
makeMove(bestMove, currentPlayer);
currentPlayer = "X";
}
}

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 };
} else if (availableSpots.length === 0) {
return { score: 0 };
}

const moves = [];
for (let i = 0; i < availableSpots.length; i++) {
const move = {};
move.index = availableSpots[i];
newBoard[availableSpots[i]] = player;

if (player === "O") {
const result = minimax(newBoard, "X");
move.score = result.score;
} else {
const result = minimax(newBoard, "O");
move.score = result.score;
}

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;
newBoard[availableSpots[i]] = ""; // desfaz o movimento
moves.push(move);
}

let bestMove;
if (player === "O") {
let bestScore = -Infinity;
for (let i = 0; i < moves.length; i++) {
if (moves[i].score > bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
} else {
let bestScore = Infinity;
for (let i = 0; i < moves.length; i++) {
if (moves[i].score < bestScore) {
bestScore = moves[i].score;
bestMove = i;
}
}
}

return moves[bestMove];
}

function resetGame() {
Expand Down Expand Up @@ -252,32 +275,6 @@ <h3>Próximas Possibilidades</h3>
});
}
}

function generateNextMoves(board, player, parent) {
parent.innerHTML = ''; // Limpar o conteúdo atual

const availableMoves = board.map((val, index) => val === "" ? index : null).filter(val => val !== null);

availableMoves.forEach(move => {
const newBoard = [...board];
newBoard[move] = player;

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) => {
const miniCell = document.createElement('div');
miniCell.className = 'mini-cell ' + val;
miniCell.textContent = val;
miniBoard.appendChild(miniCell);
});
li.appendChild(miniBoard);
});
}
</script>

</body>
Expand Down