Battleship/index.js

117 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-01-16 10:52:50 +01:00
//my personal variables
let w = 10;
let h = 10;
let lgrid = new Array(w);
let rgrid = new Array(w);
2022-01-16 13:56:53 +01:00
let waux = 10;
let haux = 10;
let x = "✔";
let o = "⬤";
2022-01-16 17:46:16 +01:00
let randomize = true;
2022-01-16 13:56:53 +01:00
let lauxgrid = new Array(w);
let rauxgrid = new Array(w);
2022-01-16 10:52:50 +01:00
//Write arrays to html
2022-01-16 11:29:28 +01:00
function writegrid() {
for (let i = 0; i < w; i++) {
for (let j = 0; j < h; j++) {
2022-01-16 10:52:50 +01:00
$('#lc' + i + '-' + j).html(lgrid[i][j]);
$('#rc' + i + '-' + j).html(rgrid[i][j]);
}
}
}
2022-01-16 13:56:53 +01:00
function writeauxgrid(){
for (let i = 0; i < waux; i++) {
for (let j = 0; j < haux; j++) {
$('#lac' + i + '-' + j).html(lauxgrid[i][j]);
$('#rac' + i + '-' + j).html(rauxgrid[i][j]);
}
}
}
2022-01-16 10:52:50 +01:00
2022-01-16 13:56:53 +01:00
function inittable(){
2022-01-16 10:52:50 +01:00
for (let i = 0; i < w; i++) {
//create rows
2022-01-16 10:52:50 +01:00
$('#local').append('<tr class="column" id="lt' + i + '"></tr>');
$('#remote').append('<tr class="column" id="rt' + i + '"></tr>');
for (let j = 0; j < h; j++) {
//create cells
$('#lt' + i).append('<td class="cellx localcellx" id="lcx' + i + '-' + j + '"></td>');
$('#rt' + i).append('<td class="cellx remotecellx" id="rcx' + i + '-' + j + '"></td>');
2022-01-16 10:52:50 +01:00
//add divs to cells
$('#lcx' + i + '-' + j).append('<div class="cell localcell" id="lc' + i + '-' + j + '"></div>');
$('#rcx' + i + '-' + j).append('<div class="cell remotecell" id="rc' + i + '-' + j + '"></div>');
2022-01-16 10:52:50 +01:00
//add arrays to grids
lgrid[i] = new Array(h);
rgrid[i] = new Array(h);
}
}
2022-01-16 13:56:53 +01:00
}
function initauxtable(){
for (let i = 0; i < waux; i++) {
//create rows
2022-01-16 13:56:53 +01:00
$('#localaux').append('<tr class="column" id="lax' + i + '"></tr>');
$('#remoteaux').append('<tr class="column" id="rax' + i + '"></tr>');
for (let j = 0; j < haux; j++) {
//create cells
$('#lax' + i).append('<td class="cellx localauxcellx" id="lacx' + i + '-' + j + '"></td>');
$('#rax' + i).append('<td class="cellx remoteauxcellx" id="racx' + i + '-' + j + '"></td>');
2022-01-16 13:56:53 +01:00
//add divs to cells
$('#lacx' + i + '-' + j).append('<div class="cell localauxcell" id="lac' + i + '-' + j + '"></div>');
$('#racx' + i + '-' + j).append('<div class="cell remoteauxcell" id="rac' + i + '-' + j + '"></div>');
2022-01-16 13:56:53 +01:00
//add arrays to grids
lauxgrid[i] = new Array(haux);
rauxgrid[i] = new Array(haux);
}
}
}
function randomizetable(){
2022-01-16 11:29:28 +01:00
for (let i = 0; i < w; i++) {
for (let j = 0; j < h; j++) {
2022-01-16 13:56:53 +01:00
let r = Math.floor(Math.random() * 2);
if (r == 0) {
2022-01-16 10:52:50 +01:00
lgrid[i][j] = x;
rgrid[i][j] = o;
} else {
lgrid[i][j] = o;
rgrid[i][j] = x;
}
}
}
2022-01-16 13:56:53 +01:00
}
function randomizeaux(){
for (let i = 0; i < waux; i++) {
for (let j = 0; j < haux; j++) {
let r = Math.floor(Math.random() * 2);
if (r == 0) {
lauxgrid[i][j] = x;
rauxgrid[i][j] = o;
} else {
lauxgrid[i][j] = o;
rauxgrid[i][j] = x;
}
}
}
}
2022-01-16 18:51:14 +01:00
function randomizeall(){
randomizetable();
writegrid();
randomizeaux();
writeauxgrid();
}
2022-01-16 13:56:53 +01:00
function main() {
inittable();
initauxtable();
2022-01-16 17:46:16 +01:00
if (randomize) {
2022-01-16 19:23:46 +01:00
setInterval(randomizeall, 500);
2022-01-16 17:46:16 +01:00
}
2022-01-16 13:56:53 +01:00
}
$(function () {
main();
2022-01-16 10:52:50 +01:00
});