//let's make a 2d int array that stores the following: 1 2 3 4 5 6 int[][] a = new int[2][3] for (int j=0; j<=1; j++){ for (int i =0; i<=2; i++){ a[j][i] = i+1 + j*3; } } //change number j<1 and i<2 in the above for-loops to M and N to get a M*N array with number 1,2,3,...,M*N layout in rows. //printing the 2d array on screen: for (int j=0; j< a.length; j++){ for (int i =0; i< a[j].length; i++){ //each row per line, with space between elements System.out.print(a[j][i] + " "); } System.out.println(); } //recall the array a above: // 1 2 3 // 4 5 6 // we can change the first row as follows: a[0] = {1,2,3, 10, 12, 17, 20} //now we get: 1 2 3 10 12 17 20 4 5 6 //so it's not a sqare array anymore a[0][4] a[1][4] //there is not 5th element on the second row! a.length a[0].length //7 elements in the first row a[1].length //3 element in the second row: //ok for the next few lectures, we will write a Tic-Tac-Toe game to practice with everything we learned so far. //we will have the game both in the text mode and in GUI form! //first we need to develop the "game engine" //we need a square grid to put 'X' and 'O' on it. //char[][] a: a is an array of array of int TicTacToe t = new TicTacToe(); /*Let's make the state of the board as follows X O X */ t.setMark('X', 0, 0) t.setMark('O', 0, 2) t.setMark('X',1, 1) /*let's write a toString method that makes a nice String out of the board: X| |O - - - |X| - - - | | in string form it actually: "X| |O\n- - -\n |X|\n - - -\n | | " */ t.toString() t.toString() t.toString() TicTacToe t = new TicTacToe(); t.toString() t.setMark('X', 0,0) System.out.println(t.tiString) //or System.out.println(t) t.setMark('O', 0, 2) System.out.println(t) t.setMark('X',1, 1) System.out.println(t)