-
Notifications
You must be signed in to change notification settings - Fork 19
/
wumpus.html
219 lines (199 loc) · 7.26 KB
/
wumpus.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simulador Wumpus World</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
background-color: #f4f4f4;
}
#game-container {
display: grid;
grid-template-columns: repeat(4, 50px);
grid-template-rows: repeat(4, 50px);
gap: 2px;
margin-bottom: 10px;
}
.cell {
width: 50px;
height: 50px;
background-color: #e0e0e0;
display: flex;
justify-content: center;
align-items: center;
border: 1px solid #ccc;
font-size: 1.5em; /* Ajuste o tamanho do emoji */
position: relative;
}
.agent {
color: blue;
z-index: 2; /* Agente está sobre os outros */
}
.wumpus {
color: brown;
z-index: 1; /* Wumpus está abaixo do agente */
}
.pit {
color: gray;
z-index: 1; /* Buracos estão abaixo do agente */
}
.gold {
color: gold;
z-index: 1; /* Ouro está abaixo do agente */
}
.odor {
color: lightyellow;
z-index: 0; /* Fedor está abaixo do agente */
}
.hidden {
visibility: hidden; /* Esconde o conteúdo */
}
.controls {
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="controls">
<button id="reset-button">Reiniciar</button>
<button id="toggle-button">Esconder/Revelar</button>
</div>
<div id="game-container"></div>
<script>
const SIZE = 4;
const EMPTY = 0;
const AGENT = 1;
const WUMPUS = 2;
const PIT = 3;
const GOLD = 4;
const ODOR = 5;
let agentPosition = { x: 0, y: 0 };
let wumpusPosition = { x: Math.floor(Math.random() * SIZE), y: Math.floor(Math.random() * SIZE) };
let pitPositions = Array.from({ length: 3 }, () => ({ x: Math.floor(Math.random() * SIZE), y: Math.floor(Math.random() * SIZE) }));
let goldPosition = { x: Math.floor(Math.random() * SIZE), y: Math.floor(Math.random() * SIZE) };
const grid = Array.from({ length: SIZE }, () => Array(SIZE).fill(EMPTY));
grid[agentPosition.y][agentPosition.x] = AGENT;
grid[wumpusPosition.y][wumpusPosition.x] = WUMPUS;
grid[goldPosition.y][goldPosition.x] = GOLD;
pitPositions.forEach(pit => grid[pit.y][pit.x] = PIT);
const container = document.getElementById('game-container');
function updateOdor() {
// Clear previous odors
grid.forEach((row, y) => {
row.forEach((cell, x) => {
if (cell === ODOR) grid[y][x] = EMPTY;
});
});
// Set odor around Wumpus
const directions = [
{ x: 0, y: -1 }, // Up
{ x: 0, y: 1 }, // Down
{ x: -1, y: 0 }, // Left
{ x: 1, y: 0 } // Right
];
directions.forEach(dir => {
const newX = wumpusPosition.x + dir.x;
const newY = wumpusPosition.y + dir.y;
if (newX >= 0 && newX < SIZE && newY >= 0 && newY < SIZE) {
grid[newY][newX] = ODOR;
}
});
}
function renderGrid(hidden = false) {
container.innerHTML = '';
grid.forEach((row, y) => {
row.forEach((cell, x) => {
const cellDiv = document.createElement('div');
cellDiv.className = 'cell';
if (cell === AGENT) {
cellDiv.classList.add('agent');
cellDiv.textContent = '🤖'; // Emoji para o agente
} else if (hidden && cell !== AGENT) {
cellDiv.classList.add('hidden');
} else if (cell === WUMPUS) {
cellDiv.classList.add('wumpus');
cellDiv.textContent = '🐉'; // Emoji para o Wumpus
} else if (cell === PIT) {
cellDiv.classList.add('pit');
cellDiv.textContent = '🕳️'; // Emoji para o buraco
} else if (cell === GOLD) {
cellDiv.classList.add('gold');
cellDiv.textContent = '💰'; // Emoji para o ouro
} else if (cell === ODOR) {
cellDiv.classList.add('odor');
cellDiv.textContent = '🌫️'; // Emoji para o fedor
}
container.appendChild(cellDiv);
});
});
}
function moveAgent(dx, dy) {
const newX = agentPosition.x + dx;
const newY = agentPosition.y + dy;
if (newX >= 0 && newX < SIZE && newY >= 0 && newY < SIZE) {
grid[agentPosition.y][agentPosition.x] = EMPTY;
agentPosition.x = newX;
agentPosition.y = newY;
grid[agentPosition.y][agentPosition.x] = AGENT;
if (agentPosition.x === wumpusPosition.x && agentPosition.y === wumpusPosition.y) {
alert('Você encontrou o Wumpus! Fim de jogo.');
resetGame();
} else if (pitPositions.some(pit => pit.x === agentPosition.x && pit.y === agentPosition.y)) {
alert('Você caiu em um buraco! Fim de jogo.');
resetGame();
} else if (agentPosition.x === goldPosition.x && agentPosition.y === goldPosition.y) {
alert('Você encontrou o Ouro! Parabéns!');
resetGame();
} else {
updateOdor();
renderGrid(hidden);
}
}
}
function resetGame() {
agentPosition = { x: 0, y: 0 };
wumpusPosition = { x: Math.floor(Math.random() * SIZE), y: Math.floor(Math.random() * SIZE) };
pitPositions = Array.from({ length: 3 }, () => ({ x: Math.floor(Math.random() * SIZE), y: Math.floor(Math.random() * SIZE) }));
goldPosition = { x: Math.floor(Math.random() * SIZE), y: Math.floor(Math.random() * SIZE) };
grid.forEach(row => row.fill(EMPTY));
grid[agentPosition.y][agentPosition.x] = AGENT;
grid[wumpusPosition.y][wumpusPosition.x] = WUMPUS;
grid[goldPosition.y][goldPosition.x] = GOLD;
pitPositions.forEach(pit => grid[pit.y][pit.x] = PIT);
updateOdor();
renderGrid(hidden);
}
let hidden = false;
document.addEventListener('keydown', (e) => {
switch (e.key) {
case 'ArrowUp':
moveAgent(0, -1);
break;
case 'ArrowDown':
moveAgent(0, 1);
break;
case 'ArrowLeft':
moveAgent(-1, 0);
break;
case 'ArrowRight':
moveAgent(1, 0);
break;
}
});
document.getElementById('reset-button').addEventListener('click', resetGame);
document.getElementById('toggle-button').addEventListener('click', () => {
hidden = !hidden;
renderGrid(hidden);
});
renderGrid(hidden);
</script>
</body>
</html>