TicTacToe V2.0。
非要用1-9来输入的结果就是使用二维数组这件事的意义变得非常难找。
留个遗体,我要改回坐标输入了。
1 public class Game { 2 String chessBoard; 3 String[][] pieces = new String[3][3]; 4 5 /** 初始化棋盘样式和棋子数组。*/ 6 Game() { 7 chessBoard = 8 "-------------- \n" + 9 "| %s | %s | %s | \n" + 10 "-------------- \n" + 11 "| %s | %s | %s | \n" + 12 "-------------- \n" + 13 "| %s | %s | %s | \n" + 14 "-------------- \n"; 15 16 for (int i = 1, j = 0; j < 3; i++, j++) 17 pieces[0][j] = String.valueOf(i); 18 for (int i = 4, j = 0; j < 3; i++, j++) 19 pieces[1][j] = String.valueOf(i); 20 for (int i = 7, j = 0; j < 3; i++, j++) 21 pieces[2][j] = String.valueOf(i); 22 } 23 24 /** 棋子数组降维。*/ 25 String[] pieceList() { 26 String[] temp = new String[9]; 27 28 System.arraycopy(pieces[0], 0, temp, 0, 3); 29 System.arraycopy(pieces[1], 0, temp, 3, 3); 30 System.arraycopy(pieces[2], 0, temp, 6, 3); 31 32 //for (int i = 0, j = 0; i < 3; i++, j += 3) 33 // System.arraycopy(pieces[i], 0, temp, j, 3); 34 35 return temp; 36 } 37 38 /** 列表到棋子数组的反映射。*/ 39 int[] map(int i) { 40 int[] result = new int[2]; 41 for 42 } 43 44 /** 打印棋盘。*/ 45 void printChessBoard() { 46 System.out.printf(this.chessBoard, (Object[])this.pieceList()); 47 } 48 }