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

📄 robot.java

📁 俄罗斯方块,机器人,华容道破解,文件格式(PE,BMP),邮箱硬盘,日历图片
💻 JAVA
字号:
package com.bokee.nicend.boxgame.main;

import java.util.LinkedList;
import java.util.List;

import com.bokee.nicend.boxgame.game.Box;
import com.bokee.nicend.boxgame.gui.GamePanel;

/**
 * 机器人
 * 
 * @author Butnet
 */
public abstract class Robot {
	/**
	 * 游戏环境
	 */
	private GamePanel panel;
	private boolean stop = false;
	private Thread thread = null;
	private List<Box> boxs = new LinkedList<Box>();

	public Robot(GamePanel panel) {
		this.panel = panel;
	}
	
	/**
	 * 取得游戏地图的宽度
	 * @return 宽度
	 */
	protected final int getGamePanelWidth(){
		return panel.getBoxCol();
	}
	
	/**
	 * 取得游戏地图的高度
	 * @return 高度
	 */
	protected final int getGamePanelHeight(){
		return panel.getBoxRow();
	}

	/**
	 * 判断游戏中的(x,y)是否为空
	 * @param x X坐标
	 * @param y Y坐标
	 * @return 空返回true,有方块返回false;
	 */
	protected final boolean isSpace(int x, int y) {
		return panel.isSpace(x, y);
	}

	/**
	 * 取得游戏中的环境数据
	 * @return 环境数组 保存游戏面板是的空值
	 */
	protected final boolean[][] getData() {
		return panel.copyData();
	}

	/**
	 * 将游戏中的方块向左移动
	 */
	protected final void moveLeft() {
		panel.action('L');
	}

	/**
	 * 将游戏中的方块向右移动
	 */
	protected final void moveRight() {
		panel.action('R');
	}

	/**
	 * 将游戏中的方块变形
	 */
	protected final void moveUp() {
		panel.action('U');
	}

	/**
	 * 将游戏中的方块向下移动
	 */
	protected final void moveDown() {
		panel.action('D');
	}

	/**
	 * 将游戏中的方块向下移动直到底
	 */
	protected final void moveSpace() {
		panel.action('S');
	}

	/**
	 * 机器人处理一个方块,需要子类实现,子类通过实现这个方法,其实对智能判断,并移动方块到指定位置,完成一个方块的移动
	 * @param box 需要处理的方块
	 */
	public abstract void doBox(Box box);

	/**
	 * 当游戏产生一个新方块时调用
	 * @param box 需要机器人处理的方块
	 */
	public final void start(final Box box) {
		if (thread == null) {
			thread = new Thread() {
				public void run() {
					while (!stop) {
						if (boxs.size() > 0) {
							while (boxs.size() > 0) {
								doBox(boxs.get(0));
								boxs.remove(0);
							}
						}
						synchronized (boxs) {
							try {
								boxs.wait();
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}
			};
			boxs.add(box);
			thread.start();
		} else {
			synchronized (boxs) {
				boxs.add(box);
				boxs.notifyAll();
			}
		}
	}

	/**
	 * 暂停机器人
	 */
	public final void stop() {
		if (thread != null) {
			stop = true;
			synchronized (boxs) {
				boxs.notifyAll();
			}
			if(thread.isAlive())
				thread.interrupt();
		}
	}
}

⌨️ 快捷键说明

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