Another Fail with coding with ChatGTP

Here is another example of using ChatGTP for a simple problem, and it returns compiling code that is totally wrong. I needed to get all the neighbors in a spiral pattern from a given starting point. I initially forgot to specify that it needed to be a spiral and that I wanted the code in Swift (the first time it generated Python, which I have omitted1).

I have a grid with a given amount of columns and rows. Given a tile position I want to get a list of all tile cooridantes in clockwise order around the given center tile

To generate a list of all tile coordinates surrounding a given center tile in a grid in a clockwise order, we can follow these steps:

  1. Identify the neighbors: First, identify the positions of all neighboring tiles around the center tile.
  2. Order them clockwise: Then, order these neighboring tiles in a clockwise direction, starting from a specific direction (usually top, or top-left).

Given a grid with rows and columns, and a center tile at position (center_row, center_column), here’s how to find the neighboring tiles in clockwise order:

Steps:

  1. Define the neighbors: The eight possible neighbors of a tile at (r, c) are:

    • Top: (r-1, c)
    • Top-right: (r-1, c+1)
    • Right: (r, c+1)
    • Bottom-right: (r+1, c+1)
    • Bottom: (r+1, c)
    • Bottom-left: (r+1, c-1)
    • Left: (r, c-1)
    • Top-left: (r-1, c-1)
  2. Filter out invalid neighbors: Some of these neighbors might be outside the grid bounds, so they need to be filtered out.

  3. Return the ordered list: The valid neighboring positions are then returned in the order specified above.

Please use swift for code generation, and the tile coordinates should be generated in a clockwise spiral form from the given tile at (r, c)

Certainly! To generate the coordinates in a clockwise spiral form from the given tile (r, c) in a grid using Swift, you'll want to iterate outward from the center tile, progressively increasing the radius of the spiral.

Here is a Swift implementation:

import Foundation

func getSpiralNeighbors(centerRow: Int, centerColumn: Int, rows: Int, columns: Int) -> [(Int, Int)] {
    var result: [(Int, Int)] = []
    
    // Add the center tile as the starting point
    result.append((centerRow, centerColumn))
    
    var distance = 1
    while result.count < rows * columns {
        // Spiral loop: up, right, down, left
        // Up
        for i in 0..<distance {
            let newRow = centerRow - distance + i
            let newCol = centerColumn + distance
            if newRow >= 0 && newRow < rows && newCol >= 0 && newCol < columns {
                result.append((newRow, newCol))
            }
        }
        // Left
        for i in 0..<distance {
            let newRow = centerRow + distance
            let newCol = centerColumn + distance - i
            if newRow >= 0 && newRow < rows && newCol >= 0 && newCol < columns {
                result.append((newRow, newCol))
            }
        }
        // Down
        for i in 0..<distance {
            let newRow = centerRow + distance - i
            let newCol = centerColumn - distance
            if newRow >= 0 && newRow < rows && newCol >= 0 && newCol < columns {
                result.append((newRow, newCol))
            }
        }
        // Right
        for i in 0..<distance {
            let newRow = centerRow - distance
            let newCol = centerColumn - distance + i
            if newRow >= 0 && newRow < rows && newCol >= 0 && newCol < columns {
                result.append((newRow, newCol))
            }
        }
        
        distance += 1
    }
    
    return result
}

// Example usage
let centerRow = 2
let centerColumn = 2
let rows = 5
let columns = 5

let neighbors = getSpiralNeighbors(centerRow: centerRow, centerColumn: centerColumn, rows: rows, columns: columns)
for neighbor in neighbors {
    print(neighbor)
}

Explanation:

  1. Start with the center tile: The algorithm starts by adding the center tile to the result list.

  2. Spiral expansion:

    • The distance variable tracks how far from the center you are expanding in each spiral iteration.
    • In each step, the algorithm moves in the order: Up, Left, Down, Right, while checking the bounds of the grid to ensure valid coordinates.
    • The distance is incremented in each spiral iteration, moving further from the center tile.
  3. Check bounds: For each step, the algorithm checks if the new coordinates are within the grid bounds (i.e., 0 <= row < rows and 0 <= col < columns).

Example:

If the grid is 5x5, and the center tile is (2, 2), the output will be the coordinates in a spiral order, starting from (2, 2) and expanding outward.

This method ensures that all tiles in the grid are covered in a spiral pattern around the given center tile.

The explanations are good and correct (or as accurate as needed to write the code yourself). However, the provided Swift code does not work. It compiles, but it never stops. And it also needs to be corrected. As it does not consider that the number of tiles grows with each iteration, I go up by one. And then I need to move left by one, but now I need to go down by three, not one, and the same is valid for going right. In the end, I ignored this code and wrote it myself :-(.


  1. See the pdf conversation for the full transcript. ↩︎

Comments

How to respond

Write your comment on your on page and link it to this page with the following link:
https://vmac.ch/posts/2024-08-17-another-fail-coding-with-chat-gtp/
Then insert the permalink to your post into the form below and submit it.

Alternatively you can reach me by email to: comment@vmac.ch