CSCE145 F2026 04 Arrays Part 03
Watch on YouTube →
Overview
Java multidimensional arrays are arrays of arrays: each dimension is indexed separately, and each inner array has its own length. The lesson applies these ideas in a 10×10 hide-and-seek game, using nested loops, character markers, random coordinates, movement validation, and squared-distance feedback, then introduces curly-brace initialization and ragged arrays.
Key takeaways
- A Java two-dimensional array is an outer array of references to inner arrays, so array.length and array[i].length can report different dimensions.
- Nested loops should use the current row’s length in the inner condition; this also makes traversal safe for ragged arrays.
- The hide-and-seek game maps grid coordinates to board[y][x], while keeping x and y as separate player and goal coordinates.
- Squared Euclidean distance can be compared against squared thresholds to classify proximity without calculating a square root.
- Validating dx and dy independently allows one coordinate change to be rejected without discarding a valid change on the other axis.
- Clamping both coordinates to 0–9 keeps movement within a 10×10 board, and a win requires both x and y to match the goal.
Chapters
0:00
Java 2D Arrays Are Arrays of Arrays
- Declare a two-dimensional integer array with a type such as int[][] and create it with dimensions such as new int[2][3].
- The outer array stores references to inner arrays; each cell is accessed with two indices, starting at 0.
- array.length gives the outer dimension, while array[i].length gives the length of the inner array at index i.
5:42
Traverse 2D Arrays with Nested for Loops
- Use an outer loop for row index i and an inner loop for column index j; the example fills each cell with i + j.
- The inner loop condition should use array[i].length, not array.length, so it checks the current row’s size.
- For a 2×3 array, the example produces rows [0, 1, 2] and [1, 2, 3].
10:17
Designing a 10×10 Hide-and-Seek Grid Game
- The game places a player and a randomly selected hidden goal on a 10×10 character board.
- The player can move in eight directions; visited positions become path markers, and moves are kept inside the board.
- Distance feedback classifies the player as getting colder, warmer, or hotter, and reaching the goal ends the game.
13:23
Set Up the Java Game, Coordinates, and Board
- The program imports java.util.Scanner for console input and java.util.Random for selecting goal coordinates.
- Constants define a board size of 10, squared cold and warm thresholds, and character markers for empty spaces, the player, the path, and a debug-only goal.
- The player starts at (0, 0); Random.nextInt(10) selects valid goal coordinates from 0 through 9.
- A char[10][10] board is filled with underscores using nested loops, then the player and hidden goal are placed using board[y][x].
24:27
Print the Board and Give Squared-Distance Hints
- Nested loops print each board row, using print for individual characters and println after each row.
- The game compares squared coordinate distance with squared thresholds, avoiding a square-root calculation.
- A distance above the cold threshold prints “getting colder”; otherwise, a distance above the warm threshold prints “getting warmer,” and closer positions print “getting hotter.”
30:10
Validate Moves, Clamp Coordinates, and Detect a Win
- Scanner reads dx and dy, each restricted to -1, 0, or 1; separate if statements validate the two independent inputs.
- The current cell becomes a path marker before px and py are updated by their respective deltas.
- Coordinate checks clamp x and y to the valid range 0–9, preventing the player from leaving the board.
- The game ends when both player coordinates match the goal coordinates; the demonstrated run reaches the hidden goal and prints a win message.
35:11
Initialize 2D Arrays and Create Ragged Rows
- Curly-brace initializers can provide all values directly, with nested braces representing the inner arrays.
- Java supports ragged arrays whose rows have different lengths; the example has three rows containing 5, 8, and 2 elements.
- Because row lengths can vary, traverse each row using a[i].length rather than assuming every inner array has the same size.
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, UofSC_CSCE145_CSCE146.