Search and Rescue Game
About this app
A fun and challenging search and rescue game similar to Battleship
Search and Rescue Game Search and Rescue Game New Game
Related apps
Put this on your site
Source
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Search and Rescue Game</title>
<meta name="description" content="A fun and challenging search and rescue game similar to Battleship">
<meta name="keywords" content="search and rescue, game, battleship">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<script type="text/javascript">
try {
// App Javascript Goes Here. Place your entire script content inside the try block for error handling.
// This will run when the DOM is ready.
document.addEventListener("DOMContentLoaded", function() {
const boardSize = 10;
const shipLength = 3;
let board = [];
let ships = [];
let shipCount = 0;
let shots = 0;
let hits = 0;
// Generate the game board
function generateBoard() {
for (let i = 0; i < boardSize; i++) {
board[i] = [];
for (let j = 0; j < boardSize; j++) {
board[i][j] = 0;
}
}
}
// Generate random ship positions
function generateShips() {
ships = [];
shipCount = 0;
for (let i = 0; i < shipLength; i++) {
let ship = {
x: Math.floor(Math.random() * (boardSize - shipLength + 1)),
y: Math.floor(Math.random() * boardSize),
hit: false
};
ships.push(ship);
shipCount++;
}
}
// Handle click on a cell
function handleClick(x, y) {
shots++;
let hit = false;
for (let i = 0; i < ships.length; i++) {
if (x >= ships[i].x && x < ships[i].x + shipLength && y === ships[i].y) {
ships[i].hit = true;
hit = true;
hits++;
break;
}
}
board[x][y] = hit ? 1 : -1;
renderBoard();
checkGameStatus();
}
// Render the game board
function renderBoard() {
let html = '';
for (let i = 0; i < boardSize; i++) {
for (let j = 0; j < boardSize; j++) {
let className = '';
if (board[i][j] === 1) {
className = 'hit';
} else if (board[i][j] === -1) {
className = 'miss';
}
html += `<div class="cell ${className}" onclick="handleClick(${i}, ${j})"></div>`;
}
}
$('#board').html(html);
}
// Check game status
function checkGameStatus() {
if (hits === shipCount) {
$('#status').text(`Congratulations! You found all the ships in ${shots} shots.`);
}
}
// Start a new game
function newGame() {
generateBoard();
generateShips();
renderBoard();
shots = 0;
hits = 0;
$('#status').text('');
}
// Initialize the game
newGame();
});
} catch (error) {
// This will throw the error to the parent window.
throw error;
}
</script>
<style>
/* App CSS Goes Here */
body {
font-family: 'Arial', sans-serif;
background-color: #f0f0f0;
}
#main-container {
max-width: 600px;
margin: 0 auto;
padding: 50px 20px;
}
h1 {
color: #333;
text-align: center;
margin-bottom: 30px;
}
#board {
display: flex;
flex-wrap: wrap;
width: 300px;
margin: 0 auto;
justify-content: center;
}
.cell {
width: 30px;
height: 30px;
margin: 2px;
border: 1px solid #ccc;
background-color: #fff;
cursor: pointer;
transition: background-color 0.3s;
}
.cell.hit {
background-color: #ff5050;
}
.cell.miss {
background-color: #999;
}
#status {
text-align: center;
margin-top: 30px;
font-size: 18px;
font-weight: bold;
}
.btn {
display: block;
width: 100px;
margin: 0 auto;
margin-top: 30px;
padding: 10px;
text-align: center;
background-color: #333;
color: #fff;
text-decoration: none;
font-size: 16px;
font-weight: bold;
cursor: pointer;
border: none;
border-radius: 5px;
transition: background-color 0.3s;
}
.btn:hover {
background-color: #555;
}
</style>
</head>
<body>
<div id="main-container" class="container">
<h1>Search and Rescue Game</h1>
<div id="board"></div>
<div id="status"></div>
<button class="btn" onclick="newGame()">New Game</button>
</div>
</body>
</html>
NEW APPS
These are apps made by the community!