COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014
COMP 110: Spring Announcements Lab 6 has been graded
COMP 110: Spring Questions?
COMP 110: Spring Today in COMP 110 Some Misc. Items Multi-Dimensional Arrays Programming Demo TicTacToe
COMP 110: Spring Program 4 Some comments
COMP 110: Spring Program 4 Extended to Friday at 5pm to better match the other section
COMP 110: Spring Lab 7 Some comments on the reverse/wheel methods
COMP 110: Spring Lab 6
COMP 110: Spring Multi-Dimensional Arrays Section 7.5 in text
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 Can be used to create a table that looks like this
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]); }
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 columns 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
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 579
COMP 110: Spring Length field for 2D Arrays int[][] table = new int[4][3]; table.length gives the number of rows (4 in this case) table[0].length, table[1].length etc gives the number of columns (3 in this case)
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(); //jump to the next line after each row is complete }
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 done display game board display game menu (move, restart, quit) if(move) ask where to move, update game board if(winner || board full) –done = true else if(restart) reset the game else if(quit) exit
COMP 110: Spring Decomposition Public methods Constructor sets everything up void play() starts the game Private Methods void displayBoard() Displays the entire game board void displayRow(int row) Displays a single row of the game board Used by displayBoard() void displayMenu() Displays the options menu to the user boolean getMove() 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 winner(int lastR, int lastC) Checks if the move to lastR,lastC resulted in a winner Used by getMove() 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) Textbook