Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Viet281101 committed Jun 1, 2024
2 parents 90eadf0 + 8ccd054 commit 8f25767
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
58 changes: 55 additions & 3 deletions 2D/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { GridBoard } from './board.js';
import { Polyomino, getRandomColor } from './polyomino.js';
import { GUIController } from './gui.js';
import { Toolbar } from './toolbar.js';
import { autoTiling } from './ai.js';

class MainApp {
constructor() {
Expand Down Expand Up @@ -154,9 +153,62 @@ class MainApp {
}
};

autoTiling() {
autoTiling(this.polyominoes, this.gridBoard, this.placePolyomino.bind(this), this.gridBoard.removePolyomino.bind(this));
//IA POUR TILING
autoTiling() {
const polyominoes = [...this.polyominoes]; // Clone des polyominos pour ne pas les modifier
polyominoes.sort((a, b) => b.shape.flat().reduce((acc, val) => acc + val) - a.shape.flat().reduce((acc, val) => acc + val)); // trie par taille
if (this.tryPlacePolyominoes(0, polyominoes)) {
console.log("Pavage Reussi!");
} else {
console.log("Pavage Echouer!");
}
};

tryPlacePolyominoes(index, polyominoes) {
if (index >= polyominoes.length) {
return true; // Tous les polyominos sont place
}

const polyomino = polyominoes[index];

for (let row = 0; row < this.gridBoard.rows; row++) {
for (let col = 0; col < this.gridBoard.cols; col++) {
const originalX = polyomino.x;
const originalY = polyomino.y;
polyomino.x = col * this.gridSize + this.gridBoard.gridOffsetX;
polyomino.y = row * this.gridSize + this.gridBoard.gridOffsetY;

if (this.gridBoard.isInBounds(polyomino) && !this.gridBoard.isOverlapping(polyomino)) {
this.placePolyomino(polyomino);
if (this.tryPlacePolyominoes(index + 1, polyominoes)) {
return true;
}
this.gridBoard.removePolyomino(polyomino);
}

polyomino.x = originalX;
polyomino.y = originalY;
}
}

return false;
};
};

const main_app = new MainApp();
window.onload = () => {
const iaButton = document.createElement('button');
iaButton.innerText = 'IA';
iaButton.id = 'ia-button';
iaButton.style.position = 'fixed';
iaButton.style.bottom = '10px';
iaButton.style.right = '10px';
iaButton.style.padding = '10px 20px';
iaButton.style.fontSize = '16px';
iaButton.style.backgroundColor = 'blue';
iaButton.style.color = 'white';
iaButton.style.border = 'none';
iaButton.style.borderRadius = '5px';
iaButton.addEventListener('click', () => main_app.autoTiling());
document.body.appendChild(iaButton);
};
4 changes: 3 additions & 1 deletion 2D/js/polyomino.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ export const SHAPES = {
TROMINO: [[1, 1, 1]],
TETROMINO_I: [[1, 1, 1, 1]],
TETROMINO_O: [[1, 1], [1, 1]],
TETROMINO_S: [[0, 1, 1], [1, 1, 0]],
TETROMINO_T: [[0, 1, 0], [1, 1, 1]],
TETROMINO_L: [[1, 0, 0], [1, 1, 1]],
TETROMINO_J: [ [1, 1, 1], [1, 0, 0]],
TETROMINO_S: [[0, 1, 1], [1, 1, 0]],
TETROMINO_Z: [[1, 1, 0], [0, 1, 1]],
};

0 comments on commit 8f25767

Please sign in to comment.