So, I code something in JS, and I don’t know why it doesn’t want to make a round of the railway when I put the train on the rail. Please I need help. I tried everything, like renaming the variable, changing the algorithm but nothing. Please see the demo on the image description:
function avancerTrains() {
// Parcourir le plateau pour trouver les trains et les faire avancer
for (let x = 0; x < plateau.largeur; x++) {
for (let y = 0; y < plateau.hauteur; y++) {
const currentCase = plateau.cases[x][y];
if (isTrain(currentCase)) {
moveTrain(x, y, currentCase);
}
}
}
function isTrain(caseInstance) {
return caseInstance instanceof Type_de_train;
}
function moveTrain(x, y, train) {
if (train === Type_de_train.Locomative_Seule) {
moveLocomotive(x, y);
} else if (isWagon(train)) {
moveWagon(x, y, train);
}
}
function isWagon(train) {
return [
Type_de_train.Wagon_A_Sa_Gauche,
Type_de_train.TroisWagon_A_Sa_Gauche,
Type_de_train.CinqWagon_A_Sa_Gauche,
].includes(train);
}
function moveLocomotive(x, y) {
if (canMoveRight(x, y) && isHorizontalRail(x + 1, y)) {
swapCases(x, y, x + 1, y, Type_de_case.Rail_horizontal, Type_de_train.Locomative_Seule);
}
}
function moveWagon(x, y, wagon) {
if (canMoveRight(x, y) && isHorizontalRail(x + 1, y)) {
swapCases(x, y, x + 1, y, Type_de_case.Rail_horizontal, wagon);
}
}
function canMoveRight(x, y) {
return x + 1 < plateau.largeur;
}
function isHorizontalRail(x, y) {
return plateau.cases[x][y] === Type_de_case.Rail_horizontal;
}
function swapCases(x1, y1, x2, y2, caseType1, caseType2) {
plateau.cases[x1][y1] = caseType1;
plateau.cases[x2][y2] = caseType2;
}
// Redessiner le plateau après avoir déplacé les trains
dessine_plateau(canvas.getContext("2d"), plateau);
}
New contributor
Dams Damoune is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3