Java Regex checking valid chars for a maze -
this check needs ensure lines file begin wall , have @ 1 entrance, @ most, 1 exit, , end wall. split conditions can test them. fails cs = "wwww", "wxw", "wwwwwwwwrwwwwww"
/** * check ensure lines composed of walls, * entrance, exit, , space */ protected boolean invalidmazecharacters(charsequence cs) { p = pattern.compile("^w+(r??w*|x??|w*|\\s*w*)w+$"); m = p.matcher(cs); if (!m.matches()) { return true; } return false; }
try
^w+(r?w*x?|x?w*r?)w+$
it's not efficient, can work on improving it. :) also, i'm not sure rules spaces are....
addendum
^w[w\s]*(?:r?[w\s]*x?|x?[w\s]*r?)[w\s]*w$
don't forget, if using java string, double backslashes.
also, fact spaces can appear anywhere leads me suspect non-regex-based function fine, too. can iterate through sequence, counting rs , xs, ignoring spaces, making sure first , last characters ws, , returning false
see bad character or r or w.
Comments
Post a Comment