This site requires JavaScript, please enable it in your browser!
Greenfoot back
cobblerschris
cobblerschris wrote ...

2012/4/19

AI Work

cobblerschris cobblerschris

2012/4/19

#
Hi i am a newbie..... i have been given an assignment to complete with some sample code. I am not looking for anyone to do the work for me in any way. i am looking for someone to help me understand the code and then i can work out whats going on. Is anyone able to help?
cobblerschris cobblerschris

2012/4/19

#
.
IsVarious IsVarious

2012/4/20

#
You'll get a bit further if you post what you're wanting help with, and give maybe some small examples of what you've tried that isn't working. It's a bit confusing what you wrote, and I'm not sure "exactly" what you're wanting help with mate.
cobblerschris cobblerschris

2012/4/20

#
Hi Here is the sample code i have been given: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class robot1 here. * * @author (your name) * @version (a version number or a date) */ public class robot1 extends Actor { private static final int EAST = 0; private static final int WEST = 1; private static final int NORTH = 2; private static final int SOUTH = 3; private int w={0,0,0,0,0,0}; private int s1=1; private int s2=1; private int net=0; private int net2=0; private int output1=0; private int output2=0; private int direction; public robot1() { setDirection(EAST); } public void act() { int w={1,-2,-1,1,-1,-2};//six value bias1,w1,w2,bias2,w3,w4 s1=sensorRight(); s2=sensorLeft(); net=s1*w+s2*w+w; if (net>=0) { output1=1; } else { output1=0; } net2=s1*w+s2*w+w; if (net2>=0) { output2=1; } else { output2=0; } if((output1==0)&&(output2==0)) { backward(2); } else if ((output1==1)&&(output2==1)) { forward(2); } else if ((output1==1)&&(output2==0)) { turnLeft(); forward(1); } else if ((output1==0)&&(output2==1)) { turnRight(); forward(1); } } public void setDirection(int direction) { this.direction = direction; switch(direction) { case SOUTH : setRotation(90); break; case EAST : setRotation(0); break; case NORTH : setRotation(270); break; case WEST : setRotation(180); break; default : break; } } public void turnLeft() { switch(direction) { case SOUTH : setDirection(EAST); break; case EAST : setDirection(NORTH); break; case NORTH : setDirection(WEST); break; case WEST : setDirection(SOUTH); break; } } public void turnRight() { switch(direction) { case SOUTH : setDirection(WEST); break; case EAST : setDirection(SOUTH); break; case NORTH : setDirection(EAST); break; case WEST : setDirection(NORTH); break; } } public boolean canMove() { World myWorld = getWorld(); int x = getX(); int y = getY(); // test for outside border if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) { return false; } else if (x < 0 || y < 0) { return false; } if(checkLeftBumper()==true) { backward(1); } return true; } public void forward(int howMany) { for(int loop=0;loop<howMany;loop++) { fwd(); } } public void backward(int howMany) { for(int loop=0;loop<howMany;loop++) { bwd(); } } public void bwd() { switch(direction) { case SOUTH : setLocation(getX(), getY() - 1); break; case EAST : setLocation(getX() - 1, getY()); break; case NORTH : setLocation(getX(), getY() + 1); break; case WEST : setLocation(getX() + 1, getY()); break; } } public void fwd() { if (!canMove()) { return; } switch(direction) { case SOUTH : setLocation(getX(), getY() + 1); break; case EAST : setLocation(getX() + 1, getY()); break; case NORTH : setLocation(getX(), getY() - 1); break; case WEST : setLocation(getX() - 1, getY()); break; } } public boolean checkLeftBumper() { Actor block=null; switch(direction) { case SOUTH : block = getOneObjectAtOffset(1, 0, prey.class); break; case EAST : block = getOneObjectAtOffset(0, 1, prey.class); break; case NORTH : block = getOneObjectAtOffset(-1,0, prey.class); break; case WEST : block = getOneObjectAtOffset(0, -1, prey.class); break; } if(block!=null) { return true; } else { return false; } } public boolean checkRightBumper() { Actor block = getOneObjectAtOffset(0, 1, prey.class); if(block!=null) { return true; } else { return false; } } public int sensorRight() { int s=0; if (checkRightBumper()==true) { s=1; } else { s=0; } return s; } public int sensorLeft() { int s=0; if (checkLeftBumper()==true) { s=1; } else { s=0; } return s; } }
davmac davmac

2012/4/20

#
... and what is it that you are having trouble with?
cobblerschris cobblerschris

2012/4/20

#
basicly i have a prey and a robot.... the prey is controlled by the user and the robot using weights and bias follows the prey automatically. i have worked out my weights and bias... my problem is none of the code has comments and i cant work out what any of it does.....
cobblerschris cobblerschris

2012/4/20

#
Hi i have made some changes to the code which is below..... however the robot still does not turn to the left or right to follow the prey..... can anyone see what im missing??? Thanks import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class robot1 here. * * @author (your name) * @version (a version number or a date) */ public class robot1 extends Actor { private static final int EAST = 0; private static final int WEST = 1; private static final int NORTH = 2; private static final int SOUTH = 3; private int w={0,0,0,0,0,0}; private int s1=1; private int s2=1; private int net=0; private int net2=0; private int output1=0; private int output2=0; private int direction; public robot1() { setDirection(EAST); } public void act() { int w={1,-2,-1,1,-1,-2};//six value bias1,w1,w2,bias2,w3,w4 s1=sensorRight(); s2=sensorLeft(); net=s1*w+s2*w+w; if (net>=0) { output1=1; } else { output1=0; } net2=s1*w+s2*w+w; if (net2>=0) { output2=1; } else { output2=0; } if((output1==0)&&(output2==0)) { backward(2); } else if ((output1==1)&&(output2==1)) { forward(2); } else if ((output1==1)&&(output2==0)) { turnLeft(2); forward(0); } else if ((output1==0)&&(output2==1)) { turnRight(2); forward(0); } } public void setDirection(int direction) { this.direction = direction; switch(direction) { case SOUTH : setRotation(90); break; case EAST : setRotation(0); break; case NORTH : setRotation(270); break; case WEST : setRotation(180); break; default : break; } } public void turnLeft() { switch(direction) { case SOUTH : setLocation(getX(), getY() - 1); break; case EAST : setLocation(getX() - 1, getY()); break; case NORTH : setLocation(getX(), getY() + 1); break; case WEST : setLocation(getX() + 1, getY()); break; } } public void turnRight() { switch(direction) { case SOUTH : setLocation(getX(), getY() - 1); break; case EAST : setLocation(getX() - 1, getY()); break; case NORTH : setLocation(getX(), getY() + 1); break; case WEST : setLocation(getX() + 1, getY()); break; } } public boolean canMove() { World myWorld = getWorld(); int x = getX(); int y = getY(); // test for outside border if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) { return false; } else if (x < 0 || y < 0) { return false; } if(checkLeftBumper()==true) { backward(1); } return true; } public void turnLeft(int howMany) { for(int loop=0;loop<howMany;loop++) { turnLeft(); } } public void turnRight(int howMany) { for(int loop=0;loop<howMany;loop++) { turnRight(); } } public void forward(int howMany) { for(int loop=0;loop<howMany;loop++) { fwd(); } } public void backward(int howMany) { for(int loop=0;loop<howMany;loop++) { bwd(); } } public void bwd() { switch(direction) { case SOUTH : setLocation(getX(), getY() - 1); break; case EAST : setLocation(getX() - 1, getY()); break; case NORTH : setLocation(getX(), getY() + 1); break; case WEST : setLocation(getX() + 1, getY()); break; } } public void fwd() { if (!canMove()) { return; } switch(direction) { case SOUTH : setLocation(getX(), getY() + 1); break; case EAST : setLocation(getX() + 1, getY()); break; case NORTH : setLocation(getX(), getY() - 1); break; case WEST : setLocation(getX() - 1, getY()); break; } } public boolean checkLeftBumper() { Actor block=null; switch(direction) { case SOUTH : block = getOneObjectAtOffset(1, 0, prey.class); break; case EAST : block = getOneObjectAtOffset(0, 1, prey.class); break; case NORTH : block = getOneObjectAtOffset(-1,0, prey.class); break; case WEST : block = getOneObjectAtOffset(0, -1, prey.class); break; } if(block!=null) { return true; } else { return false; } } public boolean checkRightBumper() { Actor block = getOneObjectAtOffset(0, 1, prey.class); if(block!=null) { return true; } else { return false; } } public int sensorRight() { int s=0; if (checkRightBumper()==true) { s=1; } else { s=0; } return s; } public int sensorLeft() { int s=0; if (checkLeftBumper()==true) { s=1; } else { s=0; } return s; } }
IsVarious IsVarious

2012/4/21

#
So, is it that you're not sure what all the code means, or that you're having errors and you're trying to figure out where it's at? You don't need to keep pasting the entire code list, I would start with what you're having issues with, and only copy that part of code. Something I've found which helps a ton with programming, and that's to study one element of programming till you fully master/understand it. For example, to look at your code it's pretty easy for me to read, but that doesn't help you much. And instead of having to come back here and post blocks of code, if you need help understanding what's happening I'm more than willing to help. if you want you can email me directly at IsVarious@yahoo.com Hope this helps.
cobblerschris cobblerschris

2012/4/21

#
hi IsVarious ill email you directly shortly Thanks
cobblerschris cobblerschris

2012/4/22

#
Hi i have emailed you.....
You need to login to post a reply.