Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Bonus/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="uk">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>П'ятнашки</title>
<link rel="stylesheet" href="styles.css">
<link href="https://cdn.webdatarocks.com/latest/webdatarocks.min.css" rel="stylesheet"/>
<script src="https://cdn.webdatarocks.com/latest/webdatarocks.toolbar.min.js"></script>
<script src="https://cdn.webdatarocks.com/latest/webdatarocks.js"></script>
</head>
<body>
<header>Кількість ходів: <span>0</span></header>
<div id="game-wrapper">
<div id="pivotContainer"></div>
</div>
</body>
<script src="script.js"></script>
</html>
148 changes: 148 additions & 0 deletions Bonus/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
const moveCounter = document.querySelector("span");

let board = [];

for (let i = 0; i < 4; i++) {
for (let j = 0; j < 4; j++) {
board.push({"row": `${i}`, "column": `${j}`, "value": (i*4) + j + 1},)
}
}
board[15].value = null;

function getTableData() {
return [
{
"row": { "type": "string" },
"column": { "type": "string" },
"value": { "type": "number" }
},
...board
];
}

function getNeighborsIndices(cellIndex) {
let validIndices = [];

if (cellIndex % 4 !== 0) validIndices.push(cellIndex - 1);
if ((cellIndex + 1) % 4 !== 0) validIndices.push(cellIndex + 1);
if (cellIndex >= 4) validIndices.push(cellIndex - 4);
if (cellIndex <= 11) validIndices.push(cellIndex + 4);

return validIndices;
}

function swapCells(firstCellIndex, secondCellIndex) {
const cellA = board[firstCellIndex];
const cellB = board[secondCellIndex];

[cellA.value, cellB.value] = [cellB.value, cellA.value];
}

function randomizeGameBoard() {
let currentCell = 15;

for (let i = 0; i < 100; i++) {
const neighbors = getNeighborsIndices(currentCell);
const randomNeighbor = Math.floor(Math.random() * (neighbors.length));

console.log(`Обміняно з: ${board[neighbors[randomNeighbor]].value}`);
swapCells(currentCell, neighbors[randomNeighbor]);

currentCell = neighbors[randomNeighbor];
}
}

function checkWin() {
for (let i = 0; i < 15; i++) {
if (board[i].value !== i+1) return false;
}

return true;
}

randomizeGameBoard();

const pivot = new WebDataRocks({
container: "#pivotContainer",
toolbar: false,
report: {
dataSource: {
data: getTableData()
},
slice: {
rows: [{uniqueName: "row"}],
columns: [
{uniqueName: "column"},
{uniqueName: "Measures"}
],
measures: [{uniqueName: "value", aggregation: "sum"}]
},
options: {
grid: {
showHeaders: false,
showHierarchyCaptions: false,
showTotals: "off",
showGrandTotals: "off"
}
},
tableSizes: {
columns: [
{ idx: 0, width: 80 }, { idx: 1, width: 80 },
{ idx: 2, width: 80 }, { idx: 3, width: 80 }, { idx: 4, width: 80 }
],
rows: [
{ idx: 0, height: 80 }, { idx: 1, height: 80 },
{ idx: 2, height: 80 }, { idx: 3, height: 80 }, { idx: 4, height: 80 }
]
}
},
customizeCell: (builder, { type, value }) => {
if (type !== "value") return;

builder.text = value
? `<div class="value-cell">${value}</div>`
: `<div class="empty-cell"></div>`;
}
});

pivot.on("cellclick", (cellData) => {
if (!cellData.measure) return;

const cellRow = +(cellData.rows[0].caption);
const cellColumn = +(cellData.columns[0].caption);
const currentCellIndex = cellRow * 4 + cellColumn;

let neighbors = [];
const neighborsIndices = getNeighborsIndices(currentCellIndex);
neighborsIndices.map(neighborIndex => neighbors.push(board[neighborIndex]));

const freeNeighbor = neighbors.find(cell => !cell.value);

if (!freeNeighbor) return;
const freeNeighborIndex = (+freeNeighbor.row * 4) + (+freeNeighbor.column);

swapCells(currentCellIndex, freeNeighborIndex);
let currentReport = pivot.getReport();
currentReport.dataSource.data = getTableData();
pivot.setReport(currentReport);

let counterValue = +(moveCounter.textContent);
moveCounter.textContent = ++counterValue;

if (checkWin()) {
setTimeout(() => {
const counterValue = +(moveCounter.textContent);
let repeatGame = confirm(`Вітаю! Ви пройшли головоломку за ${counterValue} ходів! Бажаєте повторити?`);

if (repeatGame) {
moveCounter.textContent = 0;

randomizeGameBoard();

let currentReport = pivot.getReport();
currentReport.dataSource.data = getTableData();
pivot.setReport(currentReport);
}
}, 100);
}
});
66 changes: 66 additions & 0 deletions Bonus/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
* {
box-sizing: border-box;
}

#pivotContainer .wdr-cell {
padding: 0 !important;
border: 1px solid lightgray !important;
}

body {
font-family: sans-serif;
background-color: white;
display: flex;
flex-direction: column;
align-items: center;
padding-top: 50px;
gap: 20px;
}

header {
display: flex;
justify-content: center;
padding: 10px;
border: 1px solid lightblue;
width: 30%;
border-radius: 6px;
background-color: aliceblue;
}

#game-wrapper {
display: flex;
justify-content: center;
align-items: center;
width: 480px;
height: 504px;
border-radius: 4px;
overflow: hidden;
}

#pivotContainer {
width: 100%;
height: 100%;
}

.value-cell {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
width: 100%;
background-color: #3498db;
color: white;
font-size: 20px;
font-weight: bold;
cursor: pointer;
transition: background-color 0.2s ease;
}

.value-cell:hover {
background-color: #2980b9;
}

.empty-cell {
height: 100%;
width: 100%;
}
57 changes: 45 additions & 12 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
<!DOCTYPE html>
<html>
<head lang="uk">
<meta charset="UTF-8">
<title>My Page</title>
<html lang="uk">
<head>
<title>BuyList</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="main-cont">
<div class="secondary-cont" id="left-cont">

<div class="main-search-bar">
<input type="text" placeholder="Назва товару">
<button data-tooltip="Додати товар до списку">Додати</button>
</div>

<ul></ul>
</div>

<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<span class="product-item">
Apple
<span class="amount">4</span>
</span>
</body>
<div class="secondary-cont" id="right-cont">
<div class="prod-remained">
<h2>Залишилося</h2>
<div class="prod-list">
</div>
</div>

<div class="prod-bought">
<h2>Куплено</h2>
<div class="prod-list">
</div>
</div>
<div>
<button id="save-btn">Зберегти кошик</button>
</div>
</div>
</div>
<div class="badge">
<div class="badge-visible">
Buy List
</div>
<div class="badge-hidden">
Created by:
<span class="creator">Ihor Movchan</span>
</div>
</div>
<script src="main.js"></script>
</body>
</html>
17 changes: 0 additions & 17 deletions main.css

This file was deleted.

Loading