Can someone give me a hand with this please ?
I dont know why but, my code wont work properly. When it should “block” it wont even if my “if” is true.
Here is my code, I know it’s not clean and optimized but I think it should work… I think
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int nbFloors = in.nextInt(); // number of floors
int width = in.nextInt(); // width of the area
int nbRounds = in.nextInt(); // maximum number of rounds
int exitFloor = in.nextInt(); // floor on which the exit is found
int exitPos = in.nextInt(); // position of the exit on its floor
int nbTotalClones = in.nextInt(); // number of generated clones
int nbAdditionalElevators = in.nextInt(); // ignore (always zero)
int nbElevators = in.nextInt(); // number of elevators
int[] refAsc = new int[nbElevators + 1]; // an array to save the exit and elevator position
for (int i = 0; i < nbElevators; i++) {
int elevatorFloor = in.nextInt(); // floor on which this elevator is found
int elevatorPos = in.nextInt(); // position of the elevator on its floor
refAsc[elevatorFloor]=elevatorPos;
}
System.err.println(nbElevators+ "nb elev");
System.err.println(Arrays.toString(refAsc));
int deplacement = 1;
int cible = exitPos;
int distanceT0; // distance at T=0
int distanceT1; // distance after the "move"
refAsc[exitFloor]=exitPos;
// game loop
while (true) {
int cloneFloor = in.nextInt(); // floor of the leading clone
int clonePos = in.nextInt(); // position of the leading clone on its floor
String direction = in.next(); // direction of the leading clone: LEFT or RIGHT
System.err.println(cloneFloor + " clonefloor");
if (direction.contains("R")){ // give an num valeur in fonction of the direction
deplacement = 1;
} else if (direction.contains("L")){
deplacement = -1;
}
System.err.println(deplacement + " deplacement");
if (cloneFloor >=0 ){
cible = refAsc[cloneFloor];
} else {
System.out.println("WAIT");
}
distanceT0 = Math.abs(cible - clonePos);
distanceT1 = Math.abs(cible - (clonePos + deplacement));
System.err.println(cible + " cible");
System.err.println(clonePos + " position clone");
System.err.println(deplacement + " depl");
System.err.println(distanceT0 + " dist0");
System.err.println(distanceT1 + " dist1");
System.err.println(cible + " cible");
if (distanceT0 == distanceT1){
System.out.println("WAIT");
}
else if (distanceT0 < distanceT1) {
System.err.println(" if of block");
System.out.println("BLOCK");
} else {
System.err.println(" le w8");
System.out.println("WAIT");
}
// Write an action using System.out.println()
// To debug: System.err.println("Debug messages...");
System.out.println("WAIT"); // action: WAIT or BLOCK
}
}
}
The problem is at this stage:
else if (distanceT0 < distanceT1) {
System.err.println(" if of block");
System.out.println(“BLOCK”);
It work on the first lvl but it goes wrong at the third lvl
It would help to debug if you read your code and walk through it manually
You should notice that you have Sytem.out.println in a few places: within the first if-block, within the second if-block, and outside the said if-blocks. In a single while-loop (i.e. a single turn), you’re supposed to output once only. But your code outputs more than one command in a single turn, so, the game engine treats them as commands for several turns, and the logic as a result goes out of sync with your original intention.
Quite sure I understand the game but i checked with the debug options and it is not synchronised the values that are taken from input with the game.
I got the “Warning: your code did not read all available input before printing an instruction, your outputs will not be synchronized with the game’s turns and unexpected behaviour may occur.” but i do not understand. I use the variables I need and the logic is quite simple.
Any idea of how to synchonised and avoid the warning?
The message means your code has either output too many lines or skipped output before reading the new round’s inputs. You are suggested to use the error stream to keep track of when you read new inputs and when you write outputs.
I see strange behaviour: in IDE all tests are passed but in submitting validation step last test - “Spaced elevators, few rounds” - is failed, and when I watch the animation of failing test I notice that leading clone does reach elevator at (last-1) floor but does not appear on last floor with exit - instead it becomes blocked.
hi good morning, im hving issues with the code, i got the first 5th rounds but th 6th and the7th shows this erros message “Max rounds count reached” and i used this code
int tope = width-1;
Tests and validators are slightly different. As can be seen in the replays you shared, the top elevator (on the floor below the exit) is located at a different position.
In the replay from Submit, can you see that the lead clone blocks at the top elevator? As a result, no clone is able to use it to go to the exit floor. That is where you want to further investigate and think about how to fix your code.
As can be seen in the replay, after travelling up the first elevator, the clones move right at first instead of moving left, and it takes many turns for them to return to the elevator on that floor. You have to fix your code so that the clones will always take the shortest route and won’t waste the turns.