Battleships
How do I make an battleships?
What is a battleships? How do you make a battleships? This script and codes were developed by Yang Li on 12 July 2022, Tuesday.
Battleships - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Battleships</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <h1>Welcome to Battleships!</h1> <h3>Where are the ships?</h3> <div id="board"> <div class="row"> <div class="column" id="n1"></div> <div class="column" id="n2"></div> <div class="column" id="n3"></div> <div class="column" id="n4"></div> <div class="column" id="n5"></div> </div> <div class="row"> <div class="column" id="n6"></div> <div class="column" id="n7"></div> <div class="column" id="n8"></div> <div class="column" id="n9"></div> <div class="column" id="n10"></div> </div> <div class="row"> <div class="column" id="n11"></div> <div class="column" id="n12"></div> <div class="column" id="n13"></div> <div class="column" id="n14"></div> <div class="column" id="n15"></div> </div> <div class="row"> <div class="column" id="n16"></div> <div class="column" id="n17"></div> <div class="column" id="n18"></div> <div class="column" id="n19"></div> <div class="column" id="n20"></div> </div> <div class="row"> <div class="column" id="n21"></div> <div class="column" id="n22"></div> <div class="column" id="n23"></div> <div class="column" id="n24"></div> <div class="column" id="n25"></div> </div> </div> <div id='try'> <h1>Try Again</h1> </div> <div id='success'> <h1>Nice!</h1> </div>
<!-- Extension --> <div id='win'> <h1>Winner!</h1> </div>
<button id='reset'>RESET</button> <script src="js/index.js"></script>
</body>
</html>
Battleships - Script Codes CSS Codes
body { text-align: center;
}
#board { width: 500px; height: 500px; border-style: double; border-color: black; margin: 0 auto;
}
.row { display: flex; width: 500px; height: 100px;
}
.column { width: 100px; height: 100px; border-style:solid; border-color: black; transition: all .2s ease-in-out;
}
.column:hover { background-color: red; transform: scale(1.25);
}
img { width: 80px; height: 80px; margin: 0 auto; margin-top: 5px;
}
#try { display: none;
}
#success { display: none;
}
#win { display: none;
}
#reset { width: 120px; height: 60px; font-size: 2em; margin-top: 20px; color: red;
}
Battleships - Script Codes JS Codes
// < Extension > Variable to hold number of ships found.
var shipsFound = 0;
// Boxes is an **array** of DOM elements
var boxes = document.getElementsByClassName("column");
// Writing a while loop to keep setting a random values until all three are different
while (ship1 == ship2 || ship2 == ship3 || ship1 == ship3) { // Using three variables to save our ship positions var ship1 = Math.floor(Math.random() * 26); var ship2 = Math.floor(Math.random() * 26); var ship3 = Math.floor(Math.random() * 26);
}
// Looping through all boxes so we can run an onclick on each one
for(var i = 0; i < boxes.length; i++) { // Declare variable to hold each single box var box = boxes[i]; // Creating an onclick event on the variabled we just declared box.onclick = function() { // Getting attribute from id and getting rid of the "n" just before it var str = this.getAttribute('id'); str = str.substring(1); // Converting string to integer var position = parseInt(str); // Using conditional to check if the clicked position matches a ship's location if (position == ship1 || position == ship2 || position == ship3) { // If we get a hit, the box changes to black this.style.backgroundColor = "black"; // < Extension > And we also append an image of a ship var image = document.createElement("img"); image.src = "http://www.ship-simulator-maritime-sar.com/img/schiff01.png"; this.appendChild(image); // Display success message document.getElementById("try").style.display = "none" document.getElementById("success").style.display = "block" // < Extension > Incrementing our ships found count shipsFound++; } else { // If we get no hits, display "try again" message document.getElementById("try").style.display = "block" document.getElementById("success").style.display = "none" } // < Extension > If 3 ships found, display "win" message if (shipsFound == 3) { document.getElementById("win").style.display = "block" document.getElementById("try").style.display = "none" document.getElementById("success").style.display = "none" } }
};
// < Extension > Programming Reset Button to refresh page
document.getElementById('reset').onclick = function(){ location.reload();
}
Developer | Yang Li |
Username | aussieyang |
Uploaded | July 12, 2022 |
Rating | 3 |
Size | 2,879 Kb |
Views | 40,480 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
My Friends Starter | 1,573 Kb |
Simple Hamburger Menu | 2,099 Kb |
Mac Bar | 2,204 Kb |
Geography Quiz | 2,136 Kb |
Battleships Starter | 2,429 Kb |
PokemonJourney | 2,688 Kb |
Goalkeeper | 2,373 Kb |
Fellowship of the Ring | 2,639 Kb |
Recreate Google | 1,853 Kb |
Googly Eye | 2,468 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Ghost Buttons with CSS3 | Mithicher | 2,509 Kb |
CircleType | Peterhry | 3,535 Kb |
Resume | Rottingroom | 5,483 Kb |
Sticky menu on scroll | Senff | 2,869 Kb |
Video mute | Leon9208 | 2,131 Kb |
SVG Icons Template | Legofsalmon | 2,618 Kb |
Nav Test -- cats 1 | Payls | 4,735 Kb |
The CodePen Logo | Kindofone | 4,259 Kb |
Scoreboard.js basic usage | Tbleckert | 1,733 Kb |
Ralph Lauren Mimic | Azureknight | 3,067 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!