Skip to content

Instantly share code, notes, and snippets.

@QuantumFractal
Created July 20, 2021 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuantumFractal/222658d42be2b4c5bc1a9ba8c9f8d419 to your computer and use it in GitHub Desktop.
Save QuantumFractal/222658d42be2b4c5bc1a9ba8c9f8d419 to your computer and use it in GitHub Desktop.
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