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

📄 square.java

📁 Maze solving using java
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. *//* * frrmmaze.java * * Created on May 24, 2009, 6:35:01 PM */package maze;/** * * @author James Bond 007 */import java.util.HashSet;import java.util.Set;public class Square {	private int x;	private int y;	private boolean start;	private boolean end;	private double localCost;           // cost of getting from this square to goal	private double parentCost;          // cost of getting from parent square to this node	private double passThroughCost;     // cost of getting from the start to the goal	// through this square	public Maze maze;	private Set<Square> adjacencies = new HashSet<Square>();            //All Square around this sqare	private Square parent;	public Square(int x, int y, Maze maze) {		this.x = x;		this.y = y;		this.maze = maze;	}	public int getX() {		return x;	}	public void setX(int x) {		this.x = x;	}	public int getY() {		return y;	}	public void setY(int y) {		this.y = y;	}	public boolean isStart() {		return start;	}	public void setStart(boolean start) {		this.start = start;	}	public boolean isEnd() {		return end;	}	public void setEnd(boolean end) {		this.end = end;	}	public Set<Square> getAdjacencies() {		return adjacencies;	}	public void setAdjacencies(Set<Square> adjacencies) {		this.adjacencies = adjacencies;	}	public Square getParent() {		return parent;	}	public void setParent(Square parent) {		this.parent = parent;	}	public void randomAdjacencies() {            //Randomly generate Adjacencies area            int top = x - 1;            int bottom = x + 1;            int left = y - 1;            int right = y + 1;            if (bottom < maze.getRows()) {                    if (rndAdjacent()) {                            maze.getSquare(bottom, y).addAdjacency(this);                            this.addAdjacency(maze.getSquare(bottom, y));                    }            }            if (right < maze.getColumns()) {                    if (rndAdjacent()) {                            maze.getSquare(x, right).addAdjacency(this);                            this.addAdjacency(maze.getSquare(x, right));                    }            }	}        public void loadAdjacencies(int[][] map){            int top = x - 1;            int bottom = x + 1;            int left = y - 1;            int right = y + 1;            if(map[x][y]==1) return;            if (bottom < maze.getRows()) {                if (map[bottom][y]==0) {                        maze.getSquare(bottom, y).addAdjacency(this);                        this.addAdjacency(maze.getSquare(bottom, y));                }            }            if (right < maze.getColumns()) {                if (map[x][right] == 0) {                        maze.getSquare(x, right).addAdjacency(this);                        this.addAdjacency(maze.getSquare(x, right));                }            }            if(top > -1){                if (map[top][y]==0) {                        maze.getSquare(top, y).addAdjacency(this);                        this.addAdjacency(maze.getSquare(top, y));                }            }            if(left > -1){                if (map[x][left]==0) {                        maze.getSquare(x, left).addAdjacency(this);                        this.addAdjacency(maze.getSquare(x, left));                }            }        }        	public void addAdjacency(Square square) {		adjacencies.add(square);	}		public void removeAdjacency(Square square) {		adjacencies.remove(square);	}	public double getPassThrough(Square goal) {		if (isStart()) {			return 0.0;		}		return getLocalCost(goal) + getParentCost();	}	public double getLocalCost(Square goal) {		if (isStart()) {			return 0.0;		}                if(Global.heuristics == 1)//•	Manhattan                    localCost = 1.0 * (Math.abs(x - goal.getX()) + Math.abs(y - goal.getY()));                else                    localCost = Math.sqrt(Math.pow(x - goal.getX(), 2) + Math.pow(y - goal.getY(), 2));		return localCost;	}	public double getParentCost() {		if (isStart()) {			return 0.0;		}		if (parentCost == 0.0) {			parentCost = 1 + parent.getParentCost();		}		return parentCost;	}		public boolean rndAdjacent() {		if (Math.random() > .5) {			return true;		}		return false;	}}

⌨️ 快捷键说明

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