AB.loadCSSstring ( `
body {
background-color: #eff;
}
` );
var game = [ [".", ".", ".", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "."], ];
var board = document.createElement("PRE");
// Preparing the HTML <pre> element to display the board on the page
document.body.appendChild(board);
var button=document.createElement("BUTTON");
// Preparing the "Fire! button to allow the player to fire at the ship
button.onclick = fire; // Clicking the button calls the fire() function
var t=document.createTextNode("Fire!");
document.body.appendChild(button);
button.appendChild(t);
function drawBoard() {
var boardContents = "";
var i; var j;
for (i=0; i<9; i++) {
for (j=0; j<9; j++) {
boardContents = boardContents + game[i][j]+" ";
// Append array contents for each board square
}
boardContents = boardContents + "<br>";
// Append a line break at the end of each horizontal line
} return boardContents;
// Return string representing board in HTML
}
board.innerHTML = drawBoard();
// Display the board on the page using the above function
var x=prompt("Where would you like to place your ship? Enter an X coordinate: (0-8)");
var y=prompt("Where would you like to place your ship? Enter a Y coordinate: (0-8)");
var direction=prompt("Place (h)orizontally, (v)ertically");
x = Number(x); // Convert the string returned by "prompt" into a number
y = Number(y); // Convert the string returned by "prompt" into a number
if (direction[0] == "h") {
var c;
for (c = x; c < (x + 4); c++)
{
game[y][c] = '#';
}
}
// Draw cruiser vertically
if (direction[0] == "v") {
var c;
for (c = y; c < (y + 4); c++)
{
game[c][x] = '#';
}
}
board.innerHTML = drawBoard(); // Redraw board with cruiser added
// Function for firing a shot when the "Fire! button is pressed
function fire() {
var fireX=prompt("Where would you like to fire? Enter an X coordinate: (0-8)");
var fireY=prompt("Where would you like to fire? Enter a Y coordinate: (0-8)");
fireX = Number(fireX);
// Convert the string returned by "prompt" into a number
fireY = Number(fireY);
// Convert the string returned by "prompt" into a number
if (game[fireY][fireX] == ".") {
// Check if the specified coordinate is occupied by the cruiser
alert("You Missed.");
}
else if (game[fireY][fireX] == "*") {
alert("You already hit the ship there.");
} else {
alert("Kaboom! You hit a ship");
game[fireY][fireX] = "*";
board.innerHTML = drawBoard();
// Redraw board with hit marker at specified coordinate
}
var shipfound;
var i;
var j;
// Check if there are any ships remaining on the board
for (i=0; i<9; i++) {
for (j=0; j<9; j++) {
if (game[i][j] != "." && game[i][j] != "*") {
shipfound = true;
// Set to true if a ship is found
}
}
}
if (!shipfound) {
// If no ships are found end the game
alert("All ships have been sunk. Well done Captain! Game over");
document.body.removeChild(button);
// Remove the fire button from the page after game over
}
}