5. ตัวอย่างโปรแกรมที่เขียนโดยไม่มีคาสั่ง
่
การตัดสินใจหรือวนลูป
import becker.robots.*;//เรียกใช้ becker
public class longprogram //ประกาศ class longprogram
{
public static void main(String[]args) //ประกาศฟังก์ชันหลักในการท่างาน
{
City tai = new City(); //สร้างเมืองใหม่
Robot dog1 = new Robot(tai ,0,3,Direction.SOUTH);//สร้างโรบอทdog1
Robot dog2 = new Robot(tai ,0,4,Direction.SOUTH);//สร้างโรบอทdog2
Thing box = new Thing(tai,5,5);//สร้างสิ่งของ
dog1.setLabel("A");//ก่าหนดให้ dog1 มีชื่อว่า A
dog2.setLabel("B");//ก่าหนดให้ dog2 มีชื่อว่า AB
new Wall(tai ,0,0,Direction.NORTH);//สร้างก่าแพงต่างๆ
new Wall(tai ,0,1,Direction.NORTH);
new Wall(tai ,0,2,Direction.NORTH);
new Wall(tai ,0,3,Direction.NORTH);
new Wall(tai ,0,4,Direction.NORTH);
new Wall(tai ,0,5,Direction.NORTH);
new Wall(tai ,0,6,Direction.NORTH);
new Wall(tai ,0,7,Direction.NORTH);
new Wall(tai ,0,0,Direction.WEST);
new Wall(tai ,1,0,Direction.WEST);
new Wall(tai ,2,0,Direction.WEST);
new Wall(tai ,3,0,Direction.WEST);
new Wall(tai ,4,0,Direction.WEST);
new Wall(tai ,5,0,Direction.WEST);
new Wall(tai ,6,0,Direction.WEST);
new Wall(tai ,7,0,Direction.WEST);
new Wall(tai ,0,7,Direction.EAST);
new Wall(tai ,1,7,Direction.EAST);
new Wall(tai ,2,7,Direction.EAST);
new Wall(tai ,3,7,Direction.EAST);
new Wall(tai ,4,7,Direction.EAST);
new Wall(tai ,5,7,Direction.EAST);
new Wall(tai ,6,7,Direction.EAST);
6. new Wall(tai ,7,7,Direction.EAST);
new Wall(tai ,7,0,Direction.SOUTH);
new Wall(tai ,7,1,Direction.SOUTH);
new Wall(tai ,7,2,Direction.SOUTH);
new Wall(tai ,7,3,Direction.SOUTH);
new Wall(tai ,7,4,Direction.SOUTH);
new Wall(tai ,7,5,Direction.SOUTH);
new Wall(tai ,7,6,Direction.SOUTH);
new Wall(tai ,7,7,Direction.SOUTH);
dog1.move();//ให้ dog1 กระท่าเดินหน้า 1 ครั้ง
dog1.move();
dog1.move();
dog1.move();
dog1.move();
dog1.turnLeft();/");//ให้ dog1 กระท่าเลี้ยวซ้าย
dog1.move();
dog1.move();
dog1.pickThing();//ให้ dog1 กระท่าเก็บของ
dog1.turnLeft();
dog1.move();
dog1.move();
dog1.move();
dog1.move();
dog1.move();
dog1.putThing();//ให้ dog1 กระท่าวางของ
dog2.turnLeft();
dog2.move();
dog2.pickThing();
dog2.move();
dog2.move();
dog2.putThing();
dog2.turnLeft();
dog2.turnLeft();
dog2.move();
}
}