⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 world.java

📁 是有关解释器的.用JAVA编写.可以解释一般的JAVA程序.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
			wall.avenue = eastOfAvenue;
			wall.street = atStreet;
			walls.add( wall );
		}
		update();
	}

	/**
	 * This method sets the size of the visible World. At the lower-left corner there
	 * is alway displayed the crossing of street 1 and avenue 1. Horizontally <code>avenues</code>
	 * avenues are displayed and vertically <code>streets</code> streets are displayed.
	 * However beepers, walls and robots can also be (and move) further north or east of the displayes
	 * routes. Though they cannot be further west or south of the first avenue or street.
	 */
	public static void setSize( int streets, int avenues )
	{
		getWorld().setSizeInternal( streets, avenues );
	}

	protected synchronized void setSizeInternal( int streets, int avenues )
	{
		setAvenues( avenues );
		setStreets( streets );
		update();
	}

	/**
	 * 
	 * @uml.property name="streets"
	 */
	public static void setStreets(int streets) {
		getWorld().setStreetsInternal(streets);
	}


	protected synchronized void setStreetsInternal( int streets )
	{
		this.streets = streets;
		if( this.streets < 2 )
			this.streets = 2;
		update();
		if( streetsText != null )
			streetsText.setText( "" + streets );
	}

	/**
	 * 
	 * @uml.property name="avenues"
	 */
	public static void setAvenues(int avenues) {
		getWorld().setAvenuesInternal(avenues);
	}


	protected synchronized void setAvenuesInternal( int avenues )
	{
		this.avenues = avenues;
		if( this.avenues < 2 )
			this.avenues = 2;
		update();
		if( avenuesText != null )
			avenuesText.setText( "" + avenues );
	}

	/**
	 * Sets the delay between Robot movements. The delay is measured in milliseconds.
	 * 
	 * @uml.property name="delay"
	 */
	public static void setDelay(int delay) {
		getWorld().setDelayInternal(delay);
	}


	protected void setDelayInternal( int delay )
	{
		this.delay = delay;
		if(this.delay < 0)
			this.delay = 0;
		update();
		if( delayText != null )
			delayText.setText( "" + delay );
	}
	
	private static int delay()
	{
		return getWorld().delayInternal();
	}
	
	protected int delayInternal()
	{
		return delay;
	}
	
	/**
	 * Checks if there is at least one beeper at the crossing of street <code>street</code>
	 * and avenue <code>avenue</code>.
	 */
	public static boolean checkBeeper( int street, int avenue )
	{
		return getWorld().checkBeeperInternal( street, avenue );
	}
	
	protected boolean checkBeeperInternal( int street, int avenue )
	{
		Enumeration enumeration = beepers.elements();
		while( enumeration.hasMoreElements() )
		{
			Beepers currentBeepers = (Beepers) enumeration.nextElement();
			if( ( currentBeepers.street == street ) && ( currentBeepers.avenue == avenue ) )
				return true;
		}
		return false;
	}
	
	/**
	 * Checks if there is a wall east of the avenue <code>eastOfAvenue</code> crossing the
	 * street <code>atStreet</code>.
	 */
	public static boolean checkEWWall( int atStreet, int eastOfAvenue )
	{
		return getWorld().checkEWWallInternal( atStreet, eastOfAvenue );
	}
	
	protected boolean checkEWWallInternal( int atStreet, int eastOfAvenue )
	{
		Enumeration enumeration = walls.elements();
		while( enumeration.hasMoreElements() )
		{
			Wall currentWall = (Wall) enumeration.nextElement();
			if( currentWall.isEastWest && ( currentWall.street == atStreet ) && ( currentWall.avenue == eastOfAvenue ) )
				return true;
		}
		return false;
	}

	/**
	 * Checks if there is a wall north of the street <code>northOfStreet</code> crossing the
	 * avenue <code>atAvenue</code>.
	 */
	public static boolean checkNSWall( int northOfStreet, int atAvenue )
	{
		return getWorld().checkNSWallInternal( northOfStreet, atAvenue );
	}
	
	protected boolean checkNSWallInternal( int northOfStreet, int atAvenue )
	{
		Enumeration enumeration = walls.elements();
		while( enumeration.hasMoreElements() )
		{
			Wall currentWall = (Wall) enumeration.nextElement();
			if( currentWall.isNorthSouth && ( currentWall.street == northOfStreet ) && ( currentWall.avenue == atAvenue ) )
				return true;
		}
		return false;
	}

	/**
	 * Saves the current constellation of walls, beepers and visible size of the World
	 * into a file.
	 */
	public static void saveWorld( String filename )
	{
		getWorld().saveWorldInternal( filename );
	}
	
	protected synchronized void saveWorldInternal( String filename )
	{
		PrintWriter writer = null;
		try
		{
			writer = new PrintWriter( new FileWriter( filename ) );
			writer.println( "KarelWorld" );
			
			writer.println( "streets " + numberOfStreets() );
			writer.println( "avenues " + numberOfAvenues() );
			
			Enumeration enumeration = beepers.elements();
			while( enumeration.hasMoreElements() )
			{
				Beepers current = (Beepers) enumeration.nextElement();
				writer.println( "beepers " + current.street + " " + current.avenue + " " + current.count );
			}

			enumeration = walls.elements();
			while( enumeration.hasMoreElements() )
			{
				Wall current = (Wall) enumeration.nextElement();
				if( current.isEastWest )
					writer.println( "eastwestwalls " + current.street + " " + current.avenue + " " + current.avenue );
				else
					writer.println( "northsouthwalls " + current.avenue + " " + current.street + " " + current.street );
			}
		}
		catch( IOException exception )
		{
			exception.printStackTrace();
			System.out.println( "World no saved" );
		}
		finally
		{
			if( writer != null )
			{
				writer.flush();
				writer.close();
			}
		}
	}
	
	/**
	 * Reads the visible size of the World and the positions of walls and beepers from
	 * a file. The World is reset and except for the Robots and robot Ids. Then the
	 * walls and beepers read from the file are set into the world.
	 */
	public static void readWorld( String filename )
	{
		getWorld().readWorldInternal( filename );
	}

	protected synchronized void readWorldInternal( String filename )
	{
		resetWalls();
		resetBeepers();
		BufferedReader reader = null;
		try
		{
			reader = new BufferedReader( new FileReader( filename ) );
			while( reader.ready() )
			{
				StringTokenizer tokens = new StringTokenizer( reader.readLine(), " ", false );
				if( tokens.hasMoreTokens() )
				{
					String action = tokens.nextToken();
					if( action.equalsIgnoreCase( "streets" ) )
					{
						setStreets( Integer.parseInt( tokens.nextToken() ) );
					}
					else if( action.equalsIgnoreCase( "avenues" ) )
					{
						setAvenues( Integer.parseInt( tokens.nextToken() ) );
					}
					else if( action.equalsIgnoreCase( "beepers" ) )
					{
						placeBeepers( Integer.parseInt( tokens.nextToken() ),
							Integer.parseInt( tokens.nextToken() ),
							Integer.parseInt( tokens.nextToken() )
						);
					}
					else if( action.equalsIgnoreCase( "northsouthwalls" ) )
					{
						int eoa = Integer.parseInt( tokens.nextToken() );
						int fs = Integer.parseInt( tokens.nextToken() );
						int ls = Integer.parseInt( tokens.nextToken() );
						placeNSWall( fs, eoa, ls - fs + 1 );
					}
					else if( action.equalsIgnoreCase( "eastwestwalls" ) )
					{
						int nos = Integer.parseInt( tokens.nextToken() );
						int fa = Integer.parseInt( tokens.nextToken() );
						int la = Integer.parseInt( tokens.nextToken() );
						placeEWWall( nos, fa, la - fa + 1 );
					}
				}
			}
		}
		catch( IOException exception )
		{
			exception.printStackTrace();
			System.out.println( "No World read" );
		}
		finally
		{
			if( reader != null )
				try{ reader.close(); } catch( IOException exception ) {}
		}
	}

	/**
	 * Returns the number of streets that are currently displayed.
	 */
	public static int numberOfStreets()
	{
		return getWorld().numberOfStreetsInternal();
	}
	
	protected int numberOfStreetsInternal()
	{
		return streets;
	}

	/**
	 * Returns the numer of avenues that are currently displayed.
	 */
	public static int numberOfAvenues()
	{
		return getWorld().numberOfAvenuesInternal();
	}
	
	protected int numberOfAvenuesInternal()
	{
		return avenues;
	}

	/**
	 * Sets if Robot states are to be traced (true is the default). See <a href="#trace()">trace()</a>
	 * for details on tracing.
	 * 
	 * @uml.property name="trace"
	 */
	public static void setTrace(boolean trace) {
		getWorld().setTraceInternal(trace);
	}

	protected void setTraceInternal( boolean trace )
	{
		this.trace = trace;
		update();
	}

