Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 869c6f8

Browse files
committed
Modified the maze search examples to use a new class Location (fields x,y in maze) instead of using the AWT Dimension class - I removed a 12 year old kluge!
1 parent a6455bc commit 869c6f8

File tree

11 files changed

+97
-78
lines changed

11 files changed

+97
-78
lines changed

src-search-maze/AbstractSearchEngine.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import java.awt.Dimension;
2-
31
/**
42
* 2D Maze Search
53
*
@@ -20,20 +18,20 @@ public AbstractSearchEngine(int width, int height) {
2018
public Maze getMaze() { return maze; }
2119
protected Maze maze;
2220
/**
23-
* We will use the Java type Dimension (fields width and height will
21+
* We will use the Java type Location (fields width and height will
2422
* encode the coordinates in x and y directions) for the search path:
2523
*/
26-
protected Dimension [] searchPath = null;
24+
protected Location [] searchPath = null;
2725
protected int pathCount;
2826
protected int maxDepth;
29-
protected Dimension startLoc, goalLoc, currentLoc;
27+
protected Location startLoc, goalLoc, currentLoc;
3028
protected boolean isSearching = true;
3129

3230
protected void initSearch() {
3331
if (searchPath == null) {
34-
searchPath = new Dimension[1000];
32+
searchPath = new Location[1000];
3533
for (int i=0; i<1000; i++) {
36-
searchPath[i] = new Dimension();
34+
searchPath[i] = new Location();
3735
}
3836
}
3937
pathCount = 0;
@@ -43,34 +41,34 @@ protected void initSearch() {
4341
searchPath[pathCount++] = currentLoc;
4442
}
4543

46-
protected boolean equals(Dimension d1, Dimension d2) {
47-
return d1.getWidth() == d2.getWidth() && d1.getHeight() == d2.getHeight();
44+
protected boolean equals(Location d1, Location d2) {
45+
return d1.x == d2.x && d1.y == d2.y;
4846
}
4947

50-
public Dimension [] getPath() {
51-
Dimension [] ret = new Dimension[maxDepth];
48+
public Location [] getPath() {
49+
Location [] ret = new Location[maxDepth];
5250
for (int i=0; i<maxDepth; i++) {
5351
ret[i] = searchPath[i];
5452
}
5553
return ret;
5654
}
57-
protected Dimension [] getPossibleMoves(Dimension loc) {
58-
Dimension tempMoves [] = new Dimension[4];
55+
protected Location [] getPossibleMoves(Location loc) {
56+
Location tempMoves [] = new Location[4];
5957
tempMoves[0] = tempMoves[1] = tempMoves[2] = tempMoves[3] = null;
60-
int x = loc.width;
61-
int y = loc.height;
58+
int x = loc.x;
59+
int y = loc.y;
6260
int num = 0;
6361
if (maze.getValue(x - 1, y) == 0 || maze.getValue(x - 1, y) == Maze.GOAL_LOC_VALUE) {
64-
tempMoves[num++] = new Dimension(x - 1, y);
62+
tempMoves[num++] = new Location(x - 1, y);
6563
}
6664
if (maze.getValue(x + 1, y) == 0 || maze.getValue(x + 1, y) == Maze.GOAL_LOC_VALUE) {
67-
tempMoves[num++] = new Dimension(x + 1, y);
65+
tempMoves[num++] = new Location(x + 1, y);
6866
}
6967
if (maze.getValue(x, y - 1) == 0 || maze.getValue(x, y - 1) == Maze.GOAL_LOC_VALUE) {
70-
tempMoves[num++] = new Dimension(x, y - 1);
68+
tempMoves[num++] = new Location(x, y - 1);
7169
}
7270
if (maze.getValue(x, y + 1) == 0 || maze.getValue(x, y + 1) == Maze.GOAL_LOC_VALUE) {
73-
tempMoves[num++] = new Dimension(x, y + 1);
71+
tempMoves[num++] = new Location(x, y + 1);
7472
}
7573
return tempMoves;
7674
}

src-search-maze/BreadthFirstSearchEngine.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import java.awt.Dimension;
2-
31
/**
42
* 2D Maze Search
53
*
@@ -23,8 +21,8 @@ private void doSearchOn2DGrid() {
2321
int height = maze.getHeight();
2422
boolean alReadyVisitedFlag[][] = new boolean[width][height];
2523
//float distanceToNode[][] = new float[width][height];
26-
Dimension predecessor[][] = new Dimension[width][height];
27-
DimensionQueue queue = new DimensionQueue();
24+
Location predecessor[][] = new Location[width][height];
25+
LocationQueue queue = new LocationQueue();
2826

2927
for (int i=0; i<width; i++) {
3028
for (int j=0; j<height; j++) {
@@ -34,19 +32,19 @@ private void doSearchOn2DGrid() {
3432
}
3533
}
3634

37-
alReadyVisitedFlag[startLoc.width][startLoc.height] = true;
35+
alReadyVisitedFlag[startLoc.x][startLoc.y] = true;
3836
//distanceToNode[startLoc.width][startLoc.height] = 0.0f;
3937
queue.addToBackOfQueue(startLoc);
4038
boolean success = false;
4139
outer:
4240
while (queue.isEmpty() == false) {
43-
Dimension head = queue.peekAtFrontOfQueue();
41+
Location head = queue.peekAtFrontOfQueue();
4442
if (head == null) break; // ??
45-
Dimension [] connected = getPossibleMoves(head);
43+
Location [] connected = getPossibleMoves(head);
4644
for (int i=0; i<4; i++) {
4745
if (connected[i] == null) break;
48-
int w = connected[i].width;
49-
int h = connected[i].height;
46+
int w = connected[i].x;
47+
int h = connected[i].y;
5048
if (alReadyVisitedFlag[w][h] == false) {
5149
//distanceToNode[w][h] = distanceToNode[w][h] + 1.0f;
5250
alReadyVisitedFlag[w][h] = true;
@@ -65,31 +63,31 @@ private void doSearchOn2DGrid() {
6563
if (success) {
6664
searchPath[maxDepth++] = goalLoc;
6765
for (int i=0; i<100; i++) {
68-
searchPath[maxDepth] = predecessor[searchPath[maxDepth - 1].width][searchPath[maxDepth - 1].height];
66+
searchPath[maxDepth] = predecessor[searchPath[maxDepth - 1].x][searchPath[maxDepth - 1].y];
6967
maxDepth++;
7068
if (equals(searchPath[maxDepth - 1], startLoc)) break; // back to starting node
7169
}
7270
}
7371
}
74-
protected class DimensionQueue {
75-
public DimensionQueue(int num) {
76-
queue = new Dimension[num];
72+
protected class LocationQueue {
73+
public LocationQueue(int num) {
74+
queue = new Location[num];
7775
head = tail = 0;
7876
len = num;
7977
}
80-
public DimensionQueue() {
78+
public LocationQueue() {
8179
this(400);
8280
}
83-
public void addToBackOfQueue(Dimension n) {
81+
public void addToBackOfQueue(Location n) {
8482
queue[tail] = n;
8583
if (tail >= (len - 1)) {
8684
tail = 0;
8785
} else {
8886
tail++;
8987
}
9088
}
91-
public Dimension removeFromFrontOfQueue() {
92-
Dimension ret = queue[head];
89+
public Location removeFromFrontOfQueue() {
90+
Location ret = queue[head];
9391
if (head >= (len - 1)) {
9492
head = 0;
9593
} else {
@@ -100,10 +98,10 @@ public Dimension removeFromFrontOfQueue() {
10098
public boolean isEmpty() {
10199
return head == (tail + 1);
102100
}
103-
public Dimension peekAtFrontOfQueue() {
101+
public Location peekAtFrontOfQueue() {
104102
return queue[head];
105103
}
106-
private Dimension [] queue;
104+
private Location [] queue;
107105
private int tail, head, len;
108106
}
109107
}

src-search-maze/DepthFirstSearchEngine.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import java.awt.Dimension;
2-
31
/**
42
* 2D Maze Search: Performs a depth first search in a maze
53
*
@@ -18,16 +16,16 @@ public DepthFirstSearchEngine(int width, int height) {
1816
iterateSearch(startLoc, 1);
1917
}
2018

21-
private void iterateSearch(Dimension loc, int depth) {
19+
private void iterateSearch(Location loc, int depth) {
2220
if (isSearching == false) return;
23-
maze.setValue(loc.width, loc.height, (short)depth);
24-
Dimension [] moves = getPossibleMoves(loc);
21+
maze.setValue(loc.x, loc.y, (short)depth);
22+
Location [] moves = getPossibleMoves(loc);
2523
for (int i=0; i<4; i++) {
2624
if (moves[i] == null) break; // out of possible moves from this location
2725
searchPath[depth] = moves[i];
2826
if (equals(moves[i], goalLoc)) {
29-
System.out.println("Found the goal at " + moves[i].width +
30-
", " + moves[i].height);
27+
System.out.println("Found the goal at " + moves[i].x +
28+
", " + moves[i].y);
3129
isSearching = false;
3230
maxDepth = depth;
3331
return;

src-search-maze/Location.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright Mark Watson 2008-2012. All Rights Reserved.
3+
*/
4+
5+
public class Location {
6+
int x, y;
7+
public Location(int x, int y) { this.x = x; this.y = y; }
8+
9+
public Location() {
10+
}
11+
}

src-search-maze/Maze.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import java.awt.Dimension;
2-
31
/**
4-
* Class Maze - class for representing search space as a two-dimensional maze
2+
* Class Maze - class for representing search space as a two-Locational maze
53
*/
64
public class Maze {
75
public static short OBSTICLE = -1;
86
public static short START_LOC_VALUE = -2;
97
public static short GOAL_LOC_VALUE = -3;
108
private int width = 0;
119
private int height = 0;
12-
public Dimension startLoc = new Dimension();
13-
public Dimension goalLoc = new Dimension();
10+
public Location startLoc = new Location();
11+
public Location goalLoc = new Location();
1412
/**
1513
* The maze (or search space) data is stored as a short integer rather than
1614
* as a boolean so that bread-first style searches can use the array to store
@@ -45,14 +43,14 @@ public Maze(int width, int height) {
4543
/**
4644
* Specify the starting location
4745
*/
48-
startLoc.width = 0;
49-
startLoc.height = 0;
46+
startLoc.x = 0;
47+
startLoc.y = 0;
5048
setValue(0, 0, (short)0);
5149
/**
5250
* Specify the goal location
5351
*/
54-
goalLoc.width = width - 1;
55-
goalLoc.height = height - 1;
52+
goalLoc.x = width - 1;
53+
goalLoc.y = height - 1;
5654
setValue(width - 1, height - 1, GOAL_LOC_VALUE);
5755
}
5856
synchronized public short getValue(int x, int y) { return maze[x+1][y+1]; }

src-search-maze/MazeBreadthFirstSearch.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ public void paint(Graphics g_unused) {
6868
}
6969
// redraw the path in black:
7070
g2.setColor(Color.black);
71-
Dimension [] path = currentSearchEngine.getPath();
71+
Location [] path = currentSearchEngine.getPath();
7272
for (int i=1; i< (path.length-1); i++) {
73-
int x = path[i].width;
74-
int y = path[i].height;
73+
int x = path[i].x;
74+
int y = path[i].y;
7575
short val = maze.getValue(x,y);
7676
g2.drawString("" + (path.length - i), 16 + x * 29, 19 + y * 29);
7777
}
@@ -80,7 +80,7 @@ public void paint(Graphics g_unused) {
8080
}
8181

8282
public static void main(String[] args) {
83-
MazeBreadthFirstSearch mazeSearch1 = new MazeBreadthFirstSearch();
83+
new MazeBreadthFirstSearch();
8484
}
8585

8686
private void jbInit() throws Exception {

src-search-maze/MazeDepthFirstSearch.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public void paint(Graphics g_unused) {
6666
}
6767
// redraw the path in black:
6868
g2.setColor(Color.black);
69-
Dimension [] path = currentSearchEngine.getPath();
69+
Location [] path = currentSearchEngine.getPath();
7070
for (int i=1; i< path.length; i++) {
71-
int x = path[i].width;
72-
int y = path[i].height;
71+
int x = path[i].x;
72+
int y = path[i].y;
7373
short val = maze.getValue(x,y);
7474
g2.drawString("" + val, 16 + x * 28, 19 + y * 29);
7575
}
@@ -78,7 +78,7 @@ public void paint(Graphics g_unused) {
7878
}
7979

8080
public static void main(String[] args) {
81-
MazeDepthFirstSearch mazeSearch1 = new MazeDepthFirstSearch();
81+
new MazeDepthFirstSearch();
8282
}
8383

8484
private void jbInit() throws Exception {

src-statistical-nlp/com/knowledgebooks/nlp/AutoTagger.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@
1414
* Associate pre-trained classification categories (tags) with input text: assigns
1515
* categories for news story types, technology category types, social information
1616
* types, etc. to input text.
17-
*
17+
*
1818
* <p/>
19-
* Copyright 2002-2008 by Mark Watson. All rights reserved.
19+
* Copyright 1998-2012 by Mark Watson. All rights reserved.
2020
* <p/>
21-
* This software is not public domain. It can be legally
22-
* used under either of the following licenses:
21+
* This software is can be used under either of the following licenses:
2322
* <p/>
24-
* 1. KnowledgeBooks.com Non Commercial Royalty Free License<br/>
25-
* 2. KnowledgeBooks.com Commercial Use License
23+
* 1. LGPL v3<br/>
24+
* 2. Apache 2
2625
* <p/>
27-
* see www.knowledgebooks.com for details
2826
*/
2927
public class AutoTagger {
3028
private static Hashtable<String, Hashtable<String, Float>> tagClasses;

src-statistical-nlp/com/knowledgebooks/nlp/ComparableDocument.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
package com.knowledgebooks.nlp;
22

3+
import com.knowledgebooks.nlp.util.NoiseWords;
34
import public_domain.Stemmer;
5+
46
import java.io.File;
57
import java.io.FileNotFoundException;
6-
import java.util.HashMap;
7-
import java.util.Iterator;
8-
import java.util.List;
9-
import java.util.Map;
10-
import java.util.Scanner;
11-
12-
import com.knowledgebooks.nlp.util.NoiseWords;
8+
import java.util.*;
139

1410
/**
1511
* This class stores stem count data for words in a document and provides
1612
* an API to compare the similarity between this document and another.
1713
*
1814
* @author Mark Watson
1915
*
16+
* <p/>
17+
* Copyright 1998-2012 by Mark Watson. All rights reserved.
18+
* <p/>
19+
* This software is can be used under either of the following licenses:
20+
* <p/>
21+
* 1. LGPL v3<br/>
22+
* 2. Apache 2
23+
* <p/>
24+
*
2025
*/
2126
public class ComparableDocument {
2227
private ComparableDocument() { } // disable default constructor calls

src-statistical-nlp/com/knowledgebooks/nlp/ExtractNames.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@
1818
* <p/>
1919
* Copyright 2002-2008 by Mark Watson. All rights reserved.
2020
* <p/>
21-
* This software is not public domain. It can be legally
22-
* used under the following license: LGPL version 3
21+
* <p/>
22+
* Copyright 1998-2012 by Mark Watson. All rights reserved.
23+
* <p/>
24+
* This software is can be used under either of the following licenses:
25+
* <p/>
26+
* 1. LGPL v3<br/>
27+
* 2. Apache 2
2328
* <p/>
2429
*/
2530
public class ExtractNames {

src-statistical-nlp/com/knowledgebooks/nlp/FastTag.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
* <p/>
1515
* Copyright 2002-2007 by Mark Watson. All rights reserved.
1616
* <p/>
17+
* <p/>
18+
* Copyright 1998-2012 by Mark Watson. All rights reserved.
19+
* <p/>
20+
* This software is can be used under either of the following licenses:
21+
* <p/>
22+
* 1. LGPL v3<br/>
23+
* 2. Apache 2
24+
* <p/>
1725
*/
1826
public class FastTag {
1927

0 commit comments

Comments
 (0)