Code debbuging

I'm doing a project, the code is finished, i think, it runs and all, but some of the tests that i had to run with it are giving wrong outputs, and i honestly have no clue about what's wrong... could someone help me with it?
31 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @Yumi! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Yumi
YumiOP2mo ago
btw i'm using Java without classes yes, stupid university stuff
Madjosz
Madjosz2mo ago
Everything is a class in Java. Unless it's a primitive.
Yumi
YumiOP2mo ago
it's like a preview version of java without public / static...
ayylmao123xdd
ayylmao123xdd2mo ago
show the test and the code that its testing
Yumi
YumiOP2mo ago
output file is expected output
ayylmao123xdd
ayylmao123xdd2mo ago
and whats the current output
Yumi
YumiOP2mo ago
Player P: cell 4, laps 0! dcbaP.......... (ongoing) Player P: cell 5, laps 0! .dcbaP......... (ongoing) Player P: cell 6, laps 0! ..dcbaP........ (ongoing) Player P: cell 7, laps 0! ...dcbaP....... (ongoing) Player P: cell 9, laps 0! ....dcba.P..... (ongoing) Player P: cell 12, laps 0! ....Sdcba...P.. (ongoing) Player P: cell 1, laps 0! .P..S.dcba..... (ongoing) Player P: cell 5, laps 1! ....SP.dcba.... (ongoing) Player P: cell 9, laps 1! ....S..dcPba... (ongoing) Player P: cell 14, laps 1! ....S...dc.ba.P (ongoing) Player P won the race! ....P...dc.ba.. (ended) Race ended: P won the race! ....P...dc.ba.. (ended) Race ended: P won the race! ....P...dc.ba.. (ended) Race ended: P won the race! ....P...dc.ba.. (ended) Race ended: P won the race! ....P...dc.ba.. (ended) Race ended: P won the race! ....P...dc.ba.. (ended) Race ended: P won the race! ....P...dc.ba.. (ended) Race ended: P won the race! Race ended: P won the race! Race ended: P won the race! Race ended: P won the race! Race ended: P won the race! ....P...dc.ba.. (ended) Race ended: P won the race!
ayylmao123xdd
ayylmao123xdd2mo ago
well looks like its skipping cells or picking a different path basically
Yumi
YumiOP2mo ago
so... any tip on how could i make it go right?
ayylmao123xdd
ayylmao123xdd2mo ago
ill check the code tomorrow cuz i need to launch it to check
bassus
bassus2mo ago
My time to shine 😜 Am i right that the first file is all the code?
/**
* @author João Campos
*/
import java.util.Scanner;

//constants
final char[] CARS = {'P','a','b','c','d','e','f','g','h','i'};
final char START = 'S';
final char BOOST = '+';
final char DRAG = '-';
final char OIL = '!';
final String ACCEL = "accel";
final String SHOW = "show";
final String STATUS = "status";
final String QUIT = "quit";
final String INVALID_CMD = "Invalid command";
final String RACE_ONGOING = "The race is not over yet!";
final String ONGOING = " (ongoing)";
final String ENDED = " (ended)";

//global variables
int[] carPositions;
int[] carSpeeds;
int[] carLaps;
int totalLaps;
int maxSpeed;
int numOpponents;
int numCars;
String track;
boolean[] hasCrossedS;
boolean isRaceOver;
boolean isRaceQuit;
boolean yellowFlag;
char winner;


//domain methods
/**
* Initializes the race state with starting positions and values
*/
void initState() {
numCars = numOpponents + 1;
carSpeeds = new int[numCars];
carLaps = new int[numCars];
carPositions = new int[numCars];
hasCrossedS = new boolean[numCars];

isRaceOver = false;
isRaceQuit = false;
yellowFlag = false;
winner = ' ';

for (int i = 0; i < numCars; i++) {
carSpeeds[i] = 1;
carLaps[i] = 0;
carPositions[i] = 0;
hasCrossedS[i] = false;
}
setupInitialGrid();
}

/**
* Sets up initial positions for all cars on starting grid
*/
void setupInitialGrid() {
int startPosition = findStartPosition();
for (int i = 0; i < numCars; i++) {
carPositions[i] = getPosition(startPosition - 1 - i);
}
}

