From fa37a4740b8eb88a1304e56dd762f3c85a91f700 Mon Sep 17 00:00:00 2001 From: Sacha Ligthert <sacha@ligthert.net> Date: Wed, 6 Dec 2023 19:26:06 +0100 Subject: [PATCH] Cleaning up, followed by start of a rewrite of the `deleteRobot()` function. --- robots.go | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/robots.go b/robots.go index 699ac64..4ab98a2 100644 --- a/robots.go +++ b/robots.go @@ -43,13 +43,13 @@ func (game *Game) moveRobots() { // After all the moves, lets check if any of the rowboz collided with anything for i, r := range game.robots { - // Hit a player? Game over + // Hit a player? Game over! if game.onPlayer(r) { game.gameover = 1 return } - // Robots mingling? Trash + // Robots mingling? Trash! if game.onRobot(i, r) { // Delete robot @@ -60,7 +60,7 @@ func (game *Game) moveRobots() { } - // Colided with Trash + // Hugging Trash? More trash! if game.onTrash(r) { // Delete robot @@ -72,9 +72,9 @@ func (game *Game) moveRobots() { } func (game *Game) onRobot(index int, pos Position) bool { - var found bool + var found bool = false for i, r := range game.robots { - if index != i && pos.x == r.x && pos.y == r.y { + if index != i && pos == r { found = true } } @@ -84,16 +84,27 @@ func (game *Game) onRobot(index int, pos Position) bool { // TODO: improve this func (game *Game) deleteRobot(robot int) { - var rowboz []Position + // The following would work, if it wasn't for the fact you are iterating through an array, + // and deleting elements form that same array. So deleting item #15 isn't going to work + // when the array has been reduced to 13 elements. + // if robot < len(game.robots) { + // game.robots = append(game.robots[:robot], game.robots[robot+1:]...) + // } - for i, r := range game.robots { - if robot != i { - rowboz = append(rowboz, r) - } - } + // This isn't going to work for the same reason, + // and it adds the issue that random robots will get deleted. + // var rowboz []Position - game.robots = nil - game.robots = rowboz + // for i, r := range game.robots { + // if robot != i { + // rowboz = append(rowboz, r) + // } + // } + + // game.robots = nil + // game.robots = rowboz + + // Conclusion: I need to find a different way to do this properly while iterating to that same array. game.player.score++