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

📄 directionhelper.java

📁 Position.java :辅助类 SerpentGameCanvas.java :游戏主控类 Maze.java :迷宫 SerpentGame.java :应用主控类 Serpent.java
💻 JAVA
字号:
class DirectionHelper
{
	Maze maze;
	Position curPos;
	int directionList[]={
			Serpent.MOVE_UP, Serpent.MOVE_DOWN, Serpent.MOVE_LEFT, Serpent.MOVE_RIGHT
		};
	Position positionList[]={
			new Position(0,-1), new Position(0,1), new Position(-1,0), new Position(1,0)
		};
	private int choiceList[]=new int[4];
	private int numOfChoices;
		
	DirectionHelper(Maze m, Position pos)
	{
		maze=m;
		curPos=pos;

		numOfChoices=0;
		Position posDiff=new Position();
		for( int i=0;i<4;i++ ) {
			posDiff.setValue(positionList[i]);
			if( !conflictWithWall(pos, i)) {
				choiceList[numOfChoices]=directionList[i];
				numOfChoices++;
			}
		}

	}
	
	private boolean conflictWithWall(Position head, int i)
	{
		Position posDiff=new Position();
		posDiff.setValue(positionList[i]);
		Position nextPos=new Position(head);
		nextPos.add(posDiff);
		if( nextPos.outOfRange(0,0, Maze.WIDTH-1, Maze.HEIGHT-1 )) return true;
		
		int direction=directionList[i];
		switch(direction) {
			case Serpent.MOVE_UP: 
				return ( maze.horizontalWalls[head.y-1][head.x]!=0 );
			case Serpent.MOVE_DOWN:
				return ( maze.horizontalWalls[head.y][head.x]!=0 );
			case Serpent.MOVE_LEFT:
				return ( maze.verticalWalls[head.y][head.x-1]!=0 );
			case Serpent.MOVE_RIGHT:
				return ( maze.verticalWalls[head.y][head.x]!=0 );
			default:
				return true;
		}
	}

	int numOfChoices()
	{
		return numOfChoices;
	}
	
	int getChoice(int n)
	{
		return choiceList[n];
	}
	
	void kickChoiceByValue(int direction)
	{
		int n=findChoice(direction);
		if( n==-1 ) return;
		for( int i=n;i<numOfChoices-1;i++ ) {
			choiceList[i]=choiceList[i+1];
		}
		numOfChoices--;
	}
	
	private int findChoice(int direction)
	{
		for( int i=0;i<numOfChoices;i++ ) {
			if( choiceList[i]==direction )
				return i;
		}
		return -1;
	}
	
	int reverseDirection(int direction)
	{
		switch(direction) {
			case Serpent.MOVE_UP: 
				return Serpent.MOVE_DOWN;
			case Serpent.MOVE_DOWN:
				return Serpent.MOVE_UP;
			case Serpent.MOVE_LEFT:
				return Serpent.MOVE_RIGHT;
			case Serpent.MOVE_RIGHT:
				return Serpent.MOVE_LEFT;
			default:
		}
		return direction;
	}
	
}

⌨️ 快捷键说明

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