277294.ijbsn.asia
Maze.java
2002-01-19T16:00:00Z
2002-01-19T16:00:00Z
<br/><TEXTAREA name="code" class="java" rows="16" cols="100">import java.awt.Dimension;
/**
* Class Maze - private class for representing search space as a two-dimensional maze
*/
public class Maze {
public static short OBSTICLE = -1;
public static short START_LOC_VALUE = -2;
public static short GOAL_LOC_VALUE = -3;
private int width = 0;
private int height = 0;
public Dimension startLoc = new Dimension();
public Dimension goalLoc = new Dimension();
/**
* The maze (or search space) data is stored as a short integer rather than
* as a boolean so that bread-first style searches can use the array to store
* search depth. A value of -1 indicates a barrier in the maze.
*/
private short [][]maze;
public Maze(int width, int height) {
System.out.println("New maze of size " + width + " by " + height);
this.width = width;
this.height = height;
maze = new short[width+2][height+2];
for (int i=0; i<width+2; i++) {
for (int j=0; j<height+2; j++) {
maze[i][j] = 0;
}
}
for (int i=0; i<height+2; i++) {
maze[0][i] = maze[width+1][i] = OBSTICLE;
}
for (int i=0; i<width+2; i++) {
maze[i][0] = maze[i][height+1] = OBSTICLE;
}
/**
* Randomize the maze by putting up arbitray obsticals
*/
int max_obsticles = (width * height) / 3;
for (int i=0; i<max_obsticles; i++) {
int x = (int)(Math.random()*width);
int y = (int)(Math.random()*height);
setValue(x, y, OBSTICLE);
}
/**
* Specify the starting location
*/
startLoc.width = 0;
startLoc.height = 0;
setValue(0, 0, (short)0);
/**
* Specify the goal location
*/
goalLoc.width = width - 1;
goalLoc.height = height - 1;
setValue(width - 1, height - 1, GOAL_LOC_VALUE);
}
synchronized public short getValue(int x, int y) { return maze[x+1][y+1]; }
synchronized public void setValue(int x, int y, short value) { maze[x+1][y+1] = value; }
public int getWidth() { return width; }
public int getHeight() { return height; }
}
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
2002-01-19T16:00:00Z
MazeBreadthFirstSearch.java
2002-01-19T16:00:00Z
2002-01-19T16:00:00Z
<br/><TEXTAREA name="code" class="java" rows="16" cols="100">import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
/**
* Title: MazeBreadthFirstSearch<p>
* Description: Demo program for Java AI Programming<p>
* Copyright: Copyright (c) Mark Watson, Released under Open Source Artistic License<p>
* Company: Mark Watson Associates<p>
* @author Mark Watson
* @version 1.0
*/
public class MazeBreadthFirstSearch extends javax.swing.JFrame {
JPanel jPanel1 = new JPanel();
BreadthFirstSearchEngine currentSearchEngine = null;
public MazeBreadthFirstSearch() {
try {
jbInit();
} catch (Exception e) {
System.out.println("GUI initilization error: " + e);
}
currentSearchEngine = new BreadthFirstSearchEngine(10, 10);
repaint();
}
public void paint(Graphics g_unused) {
if (currentSearchEngine == null) return;
Maze maze = currentSearchEngine.getMaze();
int width = maze.getWidth();
int height = maze.getHeight();
System.out.println("Size of current maze: " + width + " by " + height);
Graphics g = jPanel1.getGraphics();
BufferedImage image = new BufferedImage(320, 320, BufferedImage.TYPE_INT_RGB);
Graphics g2 = image.getGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 0, 320, 320);
g2.setColor(Color.black);
for (int x=0; x<width; x++) {
for (int y=0; y<height; y++) {
short val = maze.getValue(x,y);
if ( val == Maze.OBSTICLE) {
g2.setColor(Color.lightGray);
g2.fillRect(6 + x * 28, 3 + y * 29, 29, 30);
} else if (val == Maze.START_LOC_VALUE || (x==0 && y==0)) {
g2.setColor(Color.blue);
g2.drawString("S", 16 + x * 28, 19 + y * 29);
} else if (val == Maze.GOAL_LOC_VALUE) {
g2.setColor(Color.red);
g2.drawString("G", 16 + x * 28, 19 + y * 29);
}
}
}
// redraw the path in black:
g2.setColor(Color.black);
Dimension [] path = currentSearchEngine.getPath();
for (int i=1; i< (path.length-1); i++) {
int x = path[i].width;
int y = path[i].height;
short val = maze.getValue(x,y);
g2.drawString("" + (path.length - i), 16 + x * 28, 19 + y * 29);
}
g.drawImage(image, 30, 40, 320, 320, null);
}
public static void main(String[] args) {
MazeBreadthFirstSearch mazeSearch1 = new MazeBreadthFirstSearch();
}
private void jbInit() throws Exception {
this.setContentPane(jPanel1);
this.setCursor(null);
this.setDefaultCloseOperation(3);
this.setTitle("MazeBreadthFirstSearch");
this.getContentPane().setLayout(null);
jPanel1.setBackground(Color.white);
jPanel1.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
jPanel1.setDoubleBuffered(false);
jPanel1.setRequestFocusEnabled(false);
jPanel1.setLayout(null);
this.setSize(370, 420);
this.setVisible(true);
}
}
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
2002-01-19T16:00:00Z
MazeDepthFirstSearch.java
2002-01-19T16:00:00Z
2002-01-19T16:00:00Z
<br/><TEXTAREA name="code" class="java" rows="16" cols="100">import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
/**
* Title: MazeDepthFirstSearch<p>
* Description: Demo program for Java AI Programming<p>
* Copyright: Copyright (c) Mark Watson, Released under Open Source Artistic License<p>
* Company: Mark Watson Associates<p>
* @author Mark Watson
* @version 1.0
*/
public class MazeDepthFirstSearch extends javax.swing.JFrame {
JPanel jPanel1 = new JPanel();
DepthFirstSearchEngine currentSearchEngine = null;
public MazeDepthFirstSearch() {
try {
jbInit();
} catch (Exception e) {
System.out.println("GUI initilization error: " + e);
}
currentSearchEngine = new DepthFirstSearchEngine(10, 10);
repaint();
}
public void paint(Graphics g_unused) {
if (currentSearchEngine == null) return;
Maze maze = currentSearchEngine.getMaze();
int width = maze.getWidth();
int height = maze.getHeight();
System.out.println("Size of current maze: " + width + " by " + height);
Graphics g = jPanel1.getGraphics();
BufferedImage image = new BufferedImage(320, 320, BufferedImage.TYPE_INT_RGB);
Graphics g2 = image.getGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 0, 320, 320);
g2.setColor(Color.black);
for (int x=0; x<width; x++) {
for (int y=0; y<height; y++) {
short val = maze.getValue(x,y);
if ( val == Maze.OBSTICLE) {
g2.setColor(Color.lightGray);
g2.fillRect(6 + x * 28, 3 + y * 29, 29, 30);
} else if (val == Maze.START_LOC_VALUE || val == 1) {
g2.setColor(Color.blue);
g2.drawString("S", 16 + x * 28, 19 + y * 29);
} else if (val == Maze.GOAL_LOC_VALUE) {
g2.setColor(Color.red);
g2.drawString("G", 16 + x * 28, 19 + y * 29);
} else if (val > 0) {
//g2.setColor(Color.green);
//g2.drawString("" + val, 16 + x * 28, 19 + y * 29);
}
}
}
// redraw the path in black:
g2.setColor(Color.black);
Dimension [] path = currentSearchEngine.getPath();
for (int i=1; i< path.length; i++) {
int x = path[i].width;
int y = path[i].height;
short val = maze.getValue(x,y);
g2.drawString("" + val, 16 + x * 28, 19 + y * 29);
}
g.drawImage(image, 30, 40, 320, 320, null);
}
public static void main(String[] args) {
MazeDepthFirstSearch mazeSearch1 = new MazeDepthFirstSearch();
}
private void jbInit() throws Exception {
this.setContentPane(jPanel1);
this.setCursor(null);
this.setDefaultCloseOperation(3);
this.setTitle("MazeDepthFirstSearch");
this.getContentPane().setLayout(null);
jPanel1.setBackground(Color.white);
jPanel1.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION);
jPanel1.setDoubleBuffered(false);
jPanel1.setRequestFocusEnabled(false);
jPanel1.setLayout(null);
this.setSize(370, 420);
this.setVisible(true);
}
}
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
2002-01-19T16:00:00Z