2024-01-05 23:35:11 +01:00
|
|
|
const mapSize = 10;
|
|
|
|
|
|
|
|
const bsc = new Battleships(mapSize);
|
|
|
|
|
|
|
|
let board = bsc.generateDOMBoard(mapSize);
|
|
|
|
$("#board").html(board);
|
|
|
|
$("#secondaryBoard").html(board);
|
|
|
|
|
|
|
|
var previousRow = $(":not(*)");
|
|
|
|
var previousColumn = $(":not(*)");
|
|
|
|
var selectedShip = 0;
|
|
|
|
var shipRotation = 0;
|
2024-03-03 16:55:38 +01:00
|
|
|
var shipsLeft = [4, 3, 2, 1];
|
2024-01-05 23:35:11 +01:00
|
|
|
var changedFields = [];
|
|
|
|
|
2024-03-04 20:34:45 +01:00
|
|
|
var postPrep = false;
|
|
|
|
var myTurn = false;
|
|
|
|
|
2024-01-05 23:35:11 +01:00
|
|
|
var hoveredField = null;
|
|
|
|
|
2024-03-03 16:55:38 +01:00
|
|
|
refreshBoardView();
|
|
|
|
|
2024-03-05 21:34:16 +01:00
|
|
|
$(".board .field").hover(function () {
|
2024-01-05 23:35:11 +01:00
|
|
|
hoveredField = this;
|
|
|
|
// Pokaż "miarki"
|
2024-03-03 22:59:52 +01:00
|
|
|
let posX = parseInt($(this).data("pos-x"));
|
|
|
|
let posY = parseInt($(this).data("pos-y"));
|
2024-01-05 23:35:11 +01:00
|
|
|
let row = bsc.getRow(posY);
|
|
|
|
let column = bsc.getColumn(posX);
|
|
|
|
|
|
|
|
changedFields.push(row, column, $(this));
|
|
|
|
|
2024-03-03 16:55:38 +01:00
|
|
|
row.css("background", "var(--mark-line)");
|
|
|
|
column.css("background", "var(--mark-line)");
|
2024-01-05 23:35:11 +01:00
|
|
|
|
|
|
|
previousRow = row;
|
|
|
|
previousColumn = column;
|
|
|
|
|
2024-03-04 20:34:45 +01:00
|
|
|
if (postPrep) {
|
|
|
|
if (myTurn) {
|
|
|
|
$(this).css("background", "var(--mark-ship-invalid)");
|
|
|
|
} else {
|
|
|
|
(this).css("background", "var(--mark-spot)");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$(this).css("background", "var(--mark-spot)");
|
|
|
|
|
|
|
|
// Pokaż podgląd statku
|
|
|
|
|
|
|
|
var fields = [];
|
|
|
|
|
|
|
|
switch (shipRotation) {
|
|
|
|
case 0:
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
fields.push([posX + i, posY]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
fields.push([posX, posY + i]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
fields.push([posX - i, posY]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
fields.push([posX, posY - i]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
var fieldElem;
|
|
|
|
let failed = false;
|
|
|
|
for (let i = 0; i <= selectedShip; i++) {
|
|
|
|
const field = fields[i];
|
|
|
|
|
|
|
|
try {
|
|
|
|
fieldElem = bsc.getField(field[0], field[1]);
|
|
|
|
} catch {
|
|
|
|
if (!failed) {
|
|
|
|
failed = true;
|
|
|
|
i = -1;
|
|
|
|
}
|
2024-01-05 23:35:11 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 20:34:45 +01:00
|
|
|
if (failed) {
|
|
|
|
fieldElem.css("background", "var(--mark-ship-invalid)");
|
|
|
|
} else {
|
|
|
|
fieldElem.css("background", "var(--mark-ship-valid)");
|
2024-01-05 23:35:11 +01:00
|
|
|
}
|
2024-03-04 20:34:45 +01:00
|
|
|
changedFields.push(fieldElem);
|
2024-01-05 23:35:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, function () {
|
|
|
|
hoveredField = null;
|
|
|
|
// Wyłącz "miarki" po wyjściu kursora z pola (aby się nie duplikowały w przyszłości)
|
|
|
|
changedFields.forEach(field => {
|
2024-03-03 16:55:38 +01:00
|
|
|
field.css("background", "var(--field)");
|
2024-01-05 23:35:11 +01:00
|
|
|
});
|
|
|
|
changedFields.length = 0;
|
|
|
|
});
|
|
|
|
|
2024-03-05 21:34:16 +01:00
|
|
|
// $(".board .field").on("click", function() {
|
2024-01-05 23:35:11 +01:00
|
|
|
|
2024-03-05 21:34:16 +01:00
|
|
|
// });
|
2024-01-05 23:35:11 +01:00
|
|
|
|
|
|
|
var ownBoardIsActive = true;
|
|
|
|
|
2024-03-05 21:34:16 +01:00
|
|
|
$("#board").removeClass("secondary");
|
|
|
|
$("#secondaryBoard").addClass("secondary");
|
|
|
|
$(".ownBoardInfo").css("opacity", 1);
|
|
|
|
$(".controlsOwnBoard").css("opacity", 1);
|
|
|
|
|
2024-01-05 23:35:11 +01:00
|
|
|
function switchBoards() {
|
2024-03-05 21:34:16 +01:00
|
|
|
if (postPrep) {
|
|
|
|
if (ownBoardIsActive) { // Aktywna jest plansza użytkownika
|
|
|
|
$("#secondaryBoard").removeClass("secondary");
|
|
|
|
$("#board").addClass("secondary");
|
|
|
|
$(".ownBoardInfo").css("opacity", 0);
|
|
|
|
$(".controlsOwnBoard").css("opacity", 0.3);
|
|
|
|
} else { // Aktywna jest plansza przeciwnika
|
|
|
|
$("#board").removeClass("secondary");
|
|
|
|
$("#secondaryBoard").addClass("secondary");
|
|
|
|
$(".ownBoardInfo").css("opacity", 1);
|
|
|
|
$(".controlsOwnBoard").css("opacity", 1);
|
|
|
|
}
|
2024-01-05 23:35:11 +01:00
|
|
|
|
2024-03-05 21:34:16 +01:00
|
|
|
ownBoardIsActive = !ownBoardIsActive;
|
|
|
|
}
|
|
|
|
|
2024-01-05 23:35:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function switchShips() {
|
|
|
|
if (selectedShip===3) {
|
|
|
|
selectedShip = 0;
|
|
|
|
} else {
|
|
|
|
selectedShip++;
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshBoardView();
|
|
|
|
|
|
|
|
$("#selectedShip").addClass("changing");
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
switch (selectedShip) {
|
|
|
|
case 0:
|
|
|
|
$("#selectedShip").html("Jednomasztowiec");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
$("#selectedShip").html("Dwumasztowiec");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
$("#selectedShip").html("Trójmasztowiec");
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
$("#selectedShip").html("Czteromasztowiec");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$("#selectedShip").removeClass("changing");
|
|
|
|
}, 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
function switchRotation() {
|
|
|
|
if (shipRotation === 3) {
|
|
|
|
shipRotation = 0;
|
|
|
|
} else {
|
|
|
|
shipRotation++;
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshBoardView();
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshBoardView() {
|
2024-03-03 16:55:38 +01:00
|
|
|
let shipsOfType = shipsLeft[selectedShip];
|
|
|
|
$("#shipsLeft").html(shipsOfType);
|
|
|
|
if (!shipsOfType) {
|
|
|
|
$("#shipsLeft").addClass("danger");
|
|
|
|
} else {
|
|
|
|
$("#shipsLeft").removeClass("danger");
|
|
|
|
}
|
|
|
|
|
2024-01-05 23:35:11 +01:00
|
|
|
if (hoveredField) {
|
|
|
|
changedFields.forEach(field => {
|
2024-03-03 16:55:38 +01:00
|
|
|
field.css("background", "var(--field)");
|
2024-01-05 23:35:11 +01:00
|
|
|
});
|
|
|
|
changedFields.length = 0;
|
|
|
|
|
2024-03-03 22:59:52 +01:00
|
|
|
let posX = parseInt($(hoveredField).data("pos-x"));
|
|
|
|
let posY = parseInt($(hoveredField).data("pos-y"));
|
2024-01-05 23:35:11 +01:00
|
|
|
let row = bsc.getRow(posY);
|
|
|
|
let column = bsc.getColumn(posX);
|
|
|
|
|
|
|
|
changedFields.push(row, column, $(hoveredField));
|
|
|
|
|
2024-03-03 16:55:38 +01:00
|
|
|
row.css("background", "var(--mark-line)");
|
|
|
|
column.css("background", "var(--mark-line)");
|
2024-01-05 23:35:11 +01:00
|
|
|
|
2024-03-03 16:55:38 +01:00
|
|
|
$(hoveredField).css("background", "var(--mark-field)");
|
2024-01-05 23:35:11 +01:00
|
|
|
|
|
|
|
previousRow = row;
|
|
|
|
previousColumn = column;
|
|
|
|
|
|
|
|
var fields = [];
|
|
|
|
switch (shipRotation) {
|
|
|
|
case 0:
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
fields.push([posX + i, posY]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
fields.push([posX, posY + i]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
fields.push([posX - i, posY]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
fields.push([posX, posY - i]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
var fieldElem;
|
|
|
|
let failed = false;
|
2024-03-03 16:55:38 +01:00
|
|
|
for (let i = 0; i <= selectedShip; i++) {
|
2024-01-05 23:35:11 +01:00
|
|
|
const field = fields[i];
|
|
|
|
|
|
|
|
try {
|
|
|
|
fieldElem = bsc.getField(field[0], field[1]);
|
|
|
|
} catch {
|
|
|
|
if (!failed) {
|
|
|
|
failed = true;
|
|
|
|
i = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failed) {
|
2024-03-03 16:55:38 +01:00
|
|
|
fieldElem.css("background", "var(--mark-ship-invalid)");
|
2024-01-05 23:35:11 +01:00
|
|
|
} else {
|
2024-03-03 16:55:38 +01:00
|
|
|
fieldElem.css("background", "var(--mark-ship-valid)");
|
2024-01-05 23:35:11 +01:00
|
|
|
}
|
|
|
|
changedFields.push(fieldElem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|