Joe
Joe

Reputation:

Can anyone lend a fresh pair of eyes for debugging?

The purpose of this assignment was to create a Field and Robot class and objects of those classes.

The single field object is limited to a square of points from (0, 0) to (50, 50), and contains 3 pieces of gold and 3 bombs.

Three robot objects search the field (one after another) for gold from left to right (0, 0) to (0, 50) and descend through the field (1, 0) to 1, 50) and so on.

The robots are destroyed by bombs that are places by input from the user. Once the gold is collected it cannot be picked up by another robot, and one a bomb explodes it does not do so again.

This is my attempt at solving the problem so far, I am continuing to work on it, but would appreciate a second pair of eyes on it to catch something im missing. The program compiles, but the bombs and gold arent being "found" correctly and the output states that the following robots die on the same bomb as the one before it. Also, there are several pieces of code removed by comments, I did this to test different parts of the program. I think this section is where I'm having trouble. The methods field.alreadyFound() and field.alreadyBombed() return a boolean with the value true. My if statements should be saying if the gold/bomb has already been found, ignore it.

while(x <= 50 && y <= 50 && alive2 == true) {
    foundGold1 = robot2.look(field.locateGold1());
    foundGold2 = robot2.look(field.locateGold2());
    foundGold3 = robot2.look(field.locateGold3());
    foundBomb1 = robot2.look(field.locateBomb1()); 
    foundBomb2 = robot2.look(field.locateBomb2());
    foundBomb3 = robot2.look(field.locateBomb3());
    /*gotBomb1 = field.alreadyBombed1();
    gotBomb2 = field.alreadyBombed2();
    gotBomb3 = field.alreadyBombed3();
    gotGold1 = field.alreadyFound1();
    gotGold2 = field.alreadyFound2();
    gotGold3 = field.alreadyFound3();*/

    if (foundGold1 == true){
    if (field.alreadyFound1() == true){}
        else {robot2.addGold();
            field.foundGold1();}
    }
    if (foundGold2 == true) {
    if (field.alreadyFound2() == true){}
        else {robot2.addGold();
            field.foundGold2();}
    }
    if (foundGold3 == true) {
    if (field.alreadyFound3() == true){}
        else {robot2.addGold();
            field.foundGold3();}
    }
    if (foundBomb1 == true) {
    if (field.alreadyBombed1() == true){}
        else alive2 = false;
    }
    if (foundBomb2 == true) {
    if (field.alreadyBombed2() == true){}
        else alive2 = false;
    }
    if (foundBomb3 == true) {
    if (field.alreadyBombed3() == true){}
        else alive2 = false;
    }
    y = y + 1;
    robot2.setLocation(x, y);
    //System.out.println(y);

    if (y == 50)
    {x = x + 1;
    y = 0;} 
}

Upvotes: 0

Views: 110

Answers (1)

Dave Newton
Dave Newton

Reputation: 160191

I don't see where you set that a bomb has exploded, so from what I see here, you're missing that part.

Below is the code reformatted and slightly restructured: I found the code fairly difficult to work with as-is. It may be easier to work with this shorter, more canonical, and IMO more communicative version.

while (x <= 50 && y <= 50 && alive2 == true) {
    foundGold1 = robot2.look(field.locateGold1());
    foundGold2 = robot2.look(field.locateGold2());
    foundGold3 = robot2.look(field.locateGold3());
    foundBomb1 = robot2.look(field.locateBomb1());
    foundBomb2 = robot2.look(field.locateBomb2());
    foundBomb3 = robot2.look(field.locateBomb3());
    /*gotBomb1 = field.alreadyBombed1();
   gotBomb2 = field.alreadyBombed2();
   gotBomb3 = field.alreadyBombed3();
   gotGold1 = field.alreadyFound1();
   gotGold2 = field.alreadyFound2();
   gotGold3 = field.alreadyFound3();*/

    if (foundGold1 && !field.alreadyFound1()) {
        robot2.addGold();
        field.foundGold1();
    }

    if (foundGold2 && !field.alreadyFound2()) {
        robot2.addGold();
        field.foundGold2();
    }

    if (foundGold3 && !field.alreadyFound3()) {
        robot2.addGold();
        field.foundGold3();
    }

    if (foundBomb1 && !field.alreadyBombed1()) {
        alive2 = false;
    }

    if (foundBomb2 && !field.alreadyBombed2()) {
        alive2 = false;
    }

    if (foundBomb3 && !field.alreadyBombed3()) {
        alive2 = false;
    }

    y = y + 1;
    robot2.setLocation(x, y);

    if (y == 50) {
        x = x + 1;
        y = 0;
    }
}

You may also be hampered by the amount of code it takes to do this work: I assume you have essentially identical code for robot1 and robot2, the only difference being which robot you're currently processing.

Rather than repeat the code, consider passing a currentRobot in to a single method. There are a variety of ways to go about dealing with issues like this, but that fits in well with what you've already done. You're likely want to add an isAlive method/property to the robot class.

Upvotes: 4

Related Questions