Posts tagged: string

Points and Line of Sight

A very minor update, barely worth mention.

You’ll notice the entire wall segment is enclosed in the radius around the ‘player’, that yellow circle at the center. The significance here is that the top-left point of the wall segment isn’t included in the list of accessible nodes! Why not? Because, the player can’t see that node (it’s on the other side of the wall segment). Another good thing in the image above, the yellow dot is flush with the two red nodes meaning it has the same x-axis as the node to its left and same y-axis as the node above.

It’s all theory, but I believe I’m close to achieving my goal. A little recursion to jump from one point to every other point in its line of sight, adding together all the places that the orange ‘light’ can shine on, and I should finally have my map of accessible areas. Remember, it’s the idea of having a string anchored to a location, and then mapping out only the locations that the tethered string can reach.

It’ll all become clearer when I get it working. You’ll see.

Java String Validation and Scrubbing

I find myself more and more needing to validate user input, checking for illegal characters and empty Strings.

import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class ScrubTest {
 
	public String scrubText(String dirty) {
		dirty = scrub("\\s+", dirty, " ");
		return scrub("^\\s?|\\s?$", dirty, "");
	}
 
	public String scrub(String pattern, String text, String replace) {
		Pattern p = Pattern.compile(pattern);
		Matcher matcher = p.matcher(text);
		return matcher.replaceAll(replace);
	}
 
	public static void main(String[] args) {
 
		ScrubTest st = new ScrubTest();
		LinkedList<String> list = new LinkedList<String>();
 
		list.add(new String("test"));
		list.add(null);
		list.add(new String(""));
		list.add(new String(" "));
		list.add(new String("\n"));
 
		for (String s : list) {
			System.out.println ("\nTesting string: \"" + s + "\"\n---------------");
			try {
				s = st.scrubText(s);
				System.out.println ("null:  " + (s == null));
				System.out.println ("empty: " + s.isEmpty());
				System.out.println ("\\n:    " + s.equalsIgnoreCase("\n"));
			}
			catch (Exception e) {
				System.out.println ("ERROR:" + e);
			}
		}
 
		String scrubbed = "\n\n\n   This   is       just a  test            \n    ";
		scrubbed = st.scrubText(scrubbed);
 
		System.out.println (scrubbed + ".");
	}
}

Produces:

Testing string: "test"
---------------
null:  false
empty: false
\n:    false

Testing string: "null"
---------------
ERROR:java.lang.NullPointerException

Testing string: ""
---------------
null:  false
empty: true
\n:    false

Testing string: " "
---------------
null:  false
empty: true
\n:    false

Testing string: "
"
---------------
null:  false
empty: true
\n:    false
This is just a test.

Staypressed theme by Themocracy