// methods for handling robots
	/**
	 * This method is to be used to create a new Robot in this World.
	 * It should be called by the ur_Robot class which then is only
	 * a proxy. Every movement or state-change of the robot is actually
	 * done by the World.
	 */
	synchronized Integer getNewRobot( int street, int avenue, int beepers, direction dir )
	{
		if(street <= 0)
			throw new RuntimeException("Can not deliver robot to street <= 0: " + street);
		if(avenue <= 0)
			throw new RuntimeException("Can not deliver robot to avenue <= 0: " + avenue);
		if(beepers < 0)
			throw new RuntimeException("Can not deliver robot with negative number of beepers: " + beepers);
		
		sync();
		Integer currentRobotId = new Integer( nextRobotId );
		nextRobotId++;
		Robot robot = new Robot();
		robot.id = nextRobotId - 1;
		robot.avenue = avenue;
		robot.street = street;
		robot.beepers = beepers;
		robot.direction = dir.getValue();
		robots.put( currentRobotId, robot );
		update();
		trace( robot );
		return currentRobotId;
	}

	/**
	 * Returns the avenue on which the denoted Robot is currently standing.
	 */
	int getAvenue( Integer robotId )
	{
		Robot current = getRobot( robotId, false );

		return current.avenue;
	}

	/**
	 * Returns the street on which the denoted Robot is currently standing.
	 */
	int getStreet( Integer robotId )
	{
		Robot current = getRobot( robotId, false );

		return current.street;
	}

	/**
	 * Returns the current direction of the denoted Robot.
	 */
	int getDirection( Integer robotId )
	{
		Robot current = getRobot( robotId, false );

		return current.direction;
	}
	
	int getBeepers( Integer robotId )
	{
		Robot current = getRobot( robotId, false );

		return current.beepers;
	}
	
	boolean frontIsClear( Integer robotId )
	{
		Robot current = getRobot( robotId, false );
		
		if((current.avenue == 1 && current.direction == West.getValue()) ||
				(current.street == 1 && current.direction == South.getValue()))
			return false;

		Enumeration enumeration = walls.elements();
		while( enumeration.hasMoreElements() )
		{
			Wall currentWall = (Wall) enumeration.nextElement();
			if( current.direction == North.getValue() )
			{
				if( currentWall.isEastWest && currentWall.avenue == current.avenue && currentWall.street == current.street )
					return false;
			}
			else if( current.direction == South.getValue() )
			{
				if( currentWall.isEastWest && currentWall.avenue == current.avenue && currentWall.street == ( current.street - 1 ) )
					return false;
			}
			else if( current.direction == East.getValue() )
			{
				if( currentWall.isNorthSouth && currentWall.street == current.street && currentWall.avenue == current.avenue )
					return false;
			}
			else if( current.direction == West.getValue() )
			{
				if( currentWall.isNorthSouth && currentWall.street == current.street && currentWall.avenue == (current.avenue - 1))
					return false;
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -