在字符串数组中查找模式(Java)
大家好吗?我正在尝试创建一个简单的游戏,根据玩家的移动,一些角色会附加到他们的字符串中(对于第二个玩家也是如此)。游戏应该检测特定字符是否出现在这些字符串中,而不管它们出现在字符串中的什么位置。需要3个字符的组合才能取得进展
因此,例如,一个玩家可能做出的一个成功的动作就是在他们的字符串的某个地方添加字符“c”、“d”和“e”,比如“abcde”。当游戏在他们的字符串中检测到“cde”时,他们赢得了一些东西。但是,如果该玩家的字符串改为“ifc4_d'e”,则游戏不会检测到该胜利,尽管包含相同的字符
编辑:下面有三个代码段,但不要惊慌;后两种方法只是第一种,但数组和/或findWinPatterns()
方法略有改变
这是我最初的代码:
// The following is used for determining local/global wins.
static final String[][] winPatterns = new String[][] {
// Local board 1
{"!\"#", "$%&", "\'()", "!$\'", "\"%(", "#&)", "!%)", "\'%#"},
// Local board 2
{"*+,", "-./", "0:;", "*-0", "+.:", ",/;", "*.;", "0.,"},
// Local board 3
{"<=>", "?@A", "BCD", "<?B", "=@C", ">AD", "<@D", "B@>"},
// Local board 4
{"EFG", "HIJ", "KLM", "EHK", "FIL", "GJM", "EIM", "KIG"},
// Local board 5
{"NPQ", "RST", "UVW", "NRU", "PSV", "QTW", "NSW", "USQ"},
// Local board 6
{"YZ[", "\\]^", "`ab", "Y\\`", "Z]a", "[^b", "Y]b", "`]["},
// Local board 7
{"cde", "fgh", "ijk", "cfi", "dgj", "ehk", "cgk", "ige"},
// Local board 8
{"lmn", "opq", "rst", "lor", "mps", "nqt", "lpt", "rpn"},
// Local board 9
{"uvw", "xyz", "{}~", "ux{", "vy}", "wz~", "uy~", "{yw"},
// Global board
{"123", "456", "789", "147", "258", "369", "159", "357"}
};
// WIN DETECTION METHOD
// Flag variables
static boolean flag0, flag1, flag2, flag3, flag4, flag5, flag6, flag7, flag8 = true;
static void findWinPatterns(String score) {
for (int i = 0; i < winPatterns.length; i++) {
for (String pattern : winPatterns[i]) {
if (score.contains(pattern)) {
System.out.println("3 in a row!" + " i = " + i);
/*
switch (i) {
case 0:
if (flag0) {
System.out.println("[Player] has won Local Board 1!");
flag0 = false;
}
break;
case 1:
if (flag1) {
System.out.println("[Player] has won Local Board 2!");
flag1 = false;
}
break;
case 2:
if (flag2) {
System.out.println("[Player] has won Local Board 3!");
flag2 = false;
}
break;
case 3:
if (flag3) {
System.out.println("[Player] has won Local Board 4!");
flag3 = false;
}
break;
case 4:
if (flag4) {
System.out.println("[Player] has won Local Board 5!");
flag4 = false;
}
break;
case 5:
if (flag5) {
System.out.println("[Player] has won Local Board 6!");
flag5 = false;
}
break;
case 6:
if (flag6) {
System.out.println("[Player] has won Local Board 7!");
flag6 = false;
}
break;
case 7:
if (flag7) {
System.out.println("[Player] has won Local Board 8!");
flag7 = false;
}
break;
case 8:
if (flag8) {
System.out.println("[Player] has won Local Board 9!");
flag8 = false;
}
break;
case 9:
// WINNER DECLARED
break;
}
*/
}
}
}
}
// MAIN METHOD HERE
这是经过编译的,但是因为contains()
方法只检测精确的字符串,所以它不适合我的游戏。然后我试了一下:
// The following is used for determining local/global wins.
static final String[][] winPatterns = new String[][] {
// Local board 1
{"[!\"#]", "[$%&]", "[\'()]", "[!$\']", "[\"%(]", "[#&)]", "[!%)]", "[\'%#]"},
// Local board 2
{"[*+,]", "[-./]", "[0:;]", "[*-0]", "[+.:]", "[,/;]", "[*.;]", "[0.,]"},
// Local board 3
{"[<=>]", "[?@A]", "[BCD]", "[<?B]", "[=@C]", "[>AD]", "[<@D]", "[B@>]"},
// Local board 4
{"[EFG]", "[HIJ]", "[KLM]", "[EHK]", "[FIL]", "[GJM]", "[EIM]", "[KIG]"},
// Local board 5
{"[NPQ]", "[RST]", "[UVW]", "[NRU]", "[PSV]", "[QTW]", "[NSW]", "[USQ]"},
// Local board 6
{"[YZ\\[]", "[\\\\]^]", "[`ab]", "[Y\\`]", "[Z\\]a]", "[\\[^b]", "[Y\\]b]", "[`\\]\\[]"},
// Local board 7
{"[cde]", "[fgh]", "[ijk]", "[cfi]", "[dgj]", "[ehk]", "[cgk]", "[ige]"},
// Local board 8
{"[lmn]", "[opq]", "[rst]", "[lor]", "[mps]", "[nqt]", "[lpt]", "[rpn]"},
// Local board 9
{"[uvw]", "[xyz]", "[{}~]", "[ux{]", "[vy}]", "[wz~]", "[uy~]", "[{yw]"},
// Global board
{"[123]", "[456]", "[789]", "[147]", "[258]", "[369]", "[159]", "[357]"}
};
// WIN DETECTION METHOD
// Flag variables
static boolean flag0, flag1, flag2, flag3, flag4, flag5, flag6, flag7, flag8 = true;
static void findWinPatterns(String score) {
for (int i = 0; i < winPatterns.length; i++) {
for (String pattern : winPatterns[i]) {
if (score.matches(pattern)) {
System.out.println("3 in a row!" + " i = " + i);
// ... all the same stuff as before ...
}
}
}
}
// MAIN METHOD HERE
这也被编译了,但它不起作用,我想这是因为我的正则表达式很糟糕。我用this website来证实这一点,但修复它需要花费很多时间,而且看起来也一点都不干净。所以我选择使用Pattern和Matcher类来简化代码的阅读,并且。。。嗯:
// The following is used for determining local/global wins; here's where things get UGLY.
static final Pattern[][] winPatterns = new Pattern[][] {
// Local board 1
{Pattern.compile("!\"#", Pattern.LITERAL), Pattern.compile("$%&", Pattern.LITERAL), Pattern.compile("'()", Pattern.LITERAL), Pattern.compile("!$'", Pattern.LITERAL), Pattern.compile("\"%(", Pattern.LITERAL), Pattern.compile("#&)", Pattern.LITERAL), Pattern.compile("!%)", Pattern.LITERAL), Pattern.compile("'%#", Pattern.LITERAL)},
// Local board 2
{Pattern.compile("*+,", Pattern.LITERAL), Pattern.compile("-./", Pattern.LITERAL), Pattern.compile("0:;", Pattern.LITERAL), Pattern.compile("*-0", Pattern.LITERAL), Pattern.compile("+.:", Pattern.LITERAL), Pattern.compile(",/;", Pattern.LITERAL), Pattern.compile("*.;", Pattern.LITERAL), Pattern.compile("0.,", Pattern.LITERAL)},
// Local board 3
{Pattern.compile("<=>", Pattern.LITERAL), Pattern.compile("?@A", Pattern.LITERAL), Pattern.compile("BCD", Pattern.LITERAL), Pattern.compile("<?B", Pattern.LITERAL), Pattern.compile("=@C", Pattern.LITERAL), Pattern.compile(">AD", Pattern.LITERAL), Pattern.compile("<@D", Pattern.LITERAL), Pattern.compile("B@>", Pattern.LITERAL)},
// Local board 4
{Pattern.compile("EFG", Pattern.LITERAL), Pattern.compile("HIJ", Pattern.LITERAL), Pattern.compile("KLM", Pattern.LITERAL), Pattern.compile("EHK", Pattern.LITERAL), Pattern.compile("FIL", Pattern.LITERAL), Pattern.compile("GJM", Pattern.LITERAL), Pattern.compile("EIM", Pattern.LITERAL), Pattern.compile("KIG", Pattern.LITERAL)},
// Local board 5
{Pattern.compile("NPQ", Pattern.LITERAL), Pattern.compile("RST", Pattern.LITERAL), Pattern.compile("UVW", Pattern.LITERAL), Pattern.compile("NRU", Pattern.LITERAL), Pattern.compile("PSV", Pattern.LITERAL), Pattern.compile("QTW", Pattern.LITERAL), Pattern.compile("NSW", Pattern.LITERAL), Pattern.compile("USQ", Pattern.LITERAL)},
// Local board 6
{Pattern.compile("YZ[", Pattern.LITERAL), Pattern.compile("\\]^", Pattern.LITERAL), Pattern.compile("`ab", Pattern.LITERAL), Pattern.compile("Y\\`", Pattern.LITERAL), Pattern.compile("Z]a", Pattern.LITERAL), Pattern.compile("[^b", Pattern.LITERAL), Pattern.compile("Y]b", Pattern.LITERAL), Pattern.compile("`][", Pattern.LITERAL)},
// Local board 7
{Pattern.compile("cde", Pattern.LITERAL), Pattern.compile("fgh", Pattern.LITERAL), Pattern.compile("ijk", Pattern.LITERAL), Pattern.compile("cfi", Pattern.LITERAL), Pattern.compile("dgj", Pattern.LITERAL), Pattern.compile("ehk", Pattern.LITERAL), Pattern.compile("cgk", Pattern.LITERAL), Pattern.compile("ige", Pattern.LITERAL)},
// Local board 8
{Pattern.compile("lmn", Pattern.LITERAL), Pattern.compile("opq", Pattern.LITERAL), Pattern.compile("rst", Pattern.LITERAL), Pattern.compile("lor", Pattern.LITERAL), Pattern.compile("mps", Pattern.LITERAL), Pattern.compile("nqt", Pattern.LITERAL), Pattern.compile("lpt", Pattern.LITERAL), Pattern.compile("rpn", Pattern.LITERAL)},
// Local board 9
{Pattern.compile("uvw", Pattern.LITERAL), Pattern.compile("xyz", Pattern.LITERAL), Pattern.compile("{}~", Pattern.LITERAL), Pattern.compile("ux{", Pattern.LITERAL), Pattern.compile("vy}", Pattern.LITERAL), Pattern.compile("wz~", Pattern.LITERAL), Pattern.compile("uy~", Pattern.LITERAL), Pattern.compile("{yw", Pattern.LITERAL)},
// Global board
{Pattern.compile("123", Pattern.LITERAL), Pattern.compile("456", Pattern.LITERAL), Pattern.compile("789", Pattern.LITERAL), Pattern.compile("147", Pattern.LITERAL), Pattern.compile("258", Pattern.LITERAL), Pattern.compile("369", Pattern.LITERAL), Pattern.compile("159", Pattern.LITERAL), Pattern.compile("357", Pattern.LITERAL)}
};
// WIN DETECTION METHOD
// Flag variables
static boolean flag0, flag1, flag2, flag3, flag4, flag5, flag6, flag7, flag8 = true;
static void findWinPatterns(String score) {
for (int i = 0; i < winPatterns.length; i++) {
for (Pattern pattern : winPatterns[i]) {
Matcher matcher = pattern.matcher(score);
boolean matchFound = matcher.find();
if (matchFound) {
System.out.println("3 in a row!" + " i = " + i);
// ... again, all the same stuff as before ...
}
}
}
}
// MAIN METHOD HERE
这起作用了与我的初始代码完全相同。这里的顺序仍然很重要,我真的不知道为什么。我真的快要认输了,用org.apache.commons.lang3.StringUtils.containsAny()
只是快速地解决一些问题:switch
块目前是一个注释,因为现在用System.out.println
测试这个东西的功能更容易。它应该已经可以工作了,但不可否认是笨重的;9个布尔标志。。。如果你愿意,我也会接受你的建议
但无论如何,我非常感谢你能帮我解决这个难题。谢谢
# 1 楼答案
你问题的核心似乎是:
有几种方法可以做到这一点:
Regex:
用于检测字符串中所有
"abc"
的正则表达式是"^(?=.*a)(?=.*b)(?=.*c).*"
。您可以硬编码正则表达式,也可以从如下字符串构建它:使用集包含:
我希望第二个选项的性能会更快一些,但与第一个(regex)选项相比,进行比较的代码更容易理解
我把这些选项中的一个,或者你自己的,编织到你的项目中,留给你