/**
* Calculates valid position on circular track with wrap-around
* @param position raw position to normalize
* @return normalized position within track bounds
*/
int getPosition(int position) {
int trackLength = track.length();
while (position < 0) {
position += trackLength;
}
return position % trackLength;
}
/**
* @author João Campos
*/
import java.util.Scanner;

//constants
final char[] CARS = {'P','a','b','c','d','e','f','g','h','i'};
final char START = 'S';
final char BOOST = '+';
final char DRAG = '-';
final char OIL = '!';
final String ACCEL = "accel";
final String SHOW = "show";
final String STATUS = "status";
final String QUIT = "quit";
final String INVALID_CMD = "Invalid command";
final String RACE_ONGOING = "The race is not over yet!";
final String ONGOING = " (ongoing)";
final String ENDED = " (ended)";

//global variables
int[] carPositions;
int[] carSpeeds;
int[] carLaps;
int totalLaps;
int maxSpeed;
int numOpponents;
int numCars;
String track;
boolean[] hasCrossedS;
boolean isRaceOver;
boolean isRaceQuit;
boolean yellowFlag;
char winner;


//domain methods
/**
* Initializes the race state with starting positions and values
*/
void initState() {
numCars = numOpponents + 1;
carSpeeds = new int[numCars];
carLaps = new int[numCars];
carPositions = new int[numCars];
hasCrossedS = new boolean[numCars];

isRaceOver = false;
isRaceQuit = false;
yellowFlag = false;
winner = ' ';

for (int i = 0; i < numCars; i++) {
carSpeeds[i] = 1;
carLaps[i] = 0;
carPositions[i] = 0;
hasCrossedS[i] = false;
}
setupInitialGrid();
}

/**
* Sets up initial positions for all cars on starting grid
*/
void setupInitialGrid() {
int startPosition = findStartPosition();
for (int i = 0; i < numCars; i++) {
carPositions[i] = getPosition(startPosition - 1 - i);
}
}

/**
* Calculates valid position on circular track with wrap-around
* @param position raw position to normalize
* @return normalized position within track bounds
*/
int getPosition(int position) {
int trackLength = track.length();
while (position < 0) {
position += trackLength;
}
return position % trackLength;
}
/**
* Finds the start line position on the track
* @return index of start line position
*/
int findStartPosition() {
for (int i = 0; i < track.length(); i++) {
if (track.charAt(i) == START) {
return i;

}
}
return 0;
}

/**
* Applies track effects (boost, drag, oil) to car at given index
* @param carIndex index of the car to apply effects to
*/
void applyTrackEffect(int carIndex) {
int position = carPositions[carIndex];
char cell = track.charAt(position);
switch (cell) {
case BOOST -> carSpeeds[carIndex] = Math.min(maxSpeed, carSpeeds[carIndex] + 1);
case DRAG -> carSpeeds[carIndex] = Math.max(0, carSpeeds[carIndex] - 1);
case OIL -> carSpeeds[carIndex] = 0;
}
}

/**
* Calculates AI acceleration decisions based on track conditions
*/
void calculateAIAcceleration() {
for (int i = 1; i < numCars; i++) {
int currentSpeed = carSpeeds[i];
int currentPos = carPositions[i];
boolean hasBoost = false;
boolean hasOil = false;

for (int j = 1; j <= 3; j++) {
int lookAhead = getPosition(currentPos + j);
char cell = track.charAt(lookAhead);
if (cell == BOOST) hasBoost = true;
if (cell == OIL) hasOil = true;
}

if (hasBoost && currentSpeed < maxSpeed)
carSpeeds[i] = Math.min(maxSpeed, carSpeeds[i] + 1);
else if (hasOil && currentSpeed > 0)
carSpeeds[i] = Math.max(0, carSpeeds[i] - 1);
}
}
/**
* Finds the start line position on the track
* @return index of start line position
*/
int findStartPosition() {
for (int i = 0; i < track.length(); i++) {
if (track.charAt(i) == START) {
return i;

}
}
return 0;
}

