1eres corrections algo
This commit is contained in:
@@ -281,6 +281,219 @@ class Turtle {
|
||||
|
||||
}
|
||||
|
||||
const Orientation = Object.freeze({
|
||||
HAUT: 0,
|
||||
DROITE: 1,
|
||||
BAS: 2,
|
||||
GAUCHE: 3,
|
||||
});
|
||||
|
||||
|
||||
class Noeud {
|
||||
coordX;
|
||||
coordY;
|
||||
chemins;
|
||||
|
||||
constructor(coordX, coordY){
|
||||
this.coordX = coordX;
|
||||
this.coordY = coordY;
|
||||
chemins = new Array();
|
||||
}
|
||||
|
||||
addCheminPossible(chemin){
|
||||
this.chemins.push(chemin);
|
||||
}
|
||||
|
||||
getCheminsPossibles(){
|
||||
return this.chemins;
|
||||
}
|
||||
|
||||
toString(){
|
||||
console.log("node : (" + currentNode.getX + "; " + currentNode.getY + ")");
|
||||
}
|
||||
|
||||
toStringWithVoisins(){
|
||||
console.log("node : (" + currentNode.getX + "; " + currentNode.getY + ")");
|
||||
console.log("voisins connus : " + this.chemins.forEach((chemin) => chemin.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class ModeAutomatique {
|
||||
|
||||
cheminsPossibles = new Array(); // liste des listes de noeuds
|
||||
noeudsVisites = new Array();
|
||||
|
||||
// valeurs pour la première position
|
||||
posX=0;
|
||||
posY=0;
|
||||
rotation = Orientation.HAUT;
|
||||
lastRotation = this.rotation;
|
||||
|
||||
run(){
|
||||
let iter = 0;
|
||||
while(!this.isObjectifTrouve()){
|
||||
console.log("Iteration " + iter++);
|
||||
console.log(this.cheminsPossibles);
|
||||
const currentNode = this.getCaseAnalysee();
|
||||
this.analyseEnvironnement(currentNode); // met à jour ses connaissances (à dessiner au fur et à mesure ?)
|
||||
this.deplacer(currentNode); // récupère un chemin non encore parcourus en fonction de ses connaissances
|
||||
sleep(5000);
|
||||
}
|
||||
}
|
||||
|
||||
isObjectifTrouve(){
|
||||
return false;
|
||||
}
|
||||
|
||||
//////////////////////////////////////// ANALYSE ENVIRONNEMENT
|
||||
|
||||
isObstacleDevant (currentNode, rotation) {
|
||||
return false;
|
||||
}
|
||||
|
||||
analyseEnvironnement(currentNode){
|
||||
// SI case déjà visitée, alors on connait ses obstacles
|
||||
if (currentNode == null) {
|
||||
|
||||
// pour les 4 directions possibles
|
||||
for (let i = 0; i < 4; i++) {
|
||||
if (!this.isObstacleDevant(currentNode, this.rotation)){
|
||||
this.addCheminPossible(currentNode);
|
||||
}
|
||||
this.tourne(); // changement de l'angle de 90 degrés
|
||||
}
|
||||
this.noeudsVisites.push(currentNode);
|
||||
}
|
||||
}
|
||||
|
||||
tourne(){
|
||||
turtle.tourner(-90);
|
||||
this.rotation++;
|
||||
this.rotation %= 4;
|
||||
}
|
||||
|
||||
addCheminPossible(){
|
||||
this.chemin = new Array();
|
||||
if(this.rotation = Orientation.HAUT){
|
||||
this.chemin.push(new Noeud(posX+1, posY));
|
||||
} else if(this.rotation = Orientation.BAS){
|
||||
this.chemin.push(new Noeud(posX-1, posY));
|
||||
}else if(this.rotation = Orientation.GAUCHE){
|
||||
this.chemin.push(new Noeud(posX, posY-1));
|
||||
}else if(this.rotation = Orientation.DROITE){
|
||||
this.chemin.push(new Noeud(posX, posY+1));
|
||||
}
|
||||
this.cheminsPossibles.push(this.chemin);
|
||||
}
|
||||
|
||||
//////////////////////////////////////// MOVE
|
||||
|
||||
deplacer(currentNode){
|
||||
// on choisi le prochain noeud
|
||||
const nextNode = this.getNextNode();
|
||||
if (nextNode != null) {
|
||||
console.log("next node : ");
|
||||
nextNode.toString();
|
||||
}
|
||||
// on met à jour la liste des chemins en ajoutant le noeud courant dans les chemins non choisis
|
||||
this.updatePaths(nextNode);
|
||||
// on met à jour les coordonnées
|
||||
this.updateCoordonnees(currentNode, nextNode);
|
||||
// move
|
||||
this.move();
|
||||
}
|
||||
|
||||
// parcours en profondeur : il s'agit de LIFOs
|
||||
getNextNode(){
|
||||
if (this.cheminsPossibles[this.cheminsPossibles.length - 1] > 0) {
|
||||
// on récupère la dernière liste
|
||||
const cheminChoisi = this.cheminsPossibles[this.cheminsPossibles.length - 1];
|
||||
// on récupère le dernier élément de cette liste
|
||||
const nodeChoisi = cheminChoisi[cheminChoisi.length - 1];
|
||||
console.log("noeud choisi :");
|
||||
nodeChoisi.toString();
|
||||
// on créé un nouveau noeud
|
||||
return new Noeud(nodeChoisi.posX, nodeChoisi.posY);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
updatePaths(nextNode){
|
||||
nextNode = this.getCaseAnalysee();
|
||||
// on ajoute le noeud visité à toutes les listes sauf la dernière
|
||||
for (let i=0; i < this.cheminsPossibles -1; i++){
|
||||
const cheminPossible = this.cheminsPossibles[i];
|
||||
if (nextNode == null){
|
||||
// on ajoute le noeud en cours à la liste des noeuds visités
|
||||
cheminPossible.push(nextNode);
|
||||
} else {
|
||||
// on vire le noeud visité de toutes les listes s'il a déjà été visité avant (on fait demi tour)
|
||||
cheminPossible.pop();
|
||||
}
|
||||
}
|
||||
|
||||
// on supprime les listes vides
|
||||
this.cheminsPossibles = this.cheminsPossibles.filter(function (el) {
|
||||
return el.length > 0;
|
||||
});
|
||||
console.log("nombre de chemins restants : " + this.cheminsPossibles.length)
|
||||
console.log("chemins possibles : " + this.cheminsPossibles)
|
||||
}
|
||||
|
||||
updateCoordonnees(currentNode, nextNode) {
|
||||
console.log("coordonnees : (" + this.posX + "; " + this.posY + ")");
|
||||
this.lastRotation = this.rotation;
|
||||
if (currentNode.getX > nextNode.getX){
|
||||
this.rotation = Orientation.GAUCHE
|
||||
this.posY--;
|
||||
} else if (currentNode.getX < nextNode.getX){
|
||||
this.rotation = Orientation.DROITE
|
||||
this.pos++;
|
||||
} else if (currentNode.getY > nextNode.getY){
|
||||
this.rotation = Orientation.BAS
|
||||
this.posX--;
|
||||
} else {
|
||||
this.rotation = Orientation.HAUT
|
||||
this.posX++;
|
||||
}
|
||||
console.log("new coordonnees : (" + this.posX + "; " + this.posY + ")");
|
||||
}
|
||||
|
||||
move(){
|
||||
// déplacer robot (les coordonnées sont déjà à jour)
|
||||
let aRotater = this.rotation - this.lastRotation;
|
||||
if (aRotater > 2) {
|
||||
aRotater -= 2;
|
||||
}
|
||||
if (aRotater < 2) {
|
||||
aRotater += 2;
|
||||
}
|
||||
aRotater *= 90;
|
||||
turtle.tourner(aRotater);
|
||||
turtle.avancer(100);
|
||||
}
|
||||
|
||||
/////////////////////////////////////// GETTERS
|
||||
|
||||
getCaseAnalysee(){
|
||||
let index = 0;
|
||||
this.noeudsVisites.forEach((noeud) => {
|
||||
if (noeud == null) {
|
||||
console.error(this.noeudsVisites);
|
||||
console.error("INDEX NULL : " + index);
|
||||
}
|
||||
index++;
|
||||
if (noeud.posX == this.posX && noeud.posY == this.posY){
|
||||
return noeud;
|
||||
}
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
async function run() {
|
||||
connectSockets();
|
||||
@@ -336,3 +549,5 @@ async function test() {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user