Añadidos juegos
This commit is contained in:
15
3-en-raya-computer/index.html
Normal file
15
3-en-raya-computer/index.html
Normal file
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Tres en Raya vs Máquina</title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Tres en Raya vs Máquina</h1>
|
||||
<div id="board"></div>
|
||||
<div id="status"></div>
|
||||
<button id="restart-btn">Reiniciar</button>
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
122
3-en-raya-computer/script.js
Normal file
122
3-en-raya-computer/script.js
Normal file
@@ -0,0 +1,122 @@
|
||||
const boardDiv = document.getElementById('board');
|
||||
const statusDiv = document.getElementById('status');
|
||||
const restartBtn = document.getElementById('restart-btn');
|
||||
let board, gameOver;
|
||||
|
||||
function initGame() {
|
||||
board = Array(9).fill('');
|
||||
gameOver = false;
|
||||
statusDiv.textContent = "Tu turno (X)";
|
||||
renderBoard();
|
||||
}
|
||||
|
||||
function renderBoard() {
|
||||
boardDiv.innerHTML = '';
|
||||
board.forEach((cell, idx) => {
|
||||
const cellDiv = document.createElement('div');
|
||||
cellDiv.className = 'cell';
|
||||
cellDiv.textContent = cell;
|
||||
cellDiv.onclick = () => handlePlayerMove(idx);
|
||||
boardDiv.appendChild(cellDiv);
|
||||
});
|
||||
}
|
||||
|
||||
function handlePlayerMove(idx) {
|
||||
if (gameOver || board[idx] !== '') return;
|
||||
board[idx] = 'X';
|
||||
renderBoard();
|
||||
if (checkWinner('X')) {
|
||||
statusDiv.textContent = `¡Tú ganas! 🎉`;
|
||||
gameOver = true;
|
||||
return;
|
||||
}
|
||||
if (isDraw()) {
|
||||
statusDiv.textContent = "¡Empate!";
|
||||
gameOver = true;
|
||||
return;
|
||||
}
|
||||
statusDiv.textContent = "Turno de la máquina (O)";
|
||||
setTimeout(machineMove, 400);
|
||||
}
|
||||
|
||||
function machineMove() {
|
||||
if (gameOver) return;
|
||||
let bestScore = -Infinity;
|
||||
let move = null;
|
||||
for (let i = 0; i < 9; i++) {
|
||||
if (board[i] === '') {
|
||||
board[i] = 'O';
|
||||
let score = minimax(board, 0, false);
|
||||
board[i] = '';
|
||||
if (score > bestScore) {
|
||||
bestScore = score;
|
||||
move = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (move !== null) board[move] = 'O';
|
||||
renderBoard();
|
||||
if (checkWinner('O')) {
|
||||
statusDiv.textContent = `¡La máquina gana! 🤖`;
|
||||
gameOver = true;
|
||||
return;
|
||||
}
|
||||
if (isDraw()) {
|
||||
statusDiv.textContent = "¡Empate!";
|
||||
gameOver = true;
|
||||
return;
|
||||
}
|
||||
statusDiv.textContent = "Tu turno (X)";
|
||||
}
|
||||
|
||||
// Algoritmo Minimax
|
||||
function minimax(newBoard, depth, isMaximizing) {
|
||||
if (checkWinnerOnBoard(newBoard, 'O')) return 10 - depth;
|
||||
if (checkWinnerOnBoard(newBoard, 'X')) return depth - 10;
|
||||
if (newBoard.every(cell => cell !== '')) return 0;
|
||||
|
||||
if (isMaximizing) {
|
||||
let bestScore = -Infinity;
|
||||
for (let i = 0; i < 9; i++) {
|
||||
if (newBoard[i] === '') {
|
||||
newBoard[i] = 'O';
|
||||
let score = minimax(newBoard, depth + 1, false);
|
||||
newBoard[i] = '';
|
||||
bestScore = Math.max(score, bestScore);
|
||||
}
|
||||
}
|
||||
return bestScore;
|
||||
} else {
|
||||
let bestScore = Infinity;
|
||||
for (let i = 0; i < 9; i++) {
|
||||
if (newBoard[i] === '') {
|
||||
newBoard[i] = 'X';
|
||||
let score = minimax(newBoard, depth + 1, true);
|
||||
newBoard[i] = '';
|
||||
bestScore = Math.min(score, bestScore);
|
||||
}
|
||||
}
|
||||
return bestScore;
|
||||
}
|
||||
}
|
||||
|
||||
function checkWinner(player) {
|
||||
return checkWinnerOnBoard(board, player);
|
||||
}
|
||||
|
||||
function checkWinnerOnBoard(b, player) {
|
||||
const wins = [
|
||||
[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 wins.some(combo => combo.every(i => b[i] === player));
|
||||
}
|
||||
|
||||
function isDraw() {
|
||||
return board.every(cell => cell !== '') && !checkWinner('X') && !checkWinner('O');
|
||||
}
|
||||
|
||||
restartBtn.onclick = initGame;
|
||||
|
||||
initGame();
|
122
3-en-raya-computer/styles.css
Normal file
122
3-en-raya-computer/styles.css
Normal file
@@ -0,0 +1,122 @@
|
||||
body {
|
||||
background: #eef2f3;
|
||||
font-family: Arial, sans-serif;
|
||||
text-align: center;
|
||||
color: #222;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin-top: 2rem;
|
||||
color: #3498db;
|
||||
font-size: 2.2rem;
|
||||
line-height: 1.2;
|
||||
}
|
||||
|
||||
#board {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, minmax(60px, 26vw));
|
||||
grid-template-rows: repeat(3, minmax(60px, 26vw));
|
||||
gap: 0.7em;
|
||||
justify-content: center;
|
||||
margin: 2rem auto 1.2rem auto;
|
||||
max-width: 95vw;
|
||||
}
|
||||
|
||||
.cell {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: #fff;
|
||||
border-radius: 6px;
|
||||
box-shadow: 0 2px 8px #bbb;
|
||||
font-size: 10em;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.cell:hover,
|
||||
.cell:focus {
|
||||
background: #f1fafe;
|
||||
}
|
||||
|
||||
#status {
|
||||
font-size: 1.1em;
|
||||
min-height: 2em;
|
||||
margin-bottom: 1em;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
#restart-btn {
|
||||
background: #3498db;
|
||||
color: #fff;
|
||||
font-size: 1em;
|
||||
border: none;
|
||||
border-radius: 7px;
|
||||
padding: 0.7em 2em;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
margin-bottom: 2em;
|
||||
}
|
||||
|
||||
#restart-btn:hover,
|
||||
#restart-btn:focus {
|
||||
background: #297fb8;
|
||||
}
|
||||
|
||||
/* Celulares */
|
||||
@media (max-width: 480px) {
|
||||
h1 {
|
||||
font-size: 1.3rem;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
#board {
|
||||
grid-template-columns: repeat(3, minmax(38px, 30vw));
|
||||
grid-template-rows: repeat(3, minmax(38px, 30vw));
|
||||
gap: 0.4em;
|
||||
margin: 1rem auto;
|
||||
}
|
||||
.cell {
|
||||
font-size: 10em;
|
||||
border-radius: 4px;
|
||||
}
|
||||
#status {
|
||||
font-size: 1em;
|
||||
min-height: 1.2em;
|
||||
margin-bottom: 0.7em;
|
||||
}
|
||||
#restart-btn {
|
||||
font-size: 0.95em;
|
||||
padding: 0.5em 1.1em;
|
||||
border-radius: 5px;
|
||||
}
|
||||
}
|
||||
|
||||
/* Tablets */
|
||||
@media (max-width: 768px) {
|
||||
h1 {
|
||||
font-size: 1.6rem;
|
||||
}
|
||||
#board {
|
||||
grid-template-columns: repeat(3, minmax(50px, 22vw));
|
||||
grid-template-rows: repeat(3, minmax(50px, 22vw));
|
||||
}
|
||||
.cell {
|
||||
font-size: 10em;
|
||||
}
|
||||
}
|
||||
|
||||
/* Pantallas grandes */
|
||||
@media (min-width: 1200px) {
|
||||
#board {
|
||||
grid-template-columns: repeat(3, 100px);
|
||||
grid-template-rows: repeat(3, 100px);
|
||||
gap: 1em;
|
||||
}
|
||||
.cell {
|
||||
font-size: 3em;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user