Lesson 2

Exploring the Chessboard of Kotlin: An Introduction to Multidimensional Arrays

Overview and Goal

Hey, welcome on board! Today's exciting journey explores multidimensional arrays in Kotlin, which are much like a chessboard in the world of programming. Our aim is to empower you to create, access, and modify these versatile arrays. So buckle up and let's get started!

Visualizing a Multidimensional Array

Imagine a multidimensional array as an array of arrays. It operates like a chessboard; each square, which can be identified by its row and column coordinates, represents a specific value or chess piece. Note that this 2D visualization doesn't limit us. Kotlin allows us to create arrays with 3, 4, or even more dimensions, naturally extending this concept.

Creating a Multidimensional Array

Kotlin provides the arrayOf() function for declaring multidimensional arrays. Here's how it works:

Kotlin
1fun main() { 2 val chessboard = arrayOf( // Start of the outer array 3 arrayOf("R", "N", "B", "Q", "K", "B", "N", "R"), // Each inner array represents a row on the chessboard 4 arrayOf("P", "P", "P", "P", "P", "P", "P", "P"), 5 arrayOf("", "", "", "", "", "", "", ""), 6 // ... 7 ) // End of the outer array 8 9 // Access and print first two rows of the chessboard 10 println("Row 1: ${chessboard[0].joinToString()}") // Outputs: Row 1: R, N, B, Q, K, B, N, R 11 println("Row 2: ${chessboard[1].joinToString()}") // Outputs: Row 2: P, P, P, P, P, P, P, P 12}

In our chessboard array abstraction, each row represents an inner array, the elements of which we can access using two indices.

Accessing Elements in a Multidimensional Array

Let's use our chessboard analogy to retrieve a piece from a particular location:

Kotlin
1fun main() { 2 val chessboard = arrayOf( 3 arrayOf("R", "N", "B", "Q", "K", "B", "N", "R"), 4 arrayOf("P", "P", "P", "P", "P", "P", "P", "P"), 5 arrayOf("", "", "", "", "", "", "", ""), 6 // ... 7 ) 8 9 val piece = chessboard[0][2] // Access 3rd piece at the first row 10 println(piece) // Outputs: B 11}

In the above example, piece holds "B" (representing a bishop in chess) at coordinates (0, 2) because, like most languages, Kotlin uses zero-indexed arrays.

Modifying Elements in a Multidimensional Array

Kotlin's mutable arrays allow for changes to elements, demonstrated as follows:

Kotlin
1fun main() { 2 val chessboard = arrayOf( 3 arrayOf("R", "N", "B", "Q", "K", "B", "N", "R"), 4 arrayOf("P", "P", "P", "P", "P", "P", "P", "P"), 5 arrayOf("", "", "", "", "", "", "", ""), 6 // ... 7 ) 8 9 chessboard[1][4] = "" // Make the initial spot of a pawn empty 10 chessboard[0][4] = "P" // Move pawn one square backwards 11 12 println("Row 2: ${chessboard[1].joinToString()}") // Outputs: Row 2: P, P, P, P, , P, P, P 13 println("Row 1: ${chessboard[0].joinToString()}") // Outputs: Row 1: R, N, B, Q, P, B, N, R 14}

While in Kotlin, the size of an existing array can't be changed, the above code showcases a simple chess pawn's movement by modifying the respective array elements.

Summary

Kudos to you on your first encounter with multidimensional arrays in Kotlin! We've understood their structure, created a chessboard analogy of an array, accessed a chess piece, and made a chess move. Essentially, we've learned how to create, access, and modify multidimensional arrays. Are you ready for some practice exercises to solidify these newly learned concepts? Let's dive in! Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.