User Tools

Site Tools


make_a_maze

This is an old revision of the document!


Walls or obstacles are pared of the World class and can be placed in a NS or a EW direction, and have length. Here is a link to the API which shows all the constructors and methods that belong to the World class.

World.setSize(int numberOfStreets, int numberOfAvenues) will change the size of the World.

3 Ways to Make Walls

  1. World.placeNSWall(), World.placeEWWall() have three parameters (sometimes called arguments). You need to tell these methods three integers, For a East-West Street: the number of the street, the number of the Avenue, and the length of the wall (either going East or North, depending on the direction. Once you get the hang of reading the API of a class you can quickly use or build upon other people's code (or even your own).
    import kareltherobot.*;
    public class Walls implements Directions {
    	public static void task() {
    		Robot karel = new Robot(1, 2, East, 0);
    		karel.move();    
    		karel.move();	  
    		karel.pickBeeper();   	                
    		karel.move();	  
    		karel.turnLeft();	  
    		karel.move();
    		karel.move();	  
    		karel.putBeeper();  
    		karel.move();
    		karel.turnOff();
     
    	}
    	// Main entry point
    	public static void main(String[] args) {
    		World.setDelay(150);
    		World.setSize(8, 9);
    		//World.readWorld("first.kwld");  
    		World.placeBeepers(1, 4, 1);
    		World.placeEWWall(3, 1, 3);
    		World.placeNSWall(1,6, 5);
    		World.placeEWWall(6, 1, 6);
    		World.setVisible();
    		task();
    	}
    }
  2. You can make a long string and send it all at once to the World. :
    public static void main(String[] args) {
    		String hurdles = "streets 8\n" + 
    				"avenues 9\n" + 
    				"eastwestwalls 3 1 3\n" + 
    				"northsouthwalls 1 6 5\n" + 
    				"eastwestwalls 6 1 6\n" + 
    				"beepers 1 4 1";
    		World.getWorld(hurdles);
    		World.setVisible();
            task();
    	}
  3. You can place a text file with these string commands, give the file a name, and add it to your project and use the call <code java> World.readWorld(“second.kwld”);
make_a_maze.1566055931.txt.gz · Last modified: 2019/08/17 11:32 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki