public class MakeWin { public static void main(String[] args) { /* a is a two-dimensional array, whose dimensions are 3 (rows) by 3 (columns). */ char[][] a = { {'X', 'O', 'X'}, {'X', ' ', 'O'}, {' ', ' ', 'O'} }; /* line is a three-dimensional array, whose dimensions are 8 by 3 by 2. line.length is 8, because a tic-tac-toe board has eight straight lines (3 columns + 3 rows + 2 diagonals). line[0].length, line[1].length, line[2].length, etc., are each 3, because each line has 3 squares. line[0][0].length, line[0][1].length, etc., are each 2, because each square has 2 coordinates (the row number and the column number). */ int[][][] line = { //column 0 { {0, 0}, //square in row 0, column 0 {1, 0}, //square in row 1, column 0 {2, 0} }, //column 1 { {0, 1}, {1, 1}, {2, 1} }, //column 2 { {0, 2}, {1, 2}, {2, 2} }, //row 0 { {0, 0}, {0, 1}, {0, 2} }, //row 1 { {1, 0}, {1, 1}, {1, 2} }, //row 2 { {2, 0}, {2, 1}, {2, 2} }, //upper left to lower right diagonal { {0, 0}, {1, 1}, {2, 2} }, //upper right to lower rleft diagonal { {0, 2}, {1, 1}, {2, 0} } }; //Is there a line where X could win? for (int li = 0; li < line.length; ++li) { int countX = 0; int countO = 0; int countB = 0; int brow = 0; //row & column number of blank. int bcol = 0; for (int i = 0; i < line[li].length; ++i) { /* line[li] is the line. line[li][i] is the square in the line. line[li][i][0] is row number of square. line[li][i][1] is col number of square. */ final int row = line[li][i][0]; final int col = line[li][i][1]; final char c = a[row][col]; //could say //a[line[li][i][0]][line[li][i][0]] //in place of the above a[row][col] if (c == 'X') { ++countX; } else if (c == 'O') { ++countO; } else if (c == ' ') { ++countB; brow = row; bcol = col; } else { System.err.println("a[" + row + "][" + col + "] == " + c); System.exit(1); } } if (countB == 1) { //Don't mention the number 2. if (countX == line[li].length - 1) { System.out.println( "Winning move for X at row " + brow + ", column " + bcol); } else if (countO == line[li].length - 1) { System.out.println( "Winning move for Y at row " + brow + ", column " + bcol); } } } } }