golang

Golang by example: Deactivate bombs hacking with golang

You are part of a secret mission. A terrorist planted 7 bombs in a mall and in order to avoid explosion, you are sent there to deactivate bombs and save the world.

gopherswrench

The mission starts with:

Status: Operational
7 bombs active.

Objective:
Activate and deactivate the bombs. Try not to explode yourself. o/

You win when console prints:

Status: Operational
All bombs deactivated!

Rules:
The code bellow can only be changed at: setupBombs(), testGrid(), BombGrid{} and Bomb{}.

package main

import (
 "fmt"
)

func main() {
 bombs := setupBombs()

 for i := 0; i < 7; i++ {
 if !bombs[i].isRunning {
 fmt.Println("You failed, BUM!!!!")
 return
 }
 }

 bombGrid := BombGrid{"Operational", bombs}

 testGrid(bombGrid)

 if bombGrid.status != "Operational" {
 fmt.Println("You failed, BUM!!!!")
 return
 }

 if activeBombs(bombGrid) > 0 {
 fmt.Println(activeBombs(bombGrid), "bombs active.")
 } else {
 fmt.Println("All bombs deactivated!")
 }
}

func setupBombs() [7]Bomb {
 var bombs [7]Bomb
 bombs[0] = Bomb{true}
 bombs[1] = Bomb{true}
 bombs[2] = Bomb{true}
 bombs[3] = Bomb{true}
 bombs[4] = Bomb{true}
 bombs[5] = Bomb{true}
 bombs[6] = Bomb{true}
 return bombs
}

func testGrid(bombGrid BombGrid) {
 fmt.Println("Status:", bombGrid.status)
}

type BombGrid struct {
 status string
 bombs [7]Bomb
}

type Bomb struct {
 isRunning bool
}

func activeBombs(bombGrid BombGrid) int {
 activeBombs := 0
 for _, bomb := range bombGrid.bombs {
 if bomb.isRunning {
 activeBombs++
 }
 }
 return activeBombs
}

 

online snippet: https://play.golang.org/p/D-6JKZnB0i

Have fun 🙂