Newbie here!!! Just want to ask on how to insert random placement in this code and make a server on it…I include some jquery file for game instructions also but this is the main reason why I am asking for help. Problem: random placement, connect to AI opponent…or only the random placement.
// get the container element
var battletanksID = document.getElementById("battletanks");
// set rows and columns
var rows = 7;
var cols = 7;
// make the grid columns and rows
for (i = 0; i < cols; i++) {
for (j = 0; j < rows; j++) {
// create a new div HTML element for each grid box
var box = document.createElement("box");
battletanksID.appendChild(box);
// give each div element a unique id based on its row and column
box.id = 's' + j + i;
// set each grid box size. Multiply the size from the current row or column
var size = 50;
var topPosition = j * size;
var leftPosition = i * size;
box.style.top = topPosition + 'px';
box.style.left = leftPosition + 'px';
}
}
var missilecount = 10;
//tank placement
var placement = [
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,1,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,1,0,0,0,0],
[0,0,0,0,0,0,0]
]
//add an event listener that fires when a user clicks a square
battletanksID.addEventListener("click", missiles);
function missiles(m) {
// if item clicked (e.target) is not the parent element on which the event listener was set
if (m.target !== m.currentTarget) {
// extract row and column # from the HTML element's id
var row = m.target.id.substring(1,2);
var col = m.target.id.substring(2,3);
// if player clicks the box and missed hit
if (placement[row][col] == 0) {
m.target.style.background = '#e6e6e6';
// set box's value to 3 to indicate missed hit
placement[row][col] = 3;
//every missed hit it deduct missiles
missilecount--;
alert("Missed hit, Try again!");
}
// if player clicks the box and hit the enemies tank
if (placement[row][col] == 1) {
m.target.style.background = '#006600';
// set box's value to 2 to indicate hit target
placement[row][col] = 2;
//increment missiles each time tank is hit
missilecount++;
//if player hit the tank
if (missilecount == missilecount++) {
alert("Enemies tank is defeated!");
//redirect to Win page
location.replace("2.html");
}
}
//if missiles run out
if(missilecount == 0){
alert("game over!");
//redirect to Lose page
location.replace("3.html");
}
}
}
//Jquery
$(document).ready(function(){
//Hit
$("#btn2").click(function(){
$("#btn2").removeClass("btn2");
$(this).addClass("btn2");
});
//Missed Hit
$("#btn3").click(function(){
$("#btn3").removeClass("btn3");
$(this).addClass("btn3");
});
});