如何从数据文件中正确加载 3x3 矩阵?

How to load a 3x3 matrix correctly from data file?

我正在做一个 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,  ][ ,  ,  ]

如何修改我的算法以使矩阵正确加载?谢谢!

我认为这行是错误的...

gamemat[line][column] = game.charAt(line * column);

应该是……

gamemat[line][column] = game.charAt(line * 3 + column);

此外,您可能希望循环数达到 3...

for(line = 0; line < 3; line++)...