/**
* Applies track effects (boost, drag, oil) to car at given index
* @param carIndex index of the car to apply effects to
*/
void applyTrackEffect(int carIndex) {
int position = carPositions[carIndex];
char cell = track.charAt(position);
switch (cell) {
case BOOST -> carSpeeds[carIndex] = Math.min(maxSpeed, carSpeeds[carIndex] + 1);
case DRAG -> carSpeeds[carIndex] = Math.max(0, carSpeeds[carIndex] - 1);
case OIL -> carSpeeds[carIndex] = 0;
}
}

/**
* Calculates AI acceleration decisions based on track conditions
*/
void calculateAIAcceleration() {
for (int i = 1; i < numCars; i++) {
int currentSpeed = carSpeeds[i];
int currentPos = carPositions[i];
boolean hasBoost = false;
boolean hasOil = false;

for (int j = 1; j <= 3; j++) {
int lookAhead = getPosition(currentPos + j);
char cell = track.charAt(lookAhead);
if (cell == BOOST) hasBoost = true;
if (cell == OIL) hasOil = true;
}

if (hasBoost && currentSpeed < maxSpeed)
carSpeeds[i] = Math.min(maxSpeed, carSpeeds[i] + 1);
else if (hasOil && currentSpeed > 0)
carSpeeds[i] = Math.max(0, carSpeeds[i] - 1);
}
}
/**
* Checks if car crossed start line during movement
* @param startPosition starting position of movement
* @param endPosition ending position of movement
* @param startLinePosition position of start line
* @return true if start line was crossed during movement
*/
boolean hasCrossedStart(int startPosition, int endPosition, int startLinePosition) {
if (startPosition <= endPosition) {
return startPosition < startLinePosition && endPosition >= startLinePosition;
} else {
return startPosition < startLinePosition || endPosition >= startLinePosition;
}
}

/**
* Updates lap count if car crossed start line
* @param startPosition starting position of movement
* @param endPosition ending position of movement
* @param carIndex index of car to check
*/
void checkLapCompletion(int startPosition, int endPosition, int carIndex) {
int startLine = findStartPosition();
if (hasCrossedStart(startPosition, endPosition, startLine)) {
//doesn't increment carLaps as the first time the car passes S, it hasn't done a lap.
if (!hasCrossedS[carIndex])
hasCrossedS[carIndex] = true;
else
carLaps[carIndex]++;
}
}

/**
* Moves car according to current speed and resolves collisions
* @param carIndex index of car to move
*/
void moveCar(int carIndex) {
int currentSpeed = carSpeeds[carIndex];
if (yellowFlag)
currentSpeed = Math.min(currentSpeed, 1);

int oldPosition = carPositions[carIndex];
int targetPosition = getPosition(oldPosition + currentSpeed);
int finalPosition = resolveCollision(targetPosition, CARS[carIndex]);

carPositions[carIndex] = finalPosition;
applyTrackEffect(carIndex);
checkLapCompletion(oldPosition, finalPosition, carIndex);

if (carLaps[carIndex] >= totalLaps && !isRaceOver) {
isRaceOver = true;
winner = CARS[carIndex];
}
}
/**
* Checks if car crossed start line during movement
* @param startPosition starting position of movement
* @param endPosition ending position of movement
* @param startLinePosition position of start line
* @return true if start line was crossed during movement
*/
boolean hasCrossedStart(int startPosition, int endPosition, int startLinePosition) {
if (startPosition <= endPosition) {
return startPosition < startLinePosition && endPosition >= startLinePosition;
} else {
return startPosition < startLinePosition || endPosition >= startLinePosition;
}
}

/**
* Updates lap count if car crossed start line
* @param startPosition starting position of movement
* @param endPosition ending position of movement
* @param carIndex index of car to check
*/
void checkLapCompletion(int startPosition, int endPosition, int carIndex) {
int startLine = findStartPosition();
if (hasCrossedStart(startPosition, endPosition, startLine)) {
//doesn't increment carLaps as the first time the car passes S, it hasn't done a lap.
if (!hasCrossedS[carIndex])
hasCrossedS[carIndex] = true;
else
carLaps[carIndex]++;
}
}

