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 // After all the moves, lets check if any of the rowboz collided with anything
for i, r := range game.robots { for i, r := range game.robots {
// Hit a player? Game over // Hit a player? Game over!
if game.onPlayer(r) { if game.onPlayer(r) {
game.gameover = 1 game.gameover = 1
return return
} }
// Robots mingling? Trash // Robots mingling? Trash!
if game.onRobot(i, r) { if game.onRobot(i, r) {
// Delete robot // Delete robot
@ -60,7 +60,7 @@ func (game *Game) moveRobots() {
} }
// Colided with Trash // Hugging Trash? More trash!
if game.onTrash(r) { if game.onTrash(r) {
// Delete robot // Delete robot
@ -72,9 +72,9 @@ func (game *Game) moveRobots() {
} }
func (game *Game) onRobot(index int, pos Position) bool { func (game *Game) onRobot(index int, pos Position) bool {
var found bool var found bool = false
for i, r := range game.robots { for i, r := range game.robots {
if index != i && pos.x == r.x && pos.y == r.y { if index != i && pos == r {
found = true found = true
} }
} }
@ -84,16 +84,27 @@ func (game *Game) onRobot(index int, pos Position) bool {
// TODO: improve this // TODO: improve this
func (game *Game) deleteRobot(robot int) { 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 { // This isn't going to work for the same reason,
if robot != i { // and it adds the issue that random robots will get deleted.
rowboz = append(rowboz, r) // var rowboz []Position
}
}
game.robots = nil // for i, r := range game.robots {
game.robots = rowboz // 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++ game.player.score++