我正在做一个Tic Tac Toe项目,在该项目中,我导入了X和O的9个字符串:
XOOXXOOXX
然后我必须将其拆分,加载到3x3矩阵中,然后测试并看看谁赢得了比赛。我已经编写了加载文件并运行游戏的代码,如下所示:
public static void main( String args[] ) throws IOException
{
Scanner importer = new Scanner(new File("testdata.dat"));
int count = importer.nextInt();
System.out.println(count + " games to test.");
for(int i = 0; i < count; i++) {
String game = importer.next();
TicTacToe gamecalc = new TicTacToe();
System.out.println(gamecalc.getWinner(game));
}
}
然而,当我尝试加载已经建立的3x3矩阵时,如下所示:
String gamedata = game; //gamedata is passed in, reassigned to game
for(int line = 0; line < 2; line++) {
for(int column = 0; column < 2; column++) {
gamemat[line][column] = game.charAt(line * column);
}
}
并且所述数据集是:
abcdefghi
jklmnopqr
stuvwxyz1
234567890
ABCDEFGHI
使用Arrays.toString打印时,结果是:
[a, a, ][a, b, ][ , , ] //each line is one 3x3 matrix
[j, j, ][j, k, ][ , , ]
[s, s, ][s, t, ][ , , ]
[2, 2, ][2, 3, ][ , , ]
[A, A, ][A, B, ][ , , ]
如何修改算法以正确加载矩阵?谢谢