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

Rafael e Felipe - Jogador artificial MinMax #25

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
247 changes: 76 additions & 171 deletions velha.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,114 +5,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jogo da Velha com Minimax</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
height: 100vh;
}
#container {
display: flex;
width: 100%;
}
#game-board-container {
width: 40%;
display: flex;
flex-direction: column;
align-items: center;
background-color: #f4f4f4;
border-right: 1px solid #ccc;
padding: 10px;
}
#game-board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
}
.cell {
width: 100px;
height: 100px;
background-color: #cbc8c8;
display: flex;
justify-content: center;
align-items: center;
font-size: 2em;
cursor: pointer;
}
.cell.X {
color: blue;
}
.cell.O {
color: red;
}
.winning-cell {
background-color: #d4edda;
border: 2px solid #c3e6cb;
}
#tree {
width: 60%;
padding: 20px;
overflow-y: auto;
}
#tree ul {
list-style-type: none;
}
.mini-board {
display: grid;
grid-template-columns: repeat(3, 30px);
grid-template-rows: repeat(3, 30px);
gap: 2px;
margin-bottom: 10px;
}
.mini-cell {
width: 30px;
height: 30px;
background-color: #fff;
border: 1px solid #ccc;
display: flex;
justify-content: center;
align-items: center;
font-size: 1em;
}
.mini-cell.X {
color: blue;
}
.mini-cell.O {
color: red;
}
#button-container {
display: flex;
justify-content: space-between;
width: 100%;
margin-top: 10px;
}
button {
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
margin: 0 5px;
}
#reset-button {
background-color: #007bff;
color: white;
}
#play-o-button {
background-color: #17a2b8;
color: white;
}
#reset-button:hover {
background-color: #0056b3;
}
#play-o-button:hover {
background-color: #138496;
}
/* Estilos... (sem alterações) */
</style>
</head>
<body>

<div id="container">
<div id="game-board-container">
<div id="game-board">
Expand All @@ -131,7 +27,6 @@
<button id="play-o-button">Jogar O</button>
</div>
</div>

<div id="tree">
<h3>Próximas Possibilidades</h3>
<ul id="tree-root"></ul>
Expand All @@ -148,70 +43,93 @@ <h3>Próximas Possibilidades</h3>
const resetButton = document.getElementById('reset-button');
const playOButton = document.getElementById('play-o-button');

cells.forEach(cell => {
cell.addEventListener('click', handleClick);
});

cells.forEach(cell => cell.addEventListener('click', handleClick));
resetButton.addEventListener('click', resetGame);
playOButton.addEventListener('click', aiMove);

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);
if (checkWinner(board, currentPlayer)) {
highlightWinningCells(board, currentPlayer);
alert(currentPlayer + ' venceu!');
gameActive = false;
} else if (board.includes("")) {
currentPlayer = currentPlayer === "X" ? "O" : "X";
if (currentPlayer === "O") aiMove();
generateNextMoves(board, currentPlayer, treeRoot);
}
}
}

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

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, "O").index;
makeMove(bestMove, currentPlayer);

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

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;
}
function minimax(newBoard, player) {
const availableSpots = newBoard.map((val, index) => val === "" ? index : null).filter(val => val !== null);

if (checkWinner(newBoard, "O")) return { score: 10 };
if (checkWinner(newBoard, "X")) return { score: -10 };
if (availableSpots.length === 0) return { score: 0 };

const moves = [];
availableSpots.forEach(spot => {
const move = {};
move.index = spot;
newBoard[spot] = player;

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

newBoard[spot] = ""; // Reverter o movimento
moves.push(move);
});

let bestMove;
if (player === "O") {
let bestScore = -Infinity;
moves.forEach(move => {
if (move.score > bestScore) {
bestScore = move.score;
bestMove = move;
}
});
} else {
let bestScore = Infinity;
moves.forEach(move => {
if (move.score < bestScore) {
bestScore = move.score;
bestMove = move;
}
});
}

return bestMove;
}

function resetGame() {
Expand All @@ -231,9 +149,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 pattern.every(index => board[index] === player);
});
return winPatterns.some(pattern => pattern.every(index => board[index] === player));
}

function highlightWinningCells(board, player) {
Expand All @@ -242,31 +158,21 @@ <h3>Próximas Possibilidades</h3>
[0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]
];
const winningPattern = winPatterns.find(pattern => {
return pattern.every(index => board[index] === player);
});

const winningPattern = winPatterns.find(pattern => pattern.every(index => board[index] === player));
if (winningPattern) {
winningPattern.forEach(index => {
cells[index].classList.add("winning-cell");
});
winningPattern.forEach(index => cells[index].classList.add("winning-cell"));
}
}

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) => {
Expand All @@ -279,6 +185,5 @@ <h3>Próximas Possibilidades</h3>
});
}
</script>

</body>
</html>