Cleaning up, followed by start of a rewrite of the deleteRobot() function.

This commit is contained in:
Sacha Ligthert 2023-12-06 19:26:06 +01:00
parent 85ca0b0f86
commit fa37a4740b

View File

@ -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++