and doesnt seem like attacking works - Follow Up Deployment
Browse files- README.md +7 -5
- index.html +993 -19
- prompts.txt +3 -0
README.md
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: battleship-oldschool
|
| 3 |
+
emoji: 🐳
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: pink
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
+
tags:
|
| 9 |
+
- deepsite
|
| 10 |
---
|
| 11 |
|
| 12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
index.html
CHANGED
|
@@ -1,19 +1,993 @@
|
|
| 1 |
-
<!
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Battleship Game</title>
|
| 7 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
| 8 |
+
<style>
|
| 9 |
+
.cell {
|
| 10 |
+
width: 40px;
|
| 11 |
+
height: 40px;
|
| 12 |
+
transition: all 0.2s ease;
|
| 13 |
+
}
|
| 14 |
+
.cell:hover:not(.hit):not(.miss) {
|
| 15 |
+
transform: scale(1.1);
|
| 16 |
+
box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
|
| 17 |
+
}
|
| 18 |
+
.ship {
|
| 19 |
+
background-color: #4b5563;
|
| 20 |
+
}
|
| 21 |
+
.hit {
|
| 22 |
+
background-color: #ef4444;
|
| 23 |
+
position: relative;
|
| 24 |
+
}
|
| 25 |
+
.hit::after {
|
| 26 |
+
content: "✖";
|
| 27 |
+
position: absolute;
|
| 28 |
+
top: 50%;
|
| 29 |
+
left: 50%;
|
| 30 |
+
transform: translate(-50%, -50%);
|
| 31 |
+
color: white;
|
| 32 |
+
font-size: 20px;
|
| 33 |
+
}
|
| 34 |
+
.miss {
|
| 35 |
+
background-color: #60a5fa;
|
| 36 |
+
position: relative;
|
| 37 |
+
}
|
| 38 |
+
.miss::after {
|
| 39 |
+
content: "•";
|
| 40 |
+
position: absolute;
|
| 41 |
+
top: 50%;
|
| 42 |
+
left: 50%;
|
| 43 |
+
transform: translate(-50%, -50%);
|
| 44 |
+
color: white;
|
| 45 |
+
font-size: 20px;
|
| 46 |
+
}
|
| 47 |
+
@media (max-width: 768px) {
|
| 48 |
+
.cell {
|
| 49 |
+
width: 30px;
|
| 50 |
+
height: 30px;
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
@media (max-width: 480px) {
|
| 54 |
+
.cell {
|
| 55 |
+
width: 25px;
|
| 56 |
+
height: 25px;
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
.ship-preview {
|
| 60 |
+
opacity: 0.7;
|
| 61 |
+
}
|
| 62 |
+
.ship-placing {
|
| 63 |
+
background-color: #10b981;
|
| 64 |
+
}
|
| 65 |
+
.ship-invalid {
|
| 66 |
+
background-color: #f59e0b;
|
| 67 |
+
}
|
| 68 |
+
.ship-sunk {
|
| 69 |
+
background-color: #991b1b;
|
| 70 |
+
}
|
| 71 |
+
.ship-sunk::after {
|
| 72 |
+
content: "☠";
|
| 73 |
+
position: absolute;
|
| 74 |
+
top: 50%;
|
| 75 |
+
left: 50%;
|
| 76 |
+
transform: translate(-50%, -50%);
|
| 77 |
+
color: white;
|
| 78 |
+
font-size: 20px;
|
| 79 |
+
}
|
| 80 |
+
</style>
|
| 81 |
+
</head>
|
| 82 |
+
<body class="bg-gray-900 text-white min-h-screen flex flex-col items-center justify-center p-4">
|
| 83 |
+
<div class="container mx-auto max-w-6xl">
|
| 84 |
+
<h1 class="text-4xl font-bold text-center mb-8 text-blue-400">Battleship Game</h1>
|
| 85 |
+
|
| 86 |
+
<div id="setup-screen" class="text-center mb-8">
|
| 87 |
+
<h2 class="text-2xl font-semibold mb-4">Place Your Ships</h2>
|
| 88 |
+
<div class="flex flex-wrap justify-center gap-4 mb-6">
|
| 89 |
+
<div id="carrier" class="flex items-center bg-gray-800 p-2 rounded cursor-move" draggable="true" data-size="5">
|
| 90 |
+
<div class="flex">
|
| 91 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 92 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 93 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 94 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 95 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 96 |
+
</div>
|
| 97 |
+
<span class="ml-2">Carrier (5)</span>
|
| 98 |
+
</div>
|
| 99 |
+
<div id="battleship" class="flex items-center bg-gray-800 p-2 rounded cursor-move" draggable="true" data-size="4">
|
| 100 |
+
<div class="flex">
|
| 101 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 102 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 103 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 104 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 105 |
+
</div>
|
| 106 |
+
<span class="ml-2">Battleship (4)</span>
|
| 107 |
+
</div>
|
| 108 |
+
<div id="cruiser" class="flex items-center bg-gray-800 p-2 rounded cursor-move" draggable="true" data-size="3">
|
| 109 |
+
<div class="flex">
|
| 110 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 111 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 112 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 113 |
+
</div>
|
| 114 |
+
<span class="ml-2">Cruiser (3)</span>
|
| 115 |
+
</div>
|
| 116 |
+
<div id="submarine" class="flex items-center bg-gray-800 p-2 rounded cursor-move" draggable="true" data-size="3">
|
| 117 |
+
<div class="flex">
|
| 118 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 119 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 120 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 121 |
+
</div>
|
| 122 |
+
<span class="ml-2">Submarine (3)</span>
|
| 123 |
+
</div>
|
| 124 |
+
<div id="destroyer" class="flex items-center bg-gray-800 p-2 rounded cursor-move" draggable="true" data-size="2">
|
| 125 |
+
<div class="flex">
|
| 126 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 127 |
+
<div class="cell ship-preview bg-gray-600 border border-gray-400"></div>
|
| 128 |
+
</div>
|
| 129 |
+
<span class="ml-2">Destroyer (2)</span>
|
| 130 |
+
</div>
|
| 131 |
+
</div>
|
| 132 |
+
<div class="flex flex-col md:flex-row justify-center gap-8 mb-6">
|
| 133 |
+
<div>
|
| 134 |
+
<h3 class="text-xl font-medium mb-2">Your Board</h3>
|
| 135 |
+
<div id="player-board" class="grid grid-cols-10 gap-0 border-2 border-blue-400 bg-blue-900 bg-opacity-20">
|
| 136 |
+
<!-- Player board will be generated here -->
|
| 137 |
+
</div>
|
| 138 |
+
</div>
|
| 139 |
+
<div class="mt-8 md:mt-0">
|
| 140 |
+
<div class="flex items-center justify-between mb-2">
|
| 141 |
+
<h3 class="text-xl font-medium">Orientation</h3>
|
| 142 |
+
<button id="rotate-btn" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded">
|
| 143 |
+
Horizontal ↔
|
| 144 |
+
</button>
|
| 145 |
+
</div>
|
| 146 |
+
<div id="instructions" class="bg-gray-800 p-4 rounded-lg mb-4">
|
| 147 |
+
<p class="mb-2">Drag and drop ships onto your board.</p>
|
| 148 |
+
<p>Click the button above to change ship orientation.</p>
|
| 149 |
+
</div>
|
| 150 |
+
<button id="start-game-btn" class="bg-green-600 hover:bg-green-700 text-white px-6 py-3 rounded-lg text-lg font-semibold hidden">
|
| 151 |
+
Start Game
|
| 152 |
+
</button>
|
| 153 |
+
<button id="randomize-btn" class="bg-purple-600 hover:bg-purple-700 text-white px-4 py-2 rounded-lg mr-2">
|
| 154 |
+
Randomize
|
| 155 |
+
</button>
|
| 156 |
+
<button id="reset-btn" class="bg-red-600 hover:bg-red-700 text-white px-4 py-2 rounded-lg">
|
| 157 |
+
Reset
|
| 158 |
+
</button>
|
| 159 |
+
</div>
|
| 160 |
+
</div>
|
| 161 |
+
</div>
|
| 162 |
+
|
| 163 |
+
<div id="game-screen" class="hidden">
|
| 164 |
+
<div class="flex flex-col md:flex-row justify-center gap-8 mb-8">
|
| 165 |
+
<div>
|
| 166 |
+
<h3 class="text-xl font-medium mb-2">Your Board</h3>
|
| 167 |
+
<div id="player-game-board" class="grid grid-cols-10 gap-0 border-2 border-blue-400 bg-blue-900 bg-opacity-20">
|
| 168 |
+
<!-- Player game board will be generated here -->
|
| 169 |
+
</div>
|
| 170 |
+
</div>
|
| 171 |
+
<div>
|
| 172 |
+
<h3 class="text-xl font-medium mb-2">Enemy Board</h3>
|
| 173 |
+
<div id="enemy-board" class="grid grid-cols-10 gap-0 border-2 border-red-400 bg-red-900 bg-opacity-20">
|
| 174 |
+
<!-- Enemy board will be generated here -->
|
| 175 |
+
</div>
|
| 176 |
+
</div>
|
| 177 |
+
</div>
|
| 178 |
+
|
| 179 |
+
<div class="text-center mt-6">
|
| 180 |
+
<div id="game-status" class="text-2xl font-semibold mb-4 text-yellow-400"></div>
|
| 181 |
+
<div class="flex justify-center gap-4">
|
| 182 |
+
<button id="new-game-btn" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-2 rounded-lg">
|
| 183 |
+
New Game
|
| 184 |
+
</button>
|
| 185 |
+
<button id="show-ships-btn" class="bg-gray-600 hover:bg-gray-700 text-white px-6 py-2 rounded-lg">
|
| 186 |
+
Show Enemy Ships
|
| 187 |
+
</button>
|
| 188 |
+
</div>
|
| 189 |
+
</div>
|
| 190 |
+
</div>
|
| 191 |
+
|
| 192 |
+
<div id="game-over-screen" class="hidden text-center mt-8">
|
| 193 |
+
<h2 id="game-result" class="text-3xl font-bold mb-6"></h2>
|
| 194 |
+
<div class="flex justify-center gap-4">
|
| 195 |
+
<button id="play-again-btn" class="bg-green-600 hover:bg-green-700 text-white px-6 py-3 rounded-lg text-lg font-semibold">
|
| 196 |
+
Play Again
|
| 197 |
+
</button>
|
| 198 |
+
<button id="main-menu-btn" class="bg-blue-600 hover:bg-blue-700 text-white px-6 py-3 rounded-lg text-lg font-semibold">
|
| 199 |
+
Main Menu
|
| 200 |
+
</button>
|
| 201 |
+
</div>
|
| 202 |
+
</div>
|
| 203 |
+
</div>
|
| 204 |
+
|
| 205 |
+
<script>
|
| 206 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 207 |
+
// Game state
|
| 208 |
+
const gameState = {
|
| 209 |
+
playerBoard: [],
|
| 210 |
+
enemyBoard: [],
|
| 211 |
+
playerShips: [],
|
| 212 |
+
enemyShips: [],
|
| 213 |
+
currentShip: null,
|
| 214 |
+
currentShipSize: 0,
|
| 215 |
+
currentOrientation: 'horizontal',
|
| 216 |
+
gamePhase: 'setup', // 'setup', 'playing', 'gameOver'
|
| 217 |
+
playerTurn: true,
|
| 218 |
+
enemyBoardAttacks: [],
|
| 219 |
+
shipsToPlace: [
|
| 220 |
+
{ id: 'carrier', name: 'Carrier', size: 5 },
|
| 221 |
+
{ id: 'battleship', name: 'Battleship', size: 4 },
|
| 222 |
+
{ id: 'cruiser', name: 'Cruiser', size: 3 },
|
| 223 |
+
{ id: 'submarine', name: 'Submarine', size: 3 },
|
| 224 |
+
{ id: 'destroyer', name: 'Destroyer', size: 2 }
|
| 225 |
+
],
|
| 226 |
+
placedShips: 0
|
| 227 |
+
};
|
| 228 |
+
|
| 229 |
+
// DOM elements
|
| 230 |
+
const setupScreen = document.getElementById('setup-screen');
|
| 231 |
+
const gameScreen = document.getElementById('game-screen');
|
| 232 |
+
const gameOverScreen = document.getElementById('game-over-screen');
|
| 233 |
+
const playerBoard = document.getElementById('player-board');
|
| 234 |
+
const playerGameBoard = document.getElementById('player-game-board');
|
| 235 |
+
const enemyBoard = document.getElementById('enemy-board');
|
| 236 |
+
const rotateBtn = document.getElementById('rotate-btn');
|
| 237 |
+
const startGameBtn = document.getElementById('start-game-btn');
|
| 238 |
+
const randomizeBtn = document.getElementById('randomize-btn');
|
| 239 |
+
const resetBtn = document.getElementById('reset-btn');
|
| 240 |
+
const newGameBtn = document.getElementById('new-game-btn');
|
| 241 |
+
const showShipsBtn = document.getElementById('show-ships-btn');
|
| 242 |
+
const playAgainBtn = document.getElementById('play-again-btn');
|
| 243 |
+
const mainMenuBtn = document.getElementById('main-menu-btn');
|
| 244 |
+
const gameStatus = document.getElementById('game-status');
|
| 245 |
+
const gameResult = document.getElementById('game-result');
|
| 246 |
+
|
| 247 |
+
// Initialize boards
|
| 248 |
+
function initializeBoards() {
|
| 249 |
+
// Clear boards
|
| 250 |
+
playerBoard.innerHTML = '';
|
| 251 |
+
playerGameBoard.innerHTML = '';
|
| 252 |
+
enemyBoard.innerHTML = '';
|
| 253 |
+
|
| 254 |
+
// Initialize game state arrays
|
| 255 |
+
gameState.playerBoard = Array(10).fill().map(() => Array(10).fill(0));
|
| 256 |
+
gameState.enemyBoard = Array(10).fill().map(() => Array(10).fill(0));
|
| 257 |
+
gameState.playerShips = [];
|
| 258 |
+
gameState.enemyShips = [];
|
| 259 |
+
gameState.placedShips = 0;
|
| 260 |
+
|
| 261 |
+
// Create player board cells for setup
|
| 262 |
+
for (let row = 0; row < 10; row++) {
|
| 263 |
+
for (let col = 0; col < 10; col++) {
|
| 264 |
+
const cell = document.createElement('div');
|
| 265 |
+
cell.className = 'cell border border-gray-400 bg-blue-900 bg-opacity-30';
|
| 266 |
+
cell.dataset.row = row;
|
| 267 |
+
cell.dataset.col = col;
|
| 268 |
+
cell.addEventListener('dragover', handleDragOver);
|
| 269 |
+
cell.addEventListener('drop', handleDrop);
|
| 270 |
+
cell.addEventListener('dragenter', handleDragEnter);
|
| 271 |
+
cell.addEventListener('dragleave', handleDragLeave);
|
| 272 |
+
playerBoard.appendChild(cell);
|
| 273 |
+
}
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
// Create player game board cells
|
| 277 |
+
for (let row = 0; row < 10; row++) {
|
| 278 |
+
for (let col = 0; col < 10; col++) {
|
| 279 |
+
const cell = document.createElement('div');
|
| 280 |
+
cell.className = 'cell border border-gray-400 bg-blue-900 bg-opacity-30';
|
| 281 |
+
cell.dataset.row = row;
|
| 282 |
+
cell.dataset.col = col;
|
| 283 |
+
playerGameBoard.appendChild(cell);
|
| 284 |
+
}
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
// Create enemy board cells
|
| 288 |
+
for (let row = 0; row < 10; row++) {
|
| 289 |
+
for (let col = 0; col < 10; col++) {
|
| 290 |
+
const cell = document.createElement('div');
|
| 291 |
+
cell.className = 'cell border border-gray-400 bg-red-900 bg-opacity-30';
|
| 292 |
+
cell.dataset.row = row;
|
| 293 |
+
cell.dataset.col = col;
|
| 294 |
+
cell.addEventListener('click', handleEnemyCellClick);
|
| 295 |
+
enemyBoard.appendChild(cell);
|
| 296 |
+
}
|
| 297 |
+
}
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
// Setup drag and drop for ships
|
| 301 |
+
function setupDragAndDrop() {
|
| 302 |
+
const ships = document.querySelectorAll('[draggable="true"]');
|
| 303 |
+
|
| 304 |
+
ships.forEach(ship => {
|
| 305 |
+
ship.addEventListener('dragstart', handleDragStart);
|
| 306 |
+
});
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
// Handle drag start
|
| 310 |
+
function handleDragStart(e) {
|
| 311 |
+
gameState.currentShip = e.target.id;
|
| 312 |
+
gameState.currentShipSize = parseInt(e.target.dataset.size);
|
| 313 |
+
e.dataTransfer.setData('text/plain', e.target.id);
|
| 314 |
+
e.dataTransfer.effectAllowed = 'copy';
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
// Handle drag over
|
| 318 |
+
function handleDragOver(e) {
|
| 319 |
+
e.preventDefault();
|
| 320 |
+
e.dataTransfer.dropEffect = 'copy';
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
// Handle drag enter
|
| 324 |
+
function handleDragEnter(e) {
|
| 325 |
+
if (gameState.currentShip) {
|
| 326 |
+
const row = parseInt(e.target.dataset.row);
|
| 327 |
+
const col = parseInt(e.target.dataset.col);
|
| 328 |
+
|
| 329 |
+
// Highlight potential ship placement
|
| 330 |
+
highlightShipPlacement(row, col, gameState.currentShipSize, gameState.currentOrientation);
|
| 331 |
+
}
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
// Handle drag leave
|
| 335 |
+
function handleDragLeave(e) {
|
| 336 |
+
// Remove all temporary placement classes
|
| 337 |
+
const cells = playerBoard.querySelectorAll('.cell');
|
| 338 |
+
cells.forEach(cell => {
|
| 339 |
+
cell.classList.remove('ship-placing', 'ship-invalid');
|
| 340 |
+
});
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
// Handle drop
|
| 344 |
+
function handleDrop(e) {
|
| 345 |
+
e.preventDefault();
|
| 346 |
+
|
| 347 |
+
const row = parseInt(e.target.dataset.row);
|
| 348 |
+
const col = parseInt(e.target.dataset.col);
|
| 349 |
+
const shipId = e.dataTransfer.getData('text/plain');
|
| 350 |
+
const shipSize = gameState.currentShipSize;
|
| 351 |
+
|
| 352 |
+
// Check if placement is valid
|
| 353 |
+
if (canPlaceShip(row, col, shipSize, gameState.currentOrientation)) {
|
| 354 |
+
// Place the ship
|
| 355 |
+
placeShip(row, col, shipSize, gameState.currentOrientation, shipId);
|
| 356 |
+
|
| 357 |
+
// Remove the ship from the available ships
|
| 358 |
+
const shipElement = document.getElementById(shipId);
|
| 359 |
+
shipElement.style.display = 'none';
|
| 360 |
+
|
| 361 |
+
// Increment placed ships counter
|
| 362 |
+
gameState.placedShips++;
|
| 363 |
+
|
| 364 |
+
// Check if all ships are placed
|
| 365 |
+
if (gameState.placedShips === gameState.shipsToPlace.length) {
|
| 366 |
+
startGameBtn.classList.remove('hidden');
|
| 367 |
+
}
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
// Remove all temporary placement classes
|
| 371 |
+
const cells = playerBoard.querySelectorAll('.cell');
|
| 372 |
+
cells.forEach(cell => {
|
| 373 |
+
cell.classList.remove('ship-placing', 'ship-invalid');
|
| 374 |
+
});
|
| 375 |
+
|
| 376 |
+
gameState.currentShip = null;
|
| 377 |
+
}
|
| 378 |
+
|
| 379 |
+
// Highlight potential ship placement
|
| 380 |
+
function highlightShipPlacement(row, col, size, orientation) {
|
| 381 |
+
// First remove all highlights
|
| 382 |
+
const cells = playerBoard.querySelectorAll('.cell');
|
| 383 |
+
cells.forEach(cell => {
|
| 384 |
+
cell.classList.remove('ship-placing', 'ship-invalid');
|
| 385 |
+
});
|
| 386 |
+
|
| 387 |
+
// Calculate cells that would be occupied
|
| 388 |
+
const cellsToHighlight = [];
|
| 389 |
+
let isValid = true;
|
| 390 |
+
|
| 391 |
+
if (orientation === 'horizontal') {
|
| 392 |
+
for (let i = 0; i < size; i++) {
|
| 393 |
+
const newCol = col + i;
|
| 394 |
+
if (newCol >= 10) {
|
| 395 |
+
isValid = false;
|
| 396 |
+
break;
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
// Check if cell is already occupied
|
| 400 |
+
if (gameState.playerBoard[row][newCol] !== 0) {
|
| 401 |
+
isValid = false;
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
cellsToHighlight.push({row, col: newCol});
|
| 405 |
+
}
|
| 406 |
+
} else { // vertical
|
| 407 |
+
for (let i = 0; i < size; i++) {
|
| 408 |
+
const newRow = row + i;
|
| 409 |
+
if (newRow >= 10) {
|
| 410 |
+
isValid = false;
|
| 411 |
+
break;
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
// Check if cell is already occupied
|
| 415 |
+
if (gameState.playerBoard[newRow][col] !== 0) {
|
| 416 |
+
isValid = false;
|
| 417 |
+
}
|
| 418 |
+
|
| 419 |
+
cellsToHighlight.push({row: newRow, col});
|
| 420 |
+
}
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
// Apply highlighting
|
| 424 |
+
cellsToHighlight.forEach(({row, col}) => {
|
| 425 |
+
const index = row * 10 + col;
|
| 426 |
+
const cell = playerBoard.children[index];
|
| 427 |
+
|
| 428 |
+
if (isValid) {
|
| 429 |
+
cell.classList.add('ship-placing');
|
| 430 |
+
} else {
|
| 431 |
+
cell.classList.add('ship-invalid');
|
| 432 |
+
}
|
| 433 |
+
});
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
// Check if ship can be placed
|
| 437 |
+
function canPlaceShip(row, col, size, orientation) {
|
| 438 |
+
if (orientation === 'horizontal') {
|
| 439 |
+
// Check if ship goes out of bounds
|
| 440 |
+
if (col + size > 10) return false;
|
| 441 |
+
|
| 442 |
+
// Check if any cell is already occupied
|
| 443 |
+
for (let i = 0; i < size; i++) {
|
| 444 |
+
if (gameState.playerBoard[row][col + i] !== 0) {
|
| 445 |
+
return false;
|
| 446 |
+
}
|
| 447 |
+
}
|
| 448 |
+
} else { // vertical
|
| 449 |
+
// Check if ship goes out of bounds
|
| 450 |
+
if (row + size > 10) return false;
|
| 451 |
+
|
| 452 |
+
// Check if any cell is already occupied
|
| 453 |
+
for (let i = 0; i < size; i++) {
|
| 454 |
+
if (gameState.playerBoard[row + i][col] !== 0) {
|
| 455 |
+
return false;
|
| 456 |
+
}
|
| 457 |
+
}
|
| 458 |
+
}
|
| 459 |
+
|
| 460 |
+
return true;
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
// Place a ship on the board
|
| 464 |
+
function placeShip(row, col, size, orientation, shipId) {
|
| 465 |
+
const shipCells = [];
|
| 466 |
+
|
| 467 |
+
if (orientation === 'horizontal') {
|
| 468 |
+
for (let i = 0; i < size; i++) {
|
| 469 |
+
const newCol = col + i;
|
| 470 |
+
if (newCol < 10) {
|
| 471 |
+
gameState.playerBoard[row][newCol] = shipId;
|
| 472 |
+
shipCells.push({row, col: newCol});
|
| 473 |
+
}
|
| 474 |
+
}
|
| 475 |
+
} else { // vertical
|
| 476 |
+
for (let i = 0; i < size; i++) {
|
| 477 |
+
const newRow = row + i;
|
| 478 |
+
if (newRow < 10) {
|
| 479 |
+
gameState.playerBoard[newRow][col] = shipId;
|
| 480 |
+
shipCells.push({row: newRow, col});
|
| 481 |
+
}
|
| 482 |
+
}
|
| 483 |
+
}
|
| 484 |
+
|
| 485 |
+
// Add to player ships
|
| 486 |
+
gameState.playerShips.push({
|
| 487 |
+
id: shipId,
|
| 488 |
+
size: size,
|
| 489 |
+
hits: 0,
|
| 490 |
+
cells: shipCells
|
| 491 |
+
});
|
| 492 |
+
|
| 493 |
+
// Update the board display
|
| 494 |
+
updatePlayerBoard();
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
// Update player board display
|
| 498 |
+
function updatePlayerBoard() {
|
| 499 |
+
for (let row = 0; row < 10; row++) {
|
| 500 |
+
for (let col = 0; col < 10; col++) {
|
| 501 |
+
const index = row * 10 + col;
|
| 502 |
+
const cell = playerBoard.children[index];
|
| 503 |
+
const gameCell = playerGameBoard.children[index];
|
| 504 |
+
|
| 505 |
+
// Clear any ship classes
|
| 506 |
+
cell.classList.remove('ship');
|
| 507 |
+
gameCell.classList.remove('ship');
|
| 508 |
+
|
| 509 |
+
// If cell has a ship, add ship class
|
| 510 |
+
if (gameState.playerBoard[row][col] !== 0) {
|
| 511 |
+
cell.classList.add('ship');
|
| 512 |
+
gameCell.classList.add('ship');
|
| 513 |
+
}
|
| 514 |
+
}
|
| 515 |
+
}
|
| 516 |
+
}
|
| 517 |
+
|
| 518 |
+
// Randomize ship placement
|
| 519 |
+
function randomizeShips() {
|
| 520 |
+
// Reset player board
|
| 521 |
+
gameState.playerBoard = Array(10).fill().map(() => Array(10).fill(0));
|
| 522 |
+
gameState.playerShips = [];
|
| 523 |
+
gameState.placedShips = 0;
|
| 524 |
+
|
| 525 |
+
// Show all ship elements
|
| 526 |
+
gameState.shipsToPlace.forEach(ship => {
|
| 527 |
+
document.getElementById(ship.id).style.display = 'flex';
|
| 528 |
+
});
|
| 529 |
+
|
| 530 |
+
// Place each ship randomly
|
| 531 |
+
gameState.shipsToPlace.forEach(ship => {
|
| 532 |
+
let placed = false;
|
| 533 |
+
let attempts = 0;
|
| 534 |
+
const maxAttempts = 100;
|
| 535 |
+
|
| 536 |
+
while (!placed && attempts < maxAttempts) {
|
| 537 |
+
const orientation = Math.random() < 0.5 ? 'horizontal' : 'vertical';
|
| 538 |
+
const row = Math.floor(Math.random() * 10);
|
| 539 |
+
const col = Math.floor(Math.random() * 10);
|
| 540 |
+
|
| 541 |
+
if (canPlaceShip(row, col, ship.size, orientation)) {
|
| 542 |
+
placeShip(row, col, ship.size, orientation, ship.id);
|
| 543 |
+
placed = true;
|
| 544 |
+
gameState.placedShips++;
|
| 545 |
+
}
|
| 546 |
+
|
| 547 |
+
attempts++;
|
| 548 |
+
}
|
| 549 |
+
|
| 550 |
+
if (!placed) {
|
| 551 |
+
console.error(`Could not place ${ship.name} after ${maxAttempts} attempts`);
|
| 552 |
+
}
|
| 553 |
+
});
|
| 554 |
+
|
| 555 |
+
// Update the board display
|
| 556 |
+
updatePlayerBoard();
|
| 557 |
+
|
| 558 |
+
// Show start button if all ships placed
|
| 559 |
+
if (gameState.placedShips === gameState.shipsToPlace.length) {
|
| 560 |
+
startGameBtn.classList.remove('hidden');
|
| 561 |
+
}
|
| 562 |
+
}
|
| 563 |
+
|
| 564 |
+
// Reset the game
|
| 565 |
+
function resetGame() {
|
| 566 |
+
// Reset game state
|
| 567 |
+
gameState.playerBoard = Array(10).fill().map(() => Array(10).fill(0));
|
| 568 |
+
gameState.playerShips = [];
|
| 569 |
+
gameState.placedShips = 0;
|
| 570 |
+
gameState.currentShip = null;
|
| 571 |
+
gameState.currentShipSize = 0;
|
| 572 |
+
gameState.currentOrientation = 'horizontal';
|
| 573 |
+
|
| 574 |
+
// Show all ship elements
|
| 575 |
+
gameState.shipsToPlace.forEach(ship => {
|
| 576 |
+
document.getElementById(ship.id).style.display = 'flex';
|
| 577 |
+
});
|
| 578 |
+
|
| 579 |
+
// Hide start button
|
| 580 |
+
startGameBtn.classList.add('hidden');
|
| 581 |
+
|
| 582 |
+
// Update the board display
|
| 583 |
+
updatePlayerBoard();
|
| 584 |
+
|
| 585 |
+
// Reset orientation button text
|
| 586 |
+
rotateBtn.textContent = 'Horizontal ↔';
|
| 587 |
+
}
|
| 588 |
+
|
| 589 |
+
// Start the game
|
| 590 |
+
function startGame() {
|
| 591 |
+
// Setup enemy ships randomly
|
| 592 |
+
setupEnemyShips();
|
| 593 |
+
|
| 594 |
+
// Switch to game screen
|
| 595 |
+
setupScreen.classList.add('hidden');
|
| 596 |
+
gameScreen.classList.remove('hidden');
|
| 597 |
+
|
| 598 |
+
// Update game status
|
| 599 |
+
gameStatus.textContent = "Your turn - Attack the enemy!";
|
| 600 |
+
gameState.playerTurn = true;
|
| 601 |
+
gameState.gamePhase = 'playing';
|
| 602 |
+
|
| 603 |
+
// Update player game board
|
| 604 |
+
updatePlayerGameBoard();
|
| 605 |
+
|
| 606 |
+
// Initialize enemy board attack tracking
|
| 607 |
+
gameState.enemyBoardAttacks = Array(10).fill().map(() => Array(10).fill(false));
|
| 608 |
+
}
|
| 609 |
+
|
| 610 |
+
// Setup enemy ships randomly
|
| 611 |
+
function setupEnemyShips() {
|
| 612 |
+
gameState.enemyBoard = Array(10).fill().map(() => Array(10).fill(0));
|
| 613 |
+
gameState.enemyShips = [];
|
| 614 |
+
|
| 615 |
+
// Place each ship randomly
|
| 616 |
+
gameState.shipsToPlace.forEach(ship => {
|
| 617 |
+
let placed = false;
|
| 618 |
+
let attempts = 0;
|
| 619 |
+
const maxAttempts = 500; // Increased attempts for better placement
|
| 620 |
+
|
| 621 |
+
while (!placed && attempts < maxAttempts) {
|
| 622 |
+
const orientation = Math.random() < 0.5 ? 'horizontal' : 'vertical';
|
| 623 |
+
// Ensure ships are placed within bounds
|
| 624 |
+
const row = orientation === 'vertical' ?
|
| 625 |
+
Math.floor(Math.random() * (10 - ship.size)) :
|
| 626 |
+
Math.floor(Math.random() * 10);
|
| 627 |
+
const col = orientation === 'horizontal' ?
|
| 628 |
+
Math.floor(Math.random() * (10 - ship.size)) :
|
| 629 |
+
Math.floor(Math.random() * 10);
|
| 630 |
+
|
| 631 |
+
if (canPlaceEnemyShip(row, col, ship.size, orientation)) {
|
| 632 |
+
placeEnemyShip(row, col, ship.size, orientation, ship.id);
|
| 633 |
+
placed = true;
|
| 634 |
+
}
|
| 635 |
+
|
| 636 |
+
attempts++;
|
| 637 |
+
}
|
| 638 |
+
|
| 639 |
+
if (!placed) {
|
| 640 |
+
console.error(`Could not place enemy ${ship.name} after ${maxAttempts} attempts`);
|
| 641 |
+
// Force placement by clearing board and trying again
|
| 642 |
+
gameState.enemyBoard = Array(10).fill().map(() => Array(10).fill(0));
|
| 643 |
+
gameState.enemyShips = [];
|
| 644 |
+
setupEnemyShips();
|
| 645 |
+
return;
|
| 646 |
+
}
|
| 647 |
+
});
|
| 648 |
+
|
| 649 |
+
console.log("Enemy ships placed:", gameState.enemyShips);
|
| 650 |
+
}
|
| 651 |
+
|
| 652 |
+
// Check if enemy ship can be placed
|
| 653 |
+
function canPlaceEnemyShip(row, col, size, orientation) {
|
| 654 |
+
if (orientation === 'horizontal') {
|
| 655 |
+
// Check if ship goes out of bounds
|
| 656 |
+
if (col + size > 10) return false;
|
| 657 |
+
|
| 658 |
+
// Check if any cell is already occupied
|
| 659 |
+
for (let i = 0; i < size; i++) {
|
| 660 |
+
if (gameState.enemyBoard[row][col + i] !== 0) {
|
| 661 |
+
return false;
|
| 662 |
+
}
|
| 663 |
+
}
|
| 664 |
+
} else { // vertical
|
| 665 |
+
// Check if ship goes out of bounds
|
| 666 |
+
if (row + size > 10) return false;
|
| 667 |
+
|
| 668 |
+
// Check if any cell is already occupied
|
| 669 |
+
for (let i = 0; i < size; i++) {
|
| 670 |
+
if (gameState.enemyBoard[row + i][col] !== 0) {
|
| 671 |
+
return false;
|
| 672 |
+
}
|
| 673 |
+
}
|
| 674 |
+
}
|
| 675 |
+
|
| 676 |
+
return true;
|
| 677 |
+
}
|
| 678 |
+
|
| 679 |
+
// Place an enemy ship
|
| 680 |
+
function placeEnemyShip(row, col, size, orientation, shipId) {
|
| 681 |
+
const shipCells = [];
|
| 682 |
+
|
| 683 |
+
if (orientation === 'horizontal') {
|
| 684 |
+
for (let i = 0; i < size; i++) {
|
| 685 |
+
const newCol = col + i;
|
| 686 |
+
if (newCol < 10) {
|
| 687 |
+
gameState.enemyBoard[row][newCol] = shipId;
|
| 688 |
+
shipCells.push({row, col: newCol});
|
| 689 |
+
}
|
| 690 |
+
}
|
| 691 |
+
} else { // vertical
|
| 692 |
+
for (let i = 0; i < size; i++) {
|
| 693 |
+
const newRow = row + i;
|
| 694 |
+
if (newRow < 10) {
|
| 695 |
+
gameState.enemyBoard[newRow][col] = shipId;
|
| 696 |
+
shipCells.push({row: newRow, col});
|
| 697 |
+
}
|
| 698 |
+
}
|
| 699 |
+
}
|
| 700 |
+
|
| 701 |
+
// Verify all cells were placed
|
| 702 |
+
if (shipCells.length === size) {
|
| 703 |
+
// Add to enemy ships
|
| 704 |
+
gameState.enemyShips.push({
|
| 705 |
+
id: shipId,
|
| 706 |
+
name: gameState.shipsToPlace.find(s => s.id === shipId).name,
|
| 707 |
+
size: size,
|
| 708 |
+
hits: 0,
|
| 709 |
+
cells: shipCells
|
| 710 |
+
});
|
| 711 |
+
return true;
|
| 712 |
+
}
|
| 713 |
+
return false;
|
| 714 |
+
}
|
| 715 |
+
|
| 716 |
+
// Update player game board
|
| 717 |
+
function updatePlayerGameBoard() {
|
| 718 |
+
for (let row = 0; row < 10; row++) {
|
| 719 |
+
for (let col = 0; col < 10; col++) {
|
| 720 |
+
const index = row * 10 + col;
|
| 721 |
+
const cell = playerGameBoard.children[index];
|
| 722 |
+
|
| 723 |
+
// Clear any classes
|
| 724 |
+
cell.className = 'cell border border-gray-400 bg-blue-900 bg-opacity-30';
|
| 725 |
+
|
| 726 |
+
// If cell has a ship, add ship class (only if hit)
|
| 727 |
+
if (gameState.playerBoard[row][col] !== 0) {
|
| 728 |
+
// Check if this cell has been hit by enemy
|
| 729 |
+
const cellHit = cell.classList.contains('hit');
|
| 730 |
+
if (cellHit) {
|
| 731 |
+
cell.classList.add('ship');
|
| 732 |
+
}
|
| 733 |
+
}
|
| 734 |
+
}
|
| 735 |
+
}
|
| 736 |
+
}
|
| 737 |
+
|
| 738 |
+
// Handle enemy cell click (player's attack)
|
| 739 |
+
function handleEnemyCellClick(e) {
|
| 740 |
+
if (!gameState.playerTurn || gameState.gamePhase !== 'playing') return;
|
| 741 |
+
|
| 742 |
+
const row = parseInt(e.target.dataset.row);
|
| 743 |
+
const col = parseInt(e.target.dataset.col);
|
| 744 |
+
|
| 745 |
+
// Check if cell was already attacked
|
| 746 |
+
if (e.target.classList.contains('hit') || e.target.classList.contains('miss')) {
|
| 747 |
+
return;
|
| 748 |
+
}
|
| 749 |
+
|
| 750 |
+
// Process attack
|
| 751 |
+
const hit = gameState.enemyBoard[row][col] !== 0;
|
| 752 |
+
gameState.enemyBoardAttacks[row][col] = true;
|
| 753 |
+
|
| 754 |
+
if (hit) {
|
| 755 |
+
// It's a hit!
|
| 756 |
+
e.target.classList.add('hit');
|
| 757 |
+
|
| 758 |
+
// Find which ship was hit
|
| 759 |
+
const shipId = gameState.enemyBoard[row][col];
|
| 760 |
+
const ship = gameState.enemyShips.find(s => s.id === shipId);
|
| 761 |
+
|
| 762 |
+
// Increment hit count
|
| 763 |
+
ship.hits++;
|
| 764 |
+
|
| 765 |
+
// Check if ship is sunk
|
| 766 |
+
if (ship.hits === ship.size) {
|
| 767 |
+
// Mark all ship cells as sunk
|
| 768 |
+
ship.cells.forEach(({row, col}) => {
|
| 769 |
+
const index = row * 10 + col;
|
| 770 |
+
const cell = enemyBoard.children[index];
|
| 771 |
+
cell.classList.add('ship-sunk');
|
| 772 |
+
});
|
| 773 |
+
|
| 774 |
+
gameStatus.textContent = `You sunk the enemy ${shipId}!`;
|
| 775 |
+
|
| 776 |
+
// Check if all enemy ships are sunk
|
| 777 |
+
if (gameState.enemyShips.every(ship => ship.hits === ship.size)) {
|
| 778 |
+
endGame(true);
|
| 779 |
+
return;
|
| 780 |
+
}
|
| 781 |
+
} else {
|
| 782 |
+
gameStatus.textContent = "Hit! Attack again!";
|
| 783 |
+
}
|
| 784 |
+
} else {
|
| 785 |
+
// It's a miss
|
| 786 |
+
e.target.classList.add('miss');
|
| 787 |
+
gameStatus.textContent = "Miss! Enemy's turn.";
|
| 788 |
+
gameState.playerTurn = false;
|
| 789 |
+
|
| 790 |
+
// Enemy's turn after a short delay
|
| 791 |
+
setTimeout(enemyTurn, 1000);
|
| 792 |
+
}
|
| 793 |
+
}
|
| 794 |
+
|
| 795 |
+
// Enemy's turn
|
| 796 |
+
function enemyTurn() {
|
| 797 |
+
// Simple AI - random attacks with some intelligence
|
| 798 |
+
let row, col, index, cell;
|
| 799 |
+
let validAttack = false;
|
| 800 |
+
let attempts = 0;
|
| 801 |
+
const maxAttempts = 100;
|
| 802 |
+
|
| 803 |
+
// Try to find a cell that hasn't been attacked yet
|
| 804 |
+
while (!validAttack && attempts < maxAttempts) {
|
| 805 |
+
row = Math.floor(Math.random() * 10);
|
| 806 |
+
col = Math.floor(Math.random() * 10);
|
| 807 |
+
index = row * 10 + col;
|
| 808 |
+
cell = playerGameBoard.children[index];
|
| 809 |
+
|
| 810 |
+
if (!cell.classList.contains('hit') && !cell.classList.contains('miss')) {
|
| 811 |
+
validAttack = true;
|
| 812 |
+
}
|
| 813 |
+
|
| 814 |
+
attempts++;
|
| 815 |
+
}
|
| 816 |
+
|
| 817 |
+
// If we couldn't find a valid cell (shouldn't happen in normal game)
|
| 818 |
+
if (!validAttack) {
|
| 819 |
+
console.error("Couldn't find valid attack cell");
|
| 820 |
+
gameState.playerTurn = true;
|
| 821 |
+
gameStatus.textContent = "Your turn - Attack the enemy!";
|
| 822 |
+
return;
|
| 823 |
+
}
|
| 824 |
+
|
| 825 |
+
// Process attack
|
| 826 |
+
const hit = gameState.playerBoard[row][col] !== 0;
|
| 827 |
+
|
| 828 |
+
if (hit) {
|
| 829 |
+
// Enemy hit our ship
|
| 830 |
+
cell.classList.add('hit');
|
| 831 |
+
|
| 832 |
+
// Find which ship was hit
|
| 833 |
+
const shipId = gameState.playerBoard[row][col];
|
| 834 |
+
const ship = gameState.playerShips.find(s => s.id === shipId);
|
| 835 |
+
|
| 836 |
+
// Increment hit count
|
| 837 |
+
ship.hits++;
|
| 838 |
+
|
| 839 |
+
// Check if ship is sunk
|
| 840 |
+
if (ship.hits === ship.size) {
|
| 841 |
+
// Mark all ship cells as sunk
|
| 842 |
+
ship.cells.forEach(({row, col}) => {
|
| 843 |
+
const index = row * 10 + col;
|
| 844 |
+
const cell = playerGameBoard.children[index];
|
| 845 |
+
cell.classList.add('ship-sunk');
|
| 846 |
+
});
|
| 847 |
+
|
| 848 |
+
gameStatus.textContent = `Enemy sunk your ${shipId}!`;
|
| 849 |
+
|
| 850 |
+
// Check if all our ships are sunk
|
| 851 |
+
if (gameState.playerShips.every(ship => ship.hits === ship.size)) {
|
| 852 |
+
endGame(false);
|
| 853 |
+
return;
|
| 854 |
+
}
|
| 855 |
+
} else {
|
| 856 |
+
gameStatus.textContent = "Enemy hit your ship! Their turn continues.";
|
| 857 |
+
}
|
| 858 |
+
|
| 859 |
+
// Enemy gets another turn after hitting
|
| 860 |
+
setTimeout(enemyTurn, 1000);
|
| 861 |
+
} else {
|
| 862 |
+
// Enemy missed
|
| 863 |
+
cell.classList.add('miss');
|
| 864 |
+
gameStatus.textContent = "Enemy missed! Your turn.";
|
| 865 |
+
gameState.playerTurn = true;
|
| 866 |
+
}
|
| 867 |
+
}
|
| 868 |
+
|
| 869 |
+
// End the game
|
| 870 |
+
function endGame(playerWon) {
|
| 871 |
+
gameState.gamePhase = 'gameOver';
|
| 872 |
+
gameScreen.classList.add('hidden');
|
| 873 |
+
gameOverScreen.classList.remove('hidden');
|
| 874 |
+
|
| 875 |
+
if (playerWon) {
|
| 876 |
+
gameResult.textContent = "Congratulations! You won!";
|
| 877 |
+
gameResult.className = "text-3xl font-bold mb-6 text-green-400";
|
| 878 |
+
} else {
|
| 879 |
+
gameResult.textContent = "Game Over! You lost!";
|
| 880 |
+
gameResult.className = "text-3xl font-bold mb-6 text-red-400";
|
| 881 |
+
}
|
| 882 |
+
|
| 883 |
+
// Show all enemy ships
|
| 884 |
+
showEnemyShips();
|
| 885 |
+
}
|
| 886 |
+
|
| 887 |
+
// Show enemy ships
|
| 888 |
+
function showEnemyShips() {
|
| 889 |
+
for (let row = 0; row < 10; row++) {
|
| 890 |
+
for (let col = 0; col < 10; col++) {
|
| 891 |
+
const index = row * 10 + col;
|
| 892 |
+
const cell = enemyBoard.children[index];
|
| 893 |
+
|
| 894 |
+
if (gameState.enemyBoard[row][col] !== 0) {
|
| 895 |
+
cell.classList.add('ship');
|
| 896 |
+
}
|
| 897 |
+
}
|
| 898 |
+
}
|
| 899 |
+
}
|
| 900 |
+
|
| 901 |
+
// Toggle showing enemy ships
|
| 902 |
+
function toggleShowEnemyShips() {
|
| 903 |
+
const cells = enemyBoard.querySelectorAll('.cell');
|
| 904 |
+
let showing = false;
|
| 905 |
+
|
| 906 |
+
// Check if ships are currently shown
|
| 907 |
+
cells.forEach(cell => {
|
| 908 |
+
if (cell.classList.contains('ship')) {
|
| 909 |
+
showing = true;
|
| 910 |
+
}
|
| 911 |
+
});
|
| 912 |
+
|
| 913 |
+
// Toggle based on current state
|
| 914 |
+
cells.forEach(cell => {
|
| 915 |
+
if (gameState.enemyBoard[cell.dataset.row][cell.dataset.col] !== 0) {
|
| 916 |
+
if (showing) {
|
| 917 |
+
cell.classList.remove('ship');
|
| 918 |
+
showShipsBtn.textContent = 'Show Enemy Ships';
|
| 919 |
+
} else {
|
| 920 |
+
cell.classList.add('ship');
|
| 921 |
+
showShipsBtn.textContent = 'Hide Enemy Ships';
|
| 922 |
+
}
|
| 923 |
+
}
|
| 924 |
+
});
|
| 925 |
+
}
|
| 926 |
+
|
| 927 |
+
// New game
|
| 928 |
+
function newGame() {
|
| 929 |
+
// Reset game state
|
| 930 |
+
gameState.gamePhase = 'setup';
|
| 931 |
+
gameState.playerTurn = true;
|
| 932 |
+
|
| 933 |
+
// Switch to setup screen
|
| 934 |
+
gameScreen.classList.add('hidden');
|
| 935 |
+
gameOverScreen.classList.add('hidden');
|
| 936 |
+
setupScreen.classList.remove('hidden');
|
| 937 |
+
|
| 938 |
+
// Reset boards
|
| 939 |
+
initializeBoards();
|
| 940 |
+
resetGame();
|
| 941 |
+
}
|
| 942 |
+
|
| 943 |
+
// Play again
|
| 944 |
+
function playAgain() {
|
| 945 |
+
// Reset game state
|
| 946 |
+
gameState.gamePhase = 'setup';
|
| 947 |
+
gameState.playerTurn = true;
|
| 948 |
+
|
| 949 |
+
// Switch to setup screen
|
| 950 |
+
gameOverScreen.classList.add('hidden');
|
| 951 |
+
setupScreen.classList.remove('hidden');
|
| 952 |
+
|
| 953 |
+
// Reset boards
|
| 954 |
+
initializeBoards();
|
| 955 |
+
resetGame();
|
| 956 |
+
}
|
| 957 |
+
|
| 958 |
+
// Return to main menu
|
| 959 |
+
function mainMenu() {
|
| 960 |
+
// Reset game state
|
| 961 |
+
gameState.gamePhase = 'setup';
|
| 962 |
+
gameState.playerTurn = true;
|
| 963 |
+
|
| 964 |
+
// Switch to setup screen
|
| 965 |
+
gameOverScreen.classList.add('hidden');
|
| 966 |
+
setupScreen.classList.remove('hidden');
|
| 967 |
+
|
| 968 |
+
// Reset boards
|
| 969 |
+
initializeBoards();
|
| 970 |
+
resetGame();
|
| 971 |
+
}
|
| 972 |
+
|
| 973 |
+
// Event listeners
|
| 974 |
+
rotateBtn.addEventListener('click', () => {
|
| 975 |
+
gameState.currentOrientation = gameState.currentOrientation === 'horizontal' ? 'vertical' : 'horizontal';
|
| 976 |
+
rotateBtn.textContent = gameState.currentOrientation === 'horizontal' ? 'Horizontal ↔' : 'Vertical ↕';
|
| 977 |
+
});
|
| 978 |
+
|
| 979 |
+
startGameBtn.addEventListener('click', startGame);
|
| 980 |
+
randomizeBtn.addEventListener('click', randomizeShips);
|
| 981 |
+
resetBtn.addEventListener('click', resetGame);
|
| 982 |
+
newGameBtn.addEventListener('click', newGame);
|
| 983 |
+
showShipsBtn.addEventListener('click', toggleShowEnemyShips);
|
| 984 |
+
playAgainBtn.addEventListener('click', playAgain);
|
| 985 |
+
mainMenuBtn.addEventListener('click', mainMenu);
|
| 986 |
+
|
| 987 |
+
// Initialize the game
|
| 988 |
+
initializeBoards();
|
| 989 |
+
setupDragAndDrop();
|
| 990 |
+
});
|
| 991 |
+
</script>
|
| 992 |
+
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://deepsite.hf.co/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://deepsite.hf.co" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://deepsite.hf.co?remix=MikePfunk28/battleship-oldschool" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
|
| 993 |
+
</html>
|
prompts.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
can you make a battleship game
|
| 2 |
+
the vertical doesnt work, and it seems no one places the enemy board
|
| 3 |
+
and doesnt seem like attacking works
|