//by: Tien-tien Lai //CS370 Lab 1 // Slightly modified by Eric Chown 9/25/00 /********************************************************************** The agent clean the floor row by row. It doesn't do the last column until it needs to go home **********************************************************************/ public class think implements AgentProgram { int step = 0; public Action execute(Percept perceptArg) { /* * the STEP variable's last two digits are for the number of tiles in * the room. * the STEP variable's first digit (hundred) is for the agent to * tell where it should go (turn left, turn right, or forward) */ VacuumPercept percept = (VacuumPercept)perceptArg; // get that dirt! if (percept.isDirt) return new AnAction("suck"); // when at home if (percept.isHome) { //coming home! if(percept.isBump){ return new AnAction("Shut-off"); } // note: could be better by just checking if state == 0 // that would save one extra step. EC //leaving home else{ return new AnAction("forward"); } } /* * When the agent hits the wall (most of time it's the right side * of the wall), you always want to turn left. * If it hits the top wall, it wants to go left so it can go home. */ if (percept.isBump){ step+=200; return new AnAction("turn","left"); } /* * STEP = 500 can only happen when the agent needs to turn * right so it can go down toward the right side of the room. */ if(step == 500){ step-=500; return new AnAction("turn","right"); } /* * STEP = 400 can only happen when the agent walked toward the * left. At the end - 1 tile, it turns right to go to the new * row. */ else if(step==400){ step+=100; return new AnAction("forward"); } /* * if STEP is between 300 and 400, that means it's at the next * rows, and the agent needs to turn left (walks toward the left * wall) */ else if(step>=300 && step<400){ /* * take out 201 because I want to make sure is's between * 100 and 200. take out 1 is because the tile the agent * is on, also count as part of the next row. */ step-=201; return new AnAction("turn","left"); } /* * if STEP is between 200 and 300, that means it hits the wall *and turned left, so the agent needs to take a step forward. */ else if(step>=200 && step<300){ step+=100; return new AnAction("forward"); } /* *walks toward the left side of the room, step-- */ else if(step>100 && step<200){ step--; return new AnAction("forward"); } /* * if the STEP is equal to 100, that means it should turn right * because the agent shouldn't walk any further towards the left * side of the wall. I want to make sure it doesn't clean the * very last column. */ else if(step == 100){ /* * I want to make sure the agent * knows this is on the left side of the room. It needs * to turn right again, after it takes another step * forward. Therefore, adding 300 to 100 = 400 means the * agent is on the left side of the room, and it needs to * take a right again. */ step+=300; return new AnAction("turn","right"); } /* * Walk toward the right side of the room, step++. * this also applies to when the agent hits to many walls (the * steps is greater than 500, then it knows it should go home (so * it needs to keep going forward. */ else{ step++; return new AnAction("forward"); } } }