/**
* Moves car according to current speed and resolves collisions
* @param carIndex index of car to move
*/
void moveCar(int carIndex) {
int currentSpeed = carSpeeds[carIndex];
if (yellowFlag)
currentSpeed = Math.min(currentSpeed, 1);

int oldPosition = carPositions[carIndex];
int targetPosition = getPosition(oldPosition + currentSpeed);
int finalPosition = resolveCollision(targetPosition, CARS[carIndex]);

carPositions[carIndex] = finalPosition;
applyTrackEffect(carIndex);
checkLapCompletion(oldPosition, finalPosition, carIndex);

if (carLaps[carIndex] >= totalLaps && !isRaceOver) {
isRaceOver = true;
winner = CARS[carIndex];
}
}
/**
* Executes one race round with all car movements
*/
void race() {
yellowFlag = false;
calculateAIAcceleration();
int i = 0;
while (!isRaceOver && i < numCars) {
moveCar(i);
i++;
}
}

/**
* Resolves collisions during car movement
* @param targetPosition intended target position
* @param carChar character identifier of moving car
* @return final position after collision resolution
*/
int resolveCollision(int targetPosition, char carChar) {
if (isPositionOccupied(targetPosition, carChar)) {
if (!yellowFlag) {
yellowFlag = true;
}
// Move to position before the collision if desired position is occupied
return getPosition(targetPosition - 1);
}
return targetPosition;
}

/**
* Checks if position is occupied by any other car
* @param position position to check
* @param excludeCar character of car to exclude from check
* @return true if position is occupied by another car
*/
boolean isPositionOccupied(int position, char excludeCar) {
int i = 0;
while (i < numCars && !(CARS[i] != excludeCar && carPositions[i] == position)) {
i++;
}
return i < numCars;
}


//interaction methods
/**
* Reads track configuration from input
*/
String readTrack(Scanner in) {
return in.nextLine();
}

/**
* Reads integer input values for race parameters
*/
int readInput(Scanner in) {
return in.nextInt();
}

/**
* Processes acceleration command for player car
* @param acceleration acceleration value (-1, 0, or +1)
*/
void accel(int acceleration) {
if (isRaceOver) {
System.out.println("Race ended: " + winner + " won the race!");
return;
}
carSpeeds[0] = Math.max(0, Math.min(maxSpeed, carSpeeds[0] + acceleration));
race();

if (isRaceOver)
System.out.println("Player " + winner + " won the race!");
else
System.out.println("Player P: cell "+ carPositions[0] +", laps "+ carLaps[0] +"!");
}
/**
* Executes one race round with all car movements
*/
void race() {
yellowFlag = false;
calculateAIAcceleration();
int i = 0;
while (!isRaceOver && i < numCars) {
moveCar(i);
i++;
}
}

/**
* Resolves collisions during car movement
* @param targetPosition intended target position
* @param carChar character identifier of moving car
* @return final position after collision resolution
*/
int resolveCollision(int targetPosition, char carChar) {
if (isPositionOccupied(targetPosition, carChar)) {
if (!yellowFlag) {
yellowFlag = true;
}
// Move to position before the collision if desired position is occupied
return getPosition(targetPosition - 1);
}
return targetPosition;
}

/**
* Checks if position is occupied by any other car
* @param position position to check
* @param excludeCar character of car to exclude from check
* @return true if position is occupied by another car
*/
boolean isPositionOccupied(int position, char excludeCar) {
int i = 0;
while (i < numCars && !(CARS[i] != excludeCar && carPositions[i] == position)) {
i++;
}
return i < numCars;
}


//interaction methods
/**
* Reads track configuration from input
*/
String readTrack(Scanner in) {
return in.nextLine();
}

/**
* Reads integer input values for race parameters
*/
int readInput(Scanner in) {
return in.nextInt();
}

/**
* Processes acceleration command for player car
* @param acceleration acceleration value (-1, 0, or +1)
*/
void accel(int acceleration) {
if (isRaceOver) {
System.out.println("Race ended: " + winner + " won the race!");
return;
}
carSpeeds[0] = Math.max(0, Math.min(maxSpeed, carSpeeds[0] + acceleration));
race();

if (isRaceOver)
System.out.println("Player " + winner + " won the race!");
else
System.out.println("Player P: cell "+ carPositions[0] +", laps "+ carLaps[0] +"!");
}
/**
* Displays current track state with car positions
*/
void show() {
char[] displayTrack = track.toCharArray();
for (int i = 0; i < numCars; i++) {
displayTrack[carPositions[i]] = CARS[i];
}
if (!isRaceOver)
System.out.println(new String(displayTrack) + ONGOING);
else
System.out.println(new String(displayTrack) + ENDED);
}

