我在想是不是要把这个题再写成扫雷那样的矩阵判断,想换个feel,那就用Set来处理。
众所周知Set有好处,查找快,虽说这只是九宫格三子棋,但contains()就很快啊……
获胜条件是先凑齐以下八种的任意一种:
用contains()即可……
就当这是一种新的思路吧……
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
private static boolean winJudge(Set<Integer> set) {
if ((set.contains(1)) && ((set.contains(2) && set.contains(3)) || (set.contains(4) && set.contains(7)) ||
(set.contains(5) && set.contains(9)))) {
return true;
}
if ((set.contains(3)) && ((set.contains(6) && set.contains(9)) || (set.contains(5) && set.contains(7)))) {
return true;
}
if (set.contains(2) && set.contains(5) && set.contains(8)) {
return true;
}
if (set.contains(4) && set.contains(5) && set.contains(6)) {
return true;
}
return set.contains(7) && set.contains(8) && set.contains(9);
}
public static void main(String[] args) {
Set<Integer> xiaoa_set = new HashSet<>();
Set<Integer> uim_set = new HashSet<>();
Scanner scanner = new Scanner(System.in);
char[] sequence = scanner.nextLine().toCharArray();
scanner.close();
for (int i = 0; i < sequence.length; i++) {
if ((i & 1) == 0) {
xiaoa_set.add(sequence[i]-48);
if (winJudge(xiaoa_set)) {
System.out.println("xiaoa wins.");
return;
}
} else {
uim_set.add(sequence[i]-48);
if (winJudge(uim_set)) {
System.out.println("uim wins.");
return;
}
}
}
System.out.println("drew.");
}
}