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

📄 mazerunaction.java

📁 一个J2ME游戏《savorMaze》的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 * @return
	 */
	private int[] getRandomNeighbor(int[][] rooms, int x, int y)
	{
		int direction = random.nextInt() % 4;
		if(direction < 0)
		{
			direction = -direction;
		}
		int[] neighbor = null;
		for(int i = 0; i < 4; i++)
		{
			switch(direction)
			{
				case 0: // EAST
				{
					if((x + 1 < grid) && 
							(rooms[x + 1][y] & 0x1000) == 0 && 
							(rooms[x][y] & 0x0010) == 0)
					{
						neighbor = new int[2];
						neighbor[0] = x + 1;
						neighbor[1] = y;
						rooms[x][y] &= ~EAST;
						rooms[x + 1][y] &= ~WEST;
					}
					break;
				}
				case 1: // SOUTH
				{
					if((y + 1 < grid) && 
							(rooms[x][y + 1] & 0x1000) == 0 && 
							(rooms[x][y] & 0x0020) == 0)
					{
						neighbor = new int[2];
						neighbor[0] = x;
						neighbor[1] = y + 1;
						rooms[x][y] &= ~SOUTH;
						rooms[x][y + 1] &= ~NORTH;
					}
					break;
				}
				case 2: // WEST
				{
					if((x - 1 >= 0) && 
							(rooms[x - 1][y] & 0x1000) == 0 && 
							(rooms[x][y] & 0x0040) == 0)
					{
						neighbor = new int[2];
						neighbor[0] = x - 1;
						neighbor[1] = y;
						rooms[x][y] &= ~WEST;
						rooms[x - 1][y] &= ~EAST;
					}
					break;
				}
				case 3: // NORTH
				{
					if((y - 1 >= 0) && 
							(rooms[x][y - 1] & 0x1000) == 0 && 
							(rooms[x][y] & 0x0080) == 0)
					{
						neighbor = new int[2];
						neighbor[0] = x;
						neighbor[1] = y - 1;
						rooms[x][y] &= ~NORTH;
						rooms[x][y - 1] &= ~SOUTH;
					}
					break;
				}
			}
			if(neighbor != null)
			{
				break;
			}
			if(++ direction >= 4)
			{
				direction = 0;
			}
		}
		return neighbor;
	}
	
	public void run()
	{
		robotRun = ((Boolean)canvas.getAttribute(GameRunner.ROBOTRUN)).booleanValue();
		level = ((Short)canvas.getAttribute(GameRunner.LEVEL)).shortValue();
		if(level == 0)
		{
			gridSize = 6;
			grid = 5;
			maxGrid = 5;
		}else if(level == 1)
		{
			gridSize = 4;
			grid = 11;
			maxGrid = 11;
		}else if(level == 2)
		{
			gridSize = 2;
			grid = 25;
			maxGrid = 25;
		}
		initialRooms();
		
		canvas.addCommand(nextCommand);
		canvas.addCommand(exitCommand);
		canvas.setCommandListener(this);
		
		while(acting)
		{
			perform(canvas.getBufferedGraphics());
			if(runingFlag && robotRun)
			{
				robotRun();
			}
			canvas.repaint();
			try{
				Thread.sleep(canvas.getSleepTime() / 3);
			}catch(InterruptedException ie)
			{
				// do nothing!
			}
		}
		acting = true;
		status = INITIAL;
		runingFlag = false;
	}
	
	private void robotRun()
	{
		// robot stop running while arrived terminal point.
		if(robot[0] == grid - 1 && robot[1] == grid - 1)
		{
			runingFlag = false;
			robotMemory.removeAllElements();
			System.gc();
			return;
		}
		// go to easy neighbor if possible.
		if((rooms[robot[0]][robot[1]] & EAST) == 0 &&
				robot[2] == EAST &&
				robot[3] != EAST)
		{
			// change room information.
			rooms[robot[0]][robot[1]] |= 0x10000;
			rooms[robot[0] + 1][robot[1]] |= 0x40000;
			// record path which robot walk through.
			robotMemory.push(new short[]{robot[0], robot[1], robot[2], robot[3]});
			// record room which need be re-drawn
			roomsNeedDraw.push(new int[]{robot[0], robot[1]});
			robot[0] ++;
			robot[2] = EAST;
			robot[3] = WEST;
		}else if((rooms[robot[0]][robot[1]] & SOUTH) == 0 &&
				robot[2] == SOUTH &&
				robot[3] != SOUTH)
		{
			rooms[robot[0]][robot[1]] |= 0x20000;
			rooms[robot[0]][robot[1] + 1] |= 0x80000;
			robotMemory.push(new short[]{robot[0], robot[1], robot[2], robot[3]});
			roomsNeedDraw.push(new int[]{robot[0], robot[1]});
			robot[1] ++;
			robot[2] = EAST;
			robot[3] = NORTH;
		}else if((rooms[robot[0]][robot[1]] & WEST) == 0 &&
				robot[2] == WEST &&
				robot[3] != WEST)
		{
			rooms[robot[0]][robot[1]] |= 0x40000;
			rooms[robot[0] - 1][robot[1]] |= 0x10000;
			robotMemory.push(new short[]{robot[0], robot[1], robot[2], robot[3]});
			roomsNeedDraw.push(new int[]{robot[0], robot[1]});
			robot[0] --;
			robot[2] = EAST;
			robot[3] = EAST;
		}else if((rooms[robot[0]][robot[1]] & NORTH) == 0 &&
				robot[2] == NORTH &&
				robot[3] != NORTH)
		{
			rooms[robot[0]][robot[1]] |= 0x80000;
			rooms[robot[0]][robot[1] - 1] |= 0x20000;
			robotMemory.push(new short[]{robot[0], robot[1], robot[2], robot[3]});
			roomsNeedDraw.push(new int[]{robot[0], robot[1]});
			robot[1] --;
			robot[2] = EAST;
			robot[3] = SOUTH;
		}else
		{
			// go to next direction if current direction is available;
			// go back to last room if no direction is available in this room.
			robot[2] <<= 1;
			while(robot[2] > NORTH)
			{
				// clean current room's solution by robot.
				rooms[robot[0]][robot[1]] &= ~0xF0000;
				roomsNeedDraw.push(new int[]{robot[0], robot[1]});
				// get last room location of robot and clean the solution with current room.
				robot = (short[])robotMemory.pop();
				if((robot[2] & EAST) == EAST)
				{
					rooms[robot[0]][robot[1]] &= ~0x10000;
				}else if((robot[2] & SOUTH) == SOUTH)
				{
					rooms[robot[0]][robot[1]] &= ~0x20000;
				}else if((robot[2] & WEST) == WEST)
				{
					rooms[robot[0]][robot[1]] &= ~0x40000;
				}else if((robot[2] & NORTH) == NORTH)
				{
					rooms[robot[0]][robot[1]] &= ~0x80000;
				}
				robot[2] <<= 1;
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see com.savorjava.game.Action#interrupt()
	 */
	public void interrupt(int key) {

		switch(status)
		{
			case RUN:
			{
				runingFlag = true;
				int action2key = canvas.getGameAction(key);
				if(action2key == Canvas.UP || key == Canvas.KEY_NUM2)
				{
					if((rooms[hacker[0]][hacker[1]] & NORTH) == 0)
					{
						roomsNeedDraw.push(new int[]{hacker[0], hacker[1]});
						if((rooms[hacker[0]][hacker[1]] & 0x0800) == 0)
						{
							rooms[hacker[0]][hacker[1]] |= 0x0800;
							rooms[hacker[0]][hacker[1] - 1] |= 0x0200;
						}else
						{
							rooms[hacker[0]][hacker[1]] &= ~0x0800;
							rooms[hacker[0]][hacker[1] - 1] &= ~0x0200;
						}
						hacker[1] --; 
					}
				}else if(action2key == Canvas.DOWN || key == Canvas.KEY_NUM8)
				{
					if((rooms[hacker[0]][hacker[1]] & SOUTH) == 0)
					{
						roomsNeedDraw.push(new int[]{hacker[0], hacker[1]});
						if((rooms[hacker[0]][hacker[1]] & 0x0200) == 0)
						{
							rooms[hacker[0]][hacker[1]] |= 0x0200;
							rooms[hacker[0]][hacker[1] + 1] |= 0x0800;
						}else
						{
							rooms[hacker[0]][hacker[1]] &= ~0x0200;
							rooms[hacker[0]][hacker[1] + 1] &= ~0x0800;
						}
						hacker[1] ++; 
					}
				}else if(action2key == Canvas.LEFT || key == Canvas.KEY_NUM4)
				{
					if((rooms[hacker[0]][hacker[1]] & WEST) == 0)
					{
						roomsNeedDraw.push(new int[]{hacker[0], hacker[1]});
						if((rooms[hacker[0]][hacker[1]] & 0x0400) == 0)
						{
							rooms[hacker[0]][hacker[1]] |= 0x0400;
							rooms[hacker[0] - 1][hacker[1]] |= 0x0100;
						}else
						{
							rooms[hacker[0]][hacker[1]] &= ~0x0400;
							rooms[hacker[0] - 1][hacker[1]] &= ~0x0100;
						}
						hacker[0] --; 
					}
				}else if(action2key == Canvas.RIGHT || key == Canvas.KEY_NUM6)
				{
					if((rooms[hacker[0]][hacker[1]] & EAST) == 0)
					{
						roomsNeedDraw.push(new int[]{hacker[0], hacker[1]});
						if((rooms[hacker[0]][hacker[1]] & 0x0100) == 0)
						{
							rooms[hacker[0]][hacker[1]] |= 0x0100;
							rooms[hacker[0] + 1][hacker[1]] |= 0x0400;
						}else
						{
							rooms[hacker[0]][hacker[1]] &= ~0x0100;
							rooms[hacker[0] + 1][hacker[1]] &= ~0x0400;
						}
						hacker[0] ++; 
					}
				}else if(action2key == Canvas.FIRE || key == Canvas.KEY_NUM5)
				{
					status = PASS;
				}
				break;
			}
			case INITIAL:
			{
				break;
			}
			case STOP:
			{
				break;
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see javax.microedition.lcdui.Displayable#keyPressed(int)
	 */
	protected void keyPressed(int arg0) {
		interrupt(arg0);
	}
	
	
	/* (non-Javadoc)
	 * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
	 */
	public void commandAction(Command arg0, Displayable arg1) {
		if(arg0.getLabel().equals("Next"))
		{
			interrupt(canvas.getKeyCode(Canvas.FIRE));
		}else if(arg0.getLabel().equals("Exit"))
		{
			// clean the commands in Canvas.
			canvas.removeCommand(nextCommand);
			canvas.removeCommand(exitCommand);
			canvas.setCommandListener(null);
			// re-use this action.
			canvas.regRunAction(this);
			// notice allocation canvas: return to initial action.
			canvas.setGameStatus(CoreCanvas.GS_INITIAL);
		}
	}
}

⌨️ 快捷键说明

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