2
我工作的一個井字棋的項目,我在其中輸入X和O年代的9個字符的字符串:如何從數據文件正確加載3x3矩陣?
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, ][ , , ]
我該如何重做我的算法才能正確加載矩陣?謝謝!