Oto aplikacja webowa, która generuje i rysuje labirynt w formacie A4:
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Generator Labiryntu A4</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f0f0f0;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
text-align: center;
color: #333;
}
.controls {
margin: 20px 0;
display: flex;
gap: 10px;
flex-wrap: wrap;
justify-content: center;
}
button {
padding: 10px 20px;
font-size: 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
input[type="number"] {
padding: 8px;
font-size: 16px;
width: 80px;
border: 1px solid #ddd;
border-radius: 5px;
}
label {
display: flex;
align-items: center;
gap: 5px;
}
#mazeCanvas {
display: block;
margin: 20px auto;
border: 1px solid #ddd;
}
@media print {
body {
margin: 0;
background: white;
}
.container {
box-shadow: none;
padding: 0;
}
.controls, h1 {
display: none;
}
#mazeCanvas {
border: none;
width: 210mm !important;
height: 297mm !important;
max-width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Generator Labiryntu A4</h1>
<div class="controls">
<label>
Szerokość:
<input type="number" id="widthInput" value="30" min="10" max="100">
</label>
<label>
Wysokość:
<input type="number" id="heightInput" value="40" min="10" max="100">
</label>
<button onclick="generateMaze()">Generuj nowy labirynt</button>
<button onclick="window.print()">Drukuj</button>
</div>
<canvas id="mazeCanvas"></canvas>
</div>
<script>
const canvas = document.getElementById('mazeCanvas');
const ctx = canvas.getContext('2d');
// Ustawienia dla formatu A4 (210mm x 297mm)
// Przy 96 DPI: 794 x 1123 pikseli
const A4_WIDTH = 794;
const A4_HEIGHT = 1123;
canvas.width = A4_WIDTH;
canvas.height = A4_HEIGHT;
class Cell {
constructor(x, y) {
this.x = x;
this.y = y;
this.visited = false;
this.walls = {
top: true,
right: true,
bottom: true,
left: true
};
}
}
class Maze {
constructor(cols, rows) {
this.cols = cols;
this.rows = rows;
this.grid = [];
this.stack = [];
// Inicjalizacja siatki
for (let y = 0; y < rows; y++) {
let row = [];
for (let x = 0; x < cols; x++) {
row.push(new Cell(x, y));
}
this.grid.push(row);
}
// Rozpocznij od lewego górnego rogu
this.current = this.grid[0][0];
this.current.visited = true;
// Generuj labirynt
this.generate();
}
getNeighbors(cell) {
const neighbors = [];
const { x, y } = cell;
// Góra
if (y > 0) neighbors.push(this.grid[y - 1][x]);
// Prawo
if (x < this.cols - 1) neighbors.push(this.grid[y][x + 1]);
// Dół
if (y < this.rows - 1) neighbors.push(this.grid[y + 1][x]);
// Lewo
if (x > 0) neighbors.push(this.grid[y][x - 1]);
return neighbors.filter(neighbor => !neighbor.visited);
}
removeWalls(current, next) {
const dx = current.x - next.x;
const dy = current.y - next.y;
if (dx === 1) {
current.walls.left = false;
next.walls.right = false;
} else if (dx === -1) {
current.walls.right = false;
next.walls.left = false;
}
if (dy === 1) {
current.walls.top = false;
next.walls.bottom = false;
} else if (dy === -1) {
current.walls.bottom = false;
next.walls.top = false;
}
}
generate() {
while (true) {
const neighbors = this.getNeighbors(this.current);
if (neighbors.length > 0) {
const next = neighbors[Math.floor(Math.random() * neighbors.length)];
this.stack.push(this.current);
this.removeWalls(this.current, next);
this.current = next;
this.current.visited = true;
} else if (this.stack.length > 0) {
this.current = this.stack.pop();
} else {
break;
}
}
// Utwórz wejście i wyjście
this.grid[0][0].walls.left = false;
this.grid[this.rows - 1][this.cols - 1].walls.right = false;
}
draw(ctx, width, height) {
const margin = 40;
const mazeWidth = width - 2 * margin;
const mazeHeight = height - 2 * margin;
const cellWidth = mazeWidth / this.cols;
const cellHeight = mazeHeight / this.rows;
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, width, height);
ctx.strokeStyle = 'black';
ctx.lineWidth = 2;
for (let y = 0; y < this.rows; y++) {
for (let x = 0; x < this.cols; x++) {
const cell = this.grid[y][x];
const px = margin + x * cellWidth;
const py = margin + y * cellHeight;
ctx.beginPath();
if (cell.walls.top) {
ctx.moveTo(px, py);
ctx.lineTo(px + cellWidth, py);
}
if (cell.walls.right) {
ctx.moveTo(px + cellWidth, py);
ctx.lineTo(px + cellWidth, py + cellHeight);
}
if (cell.walls.bottom) {
ctx.moveTo(px, py + cellHeight);
ctx.lineTo(px + cellWidth, py + cellHeight);
}
if (cell.walls.left) {
ctx.moveTo(px, py);
ctx.lineTo(px, py + cellHeight);
}
ctx.stroke();
}
}
// Oznacz start i koniec
ctx.fillStyle = '#4CAF50';
ctx.font = '16px Arial';
ctx.fillText('START', margin + 5, margin - 10);
ctx.fillStyle = '#f44336';
ctx.fillText('META', width - margin - 45, height - margin + 25);
}
}
function generateMaze() {
const width = parseInt(document.getElementById('widthInput').value);
const height = parseInt(document.getElementById('heightInput').value);
const maze = new Maze(width, height);
maze.draw(ctx, A4_WIDTH, A4_HEIGHT);
}
// Generuj początkowy labirynt
generateMaze();
</script>
</body>
</html>
Ta aplikacja oferuje następujące funkcjonalności:
- Generowanie labiryntu - używa algorytmu recursive backtracking do tworzenia labiryntów
- Format A4 - canvas jest ustawiony na proporcje A4 (210mm x 297mm)
- Konfigurowalne wymiary - możesz zmienić liczbę kolumn i wierszy labiryntu
- Drukowanie - przycisk "Drukuj" pozwala wydrukować labirynt na papierze A4
- Start i Meta - oznaczone są punkty początkowy i końcowy labiryntu
Aby użyć aplikacji:
- Zapisz kod jako plik HTML (np.
labirynt.html)
- Otwórz go w przeglądarce
- Dostosuj wymiary labiryntu według potrzeb
- Kliknij "Generuj nowy labirynt" aby stworzyć nowy
- Kliknij "Drukuj" aby wydrukować na papierze A4
Aplikacja automatycznie dopasowuje labirynt do formatu A4 przy drukowaniu, ukrywając elementy interfejsu i zachowując tylko sam labirynt.