/**
* Shows status of specified player
* @param playerId identifier of player to check
*/
void status(String playerId) {
char requestedCar = playerId.charAt(0);

if (isRaceOver && requestedCar == winner) {
System.out.println("Race ended: " + winner + " won the race!");
return;
}
int carIndex = -1;
if (requestedCar == 'P')
carIndex = 0;
else if (requestedCar >= 'a' && requestedCar < 'a' + numOpponents)
carIndex = requestedCar - 'a' + 1;

if (carIndex != -1 && carIndex < numCars) {
System.out.print("Player " + CARS[carIndex] + ": cell ");
System.out.println(carPositions[carIndex] + ", laps " + carLaps[carIndex] + "!");
} else {
System.out.println("Player " + requestedCar + " does not exist!");
}
}

/**
* Processes quit command and displays race result
*/
void quit() {
if (isRaceOver)
System.out.println("Race ended: " + winner + " won the race!");
else
System.out.println(RACE_ONGOING);
isRaceQuit = true;
}
/**
* Displays current track state with car positions
*/
void show() {
char[] displayTrack = track.toCharArray();
for (int i = 0; i < numCars; i++) {
displayTrack[carPositions[i]] = CARS[i];
}
if (!isRaceOver)
System.out.println(new String(displayTrack) + ONGOING);
else
System.out.println(new String(displayTrack) + ENDED);
}

/**
* Shows status of specified player
* @param playerId identifier of player to check
*/
void status(String playerId) {
char requestedCar = playerId.charAt(0);

if (isRaceOver && requestedCar == winner) {
System.out.println("Race ended: " + winner + " won the race!");
return;
}
int carIndex = -1;
if (requestedCar == 'P')
carIndex = 0;
else if (requestedCar >= 'a' && requestedCar < 'a' + numOpponents)
carIndex = requestedCar - 'a' + 1;

if (carIndex != -1 && carIndex < numCars) {
System.out.print("Player " + CARS[carIndex] + ": cell ");
System.out.println(carPositions[carIndex] + ", laps " + carLaps[carIndex] + "!");
} else {
System.out.println("Player " + requestedCar + " does not exist!");
}
}

/**
* Processes quit command and displays race result
*/
void quit() {
if (isRaceOver)
System.out.println("Race ended: " + winner + " won the race!");
else
System.out.println(RACE_ONGOING);
isRaceQuit = true;
}
/**
* Processes user commands from input
* @param in scanner for reading user input
*/
void processCommand(Scanner in) {
String command = in.next();
switch (command) {
case ACCEL -> {
accel(in.nextInt());
in.nextLine();
}
case SHOW -> {
show();
in.nextLine();
}
case STATUS -> {
status(in.next());
in.nextLine();
}
case QUIT -> {
quit();
in.nextLine();
}
default -> {
System.out.println(INVALID_CMD);
in.nextLine();
}
}
}

void main() {
Scanner input = new Scanner(System.in);
track = readTrack(input);
totalLaps = readInput(input);
maxSpeed = readInput(input);
numOpponents = readInput(input);
input.nextLine();
initState();

do {
processCommand(input);
} while (!isRaceQuit);
input.close();
}
/**
* Processes user commands from input
* @param in scanner for reading user input
*/
void processCommand(Scanner in) {
String command = in.next();
switch (command) {
case ACCEL -> {
accel(in.nextInt());
in.nextLine();
}
case SHOW -> {
show();
in.nextLine();
}
case STATUS -> {
status(in.next());
in.nextLine();
}
case QUIT -> {
quit();
in.nextLine();
}
default -> {
System.out.println(INVALID_CMD);
in.nextLine();
}
}
}

