COMP 110: Spring Announcements Lab 7 was due today Binary Expression Assignment due Friday
COMP 110: Spring Today in COMP 110 Multi-Dimensional Arrays Programming Demo TicTacToe
COMP 110: Spring D Arrays Arrays having more than one index are often useful Tables Grids Bingo games? 0: Open1: High2: Low3: Close 0: Apple Inc : Walt Disney Co : Google Inc : Microsoft Corp
COMP 110: Spring Creating 2D Arrays //create a 2D array with 4 rows and 3 columns int[][] table = new int[4][3]; or int[][] table; table = new int[4][3]; rows columns
COMP 110: Spring Creating 2D Arrays int[][] table = new int[4][3]; gives you access to table[0][0] //1 st row, 1 st column table[0][1] //1 st row, 2 nd column table[0][2] //1 st row, 3 rd column table[1][0] table[1][1] table[1][2] table[2][0] table[2][1] table[2][2] table[3][0] //4 th row, 1 st column table[3][1] //4 th row, 2 nd column table[3][2] //4 th row, 3 rd column table[0][0]table[0][1]table[0][2] table[1][0]table[1][1]table[1][2] table[2][0]table[2][1]table[2][2] table[3][0]table[3][1]table[3][2]
COMP 110: Spring Using 1D Arrays We used a single loop to process a 1D array int[] scores = { 13, 57, 93, 60, 102 }; for(int i = 0; i < scores.length; i++) { System.out.println(scores[i]); } 6
COMP 110: Spring Using 2D Arrays When processing 2D arrays, we usually used two nested loops int[][] table = new int[4][3]; //the outer loop iterates over the rows for(int row = 0; row < 4; row++) { //the inner loop iterates over the rows for(int column = 0; column < 3; column++) { table[row][column] = 0; }
COMP 110: Spring Multi-Dimensional Arrays You can have more than two dimensions int[][][] table = new int[4][3][5]; Use more nested loops to access all elements 8
COMP 110: Spring Representation of 2D Arrays int[] scores = new int[5]; scores is a one-dimensional array The type of scores[0], scores[1] etc is int int[][] table = new int[4][3]; Internally table is also represented as a one- dimensional array The type of table[0], table[1] etc is int[] (an array!) We still refer to table as a two-dimensional array
COMP 110: Spring Representation of 2D Arrays int[][] table = new int[4][3]; table[0] table[1] table[2] table[3] table
COMP 110: Spring Length field for 2D Arrays int[][] table = new int[4][3]; table.length gives the number of rows (4) table[0].length, table[1].length etc gives the number of columns (3)
COMP 110: Spring Multi-Dimensional Arrays as Parameters //a method to print a 2D array to screen public void print2DArray(int[][] arr) { for(int row = 0; row < arr.length; row++) { for(int column = 0; column < arr[row].length; column++) { System.out.print(arr[row][column] + " "); } System.out.println(); }
COMP 110: Spring Multi-Dimensional Arrays as Return Types //create a 2D array of the specified size and return it public int[][] create2DArray(int rows, int columns) { int[][] array = new int[rows][columns]; return array ; }
COMP 110: Spring Programming Demo TicTacToe Create a class that allows the user to play a game of TicTacToe
COMP 110: Spring TicTacToe Represent the game board as a 2D array of size 3x3 Type? char XOO OXX XXO
COMP 110: Spring Pseudocode Until game over display game board ask user for move until valid one is entered make move check for a winner check if board full
COMP 110: Spring Decomposition Public methods Constructor sets everything up void play() starts the game Possible Methods void displayBoard() Displays the entire game board boolean askForInput() Allows the user to update the game board by making a move Returns true if the move resulted in a winner and false if not boolean isMoveValid(int row, int column) Return whether or not the move is valid int getWinner() boolean boardFull() Returns whether the board is full and the game is over
COMP 110: Spring Programming Demo Programming
COMP 110: Spring Friday Recitation Bring Laptop (fully charged)