Hi, I’m coding in JavaScript in coder of carribeans challenge and it seems like there are some bugs with the print function. It doesn’t always print what it should, maybe I’m missing something but here is an example:
else if (toGo.x < ship.x && toGo.y > ship.y){
printErr('Im in this case', ship.sens);
switch(ship.sens){
case 0: printErr('Im I here?'); ship.speed !== 0 && toGo.dist===ship.speed ? print('SLOWER') :print('STARBOARD'); break;
case 1: ship.speed !== 0 && toGo.dist===ship.speed ? print('SLOWER') :print('PORT');break;
case 2: ship.speed !== 0 && toGo.dist===ship.speed ? print('SLOWER') :print('PORT');break;
case 3: ship.speed !== 0 && toGo.dist===ship.speed ? print('SLOWER') :print('PORT') ;break;
case 4: printErr('should print FASTER'); print('FASTER');break;
case 5: ship.speed !== 0 && toGo.dist===ship.speed ? print('SLOWER') :printErr('should print starboard');print('STARBOARD');break;
}
}
In this sample of code, I printErr what should be outputed, but as you will se in the logs, the print outputs something totally different:
speed 2
dist 2
Im in this case 4
should print FASTER
Standard Output Stream:
STARBOARD
Same thing in another case:
speed 1
dist 2
Im in this case 5
should print starboard
Standard Output Stream:
FASTER
Unfortunately I’m a new user so I can’t upload images. Does anybody knows what’s happening here?
case 5: condition ? print('SLOWER') :printErr('should print starboard');print('STARBOARD');break;
you do two prints for one turn. The second print will be counted for the next turn.
Also check if you print more in the same main loop.
thanks for your answer! But it is a ternary operator so I’m doing either print(‘SLOWER’) or print(‘STARBOARD’), not both…
press F12 to enter the browser developer mode, go to the console and write
true ? alert('SLOWER') :alert('should print starboard');alert('STARBOARD');
I did it. And I got two message boxes.
1 Like
However the bug remains since it doesn’t outputs ‘SLOWER’ either
As mention by euler, if your code prints several actions instead of one, they are stacked for the next turns.
The best way to avoid multiple print is to so something like
var action = 'MOVE 0 0'; // Default action
switch(blablabla)
[...]
case 5: action = condition ? 'SLOWER' : 'FASTER';
[...]
print(action);
I’ve did it using if else instead of ternary, the problem persists
Here the behaviour of the ship
CodinGame is a challenge-based training platform for programmers where you can play with the hottest programming topics. Solve games, code AI bots, learn from your peers, have fun.
and here the code sample of all the case I handle for steering (toGo is the position to reach and ship the current position of the ship, distance is well calculated taking into account the increment according the direction and the parity of ship.y)
if(toGo.x > ship.x && toGo.y === ship.y){
switch(ship.sens){
case 0: toPrint = 'FASTER'; break;
case 1: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD'; } break;
case 2: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD'; } break;
case 3: toPrint = 'STARBOARD';break;
case 4: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT'; } break;
case 5: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT'; } break;
}
} else if (toGo.x < ship.x && toGo.y === ship.y){
switch(ship.sens){
case 3: toPrint = 'FASTER'; break;
case 4: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD'; } break;
case 5: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD'; } break;
case 0: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD'; } break;
case 1: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT'; } break;
case 2: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT'; } break;
}
} else if (toGo.x >= ship.x && toGo.y < ship.y) {
switch(ship.sens){
case 0: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT'; } break;
case 1: toPrint = 'FASTER';break;
case 2: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD'; } break;
case 3: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD' ; } break;
case 4: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT'; } break;
case 5: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT'; } break;
}
} else if (toGo.x >= ship.x && toGo.y > ship.y){
switch(ship.sens){
case 0:if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD'; } break;
case 1: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD'; } break;
case 2: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD'; } break;
case 3: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT' ; } break;
case 4: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT'; } break;
case 5: toPrint = 'FASTER';break;
}
} else if (toGo.x < ship.x && toGo.y < ship.y){
switch(ship.sens){
case 0: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT'; } break;
case 1:if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT'; } break;
case 2: toPrint = 'FASTER';break;
case 3: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD' ; } break;
case 4: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD'; } break;
case 5: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD'; } break;
}
} else if (toGo.x < ship.x && toGo.y > ship.y){
switch(ship.sens){
case 0: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD'; } break;
case 1: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT'; } break;
case 2: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT'; } break;
case 3: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'PORT'; } break;
case 4: toPrint = 'FASTER';break;
case 5: if(ship.speed !== 0 && toGo.dist===ship.speed) { toPrint = 'SLOWER'; } else { toPrint = 'STARBOARD'; } break;
}
}
print(toPrint);