void main() {
Scanner input = new Scanner(System.in);
track = readTrack(input);
totalLaps = readInput(input);
maxSpeed = readInput(input);
numOpponents = readInput(input);
input.nextLine();
initState();

do {
processCommand(input);
} while (!isRaceQuit);
input.close();
}
Why is there even a readTrack or readInput? Why the input.nextLine(); int the main Btw, doing all of this with "global" variables is kinda meh in my opinion
Yumi
YumiOP2mo ago
yess "guidelines" they gave us at university... what would u recomend there?
bassus
bassus2mo ago
Bruh... change your uni^^
Yumi
YumiOP2mo ago
they tell us to do input.nextLine() always to consume the line break, idk
bassus
bassus2mo ago
Do the non-final ones with local variables and input parameters... might bloat the code though in other ways... probably there is no perfect solution without classes or at least structs Ah wait true, i'm just braindead Why even do all of this in Java, this looks like cracked C (not your fault, i'm lamenting over the didactics your teachers use)
Yumi
YumiOP2mo ago
ik it does.. i've coded in C before and it looks too similar 😭 well, in the meantime i was able to fix my problem with the collisions, that was.. doubtufull
Dexter
Dexter2mo ago
int resolveCollision(int targetPosition, char carChar) {
if (isPositionOccupied(targetPosition, carChar)) {
if (!yellowFlag) {
yellowFlag = true;
}
// Move to position before the collision if desired position is occupied
return resolveCollision(targetPosition - 1, carChar);
}
return getPosition(targetPosition);
}
int resolveCollision(int targetPosition, char carChar) {
if (isPositionOccupied(targetPosition, carChar)) {
if (!yellowFlag) {
yellowFlag = true;
}
// Move to position before the collision if desired position is occupied
return resolveCollision(targetPosition - 1, carChar);
}
return getPosition(targetPosition);
}
This message has been formatted automatically. You can disable this using /preferences.
bassus
bassus2mo ago
This is sus:
/**
* Resolves collisions during car movement
* @param targetPosition intended target position
* @param carChar character identifier of moving car
* @return final position after collision resolution
*/
int resolveCollision(int targetPosition, char carChar) {
if (isPositionOccupied(targetPosition, carChar)) {
if (!yellowFlag) {
yellowFlag = true;
}
// Move to position before the collision if desired position is occupied
return getPosition(targetPosition - 1);
}
return targetPosition;
}
/**
* Resolves collisions during car movement
* @param targetPosition intended target position
* @param carChar character identifier of moving car
* @return final position after collision resolution
*/
int resolveCollision(int targetPosition, char carChar) {
if (isPositionOccupied(targetPosition, carChar)) {
if (!yellowFlag) {
yellowFlag = true;
}
// Move to position before the collision if desired position is occupied
return getPosition(targetPosition - 1);
}
return targetPosition;
}
Why do you just return getPosition(targetPosition - 1);? How do you know, that the next position behind is free Lol, you where one second faster Still errors popping up?
Yumi
YumiOP2mo ago
there is a very fun error, in a code part that should run fine, the car just decides to do +1 speed on his own, and return to normal behaviour, but with +1 in position... that was the point, i didn't know, and there was a big chance it wasn't free
bassus
bassus2mo ago
I know, this is why i wrote this https://discord.com/channels/648956210850299986/1432037931798761472/1432102423731900417 xD Show Maybe this is the cause, wild guess
currentSpeed = Math.min(currentSpeed, 1);
currentSpeed = Math.min(currentSpeed, 1);
Yumi
YumiOP2mo ago
amen, lol
bassus
bassus2mo ago
I have to proof read a friend's bachelor's thesis now, i will help later if you still need
Yumi
YumiOP2mo ago
no, ig i'm done with the project.. it was due 8pm.. but as hour changed, it gave us an extra hour, lol but i'll just accept my 97% on the code function still, thank you very much
JavaBot
JavaBot2mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
bassus
bassus2mo ago
Hmmm, wait How long till you have to put it in in minutes? @Yumi
Yumi
YumiOP2mo ago
24
bassus
bassus2mo ago
Okay, we got this
Yumi
YumiOP2mo ago
good luck to your friend 🫡
bassus
bassus2mo ago
thx
JavaBot
JavaBot2mo ago
Post Closed
This post has been closed by <@1348700887161245751>.

Did you find this page helpful?