Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
bruteforce dispatch
package main
import (
"fmt"
"reflect"
)
const NORMALLY_EFFECTIVE = "it's normally effective"
const SUPER_EFFECTIVE = "it's SUPER effective!"
const NOT_VERY_EFFECTIVE = "it's not very effective..."
type PokeType interface { f() }
type Fire struct {}
func (e Fire) f() {}
type Electric struct {}
func (e Electric) f() {}
type Pokemon struct {
name string
hp int
type_ PokeType
}
func fight(atk Pokemon, def Pokemon) {
fmt.Printf("%s attacked %s!, ", atk.name, def.name);
switch atk.type_.(type) {
case Electric:
switch def.type_.(type) {
case Fire:
fmt.Println(NORMALLY_EFFECTIVE)
case Electric:
fmt.Println(NOT_VERY_EFFECTIVE)
default:
fmt.Println("Type not found")
}
case Fire:
switch def.type_.(type) {
case Fire:
fmt.Println(NOT_VERY_EFFECTIVE)
case Electric:
fmt.Println(NORMALLY_EFFECTIVE)
default:
fmt.Println("Type not found")
}
fmt.Println("Attacker is fire!")
default:
fmt.Println("Type not found")
}
}
func main() {
pikachu := Pokemon{
name: "pickachu",
hp: 100,
type_: Electric{},
}
charizard := Pokemon{
name: "charizard",
hp: 100,
type_: Fire{},
}
fight(pikachu, charizard)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment