Lesson 4
Finding Ideal Game Piece Positions in Go
Introduction

Hello, and welcome, code explorer! Today's journey takes us through the intricate paths within a 2-dimensional array, often likened to a game board. Our mission is to identify ideal spots for game piece placement. Does it sound like an adventure? Let's embark!

Task Statement

Visualize a chessboard in the form of a 2D array, where each cell could be marked E for empty or P for a piece. Our quest involves summoning a Go function named FindPositions(). Upon examining this 2D array, this function identifies all the spots where a new piece could be placed so that it can move to another empty cell in one move. The catch is that a piece can move only to an immediately neighboring cell directly above, below, to the left, or to the right, but not diagonally.

Consider this 4x4 board, for instance:

1P E E P 2E P E P 3P E P P 4P E P E

The function should output the following pairs representing positions: (0, 1), (0, 2), (1, 2), (2, 1), (3, 1). This output represents the positions where a new piece can fit perfectly and then be able to move in the next turn.

Solution Building: Step 1

Stepping right into action, we start with an empty positions slice to help us log the sought positions. Understanding the dimensions of our "board" paves the way for defining boundaries in our exploration mission. Now, how does one determine the size of a 2D array in Go? The answer lies in the len function.

Using a slice is crucial here because Go slices are dynamic and can resize themselves to accommodate any number of elements, making it a perfect choice for storing an unknown number of positions.

Go
1package main 2 3import ( 4 "fmt" 5) 6 7func FindPositions(board [][]rune) [][2]int { 8 var positions [][2]int 9 10 rows := len(board) 11 cols := len(board[0]) 12 // Further exploration follows
Solution Building: Step 2

With our boundary map, we begin our expedition across the board. We use two nested for loops to do this, traversing the entire board one cell at a time.

Go
1package main 2 3import ( 4 "fmt" 5) 6 7func FindPositions(board [][]rune) [][2]int { 8 var positions [][2]int 9 10 rows := len(board) 11 cols := len(board[0]) 12 13 for i := 0; i < rows; i++ { 14 for j := 0; j < cols; j++ { 15 // ensuing exploration
Solution Building: Step 3

What's the plan for each cell, you ask? While exploring each cell, our trusty Go code inspects whether the cell is empty. If confirmed, it then peeks into the neighbors in the up, down, left, and right directions. If another vacant cell (E) is spotted, we jot down the main cell's position in our positions slice.

Go
1package main 2 3import ( 4 "fmt" 5) 6 7func FindPositions(board [][]rune) [][2]int { 8 var positions [][2]int 9 10 rows := len(board) 11 cols := len(board[0]) 12 13 for i := 0; i < rows; i++ { 14 for j := 0; j < cols; j++ { 15 if board[i][j] == 'E' { 16 if (i > 0 && board[i-1][j] == 'E') || 17 (i < rows-1 && board[i+1][j] == 'E') || 18 (j > 0 && board[i][j-1] == 'E') || 19 (j < cols-1 && board[i][j+1] == 'E') { 20 positions = append(positions, [2]int{i, j}) 21 } 22 } 23 } 24 } 25 return positions 26} 27 28func main() { 29 board := [][]rune{ 30 {'P', 'E', 'E', 'P'}, 31 {'E', 'P', 'E', 'P'}, 32 {'P', 'E', 'P', 'P'}, 33 {'P', 'E', 'P', 'E'}, 34 } 35 36 positions := FindPositions(board) 37 38 for _, pos := range positions { 39 fmt.Printf("(%d, %d)\n", pos[0], pos[1]) 40 } 41} 42 43// The output will be: 44// (0, 1) 45// (0, 2) 46// (1, 2) 47// (2, 1) 48// (3, 1)
Lesson Summary

Neatly wrapped up, haven't we? Today we engaged in a thrilling treasure hunt and emerged victorious with the positions slice. This journey revealed the importance of understanding 2D arrays and effectively maneuvering through them using Go. Striking a balance between precision and caution, we cleverly avoided unwanted recursion and harnessed the power of conditional statements to devise an efficient blueprint for our code.

Now that we've mastered the theory, we step into the realm of application — the practice field. Venture forth and make your mark by solving correlated challenges. And remember, persistence is the best companion for a coder. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.