Created
May 19, 2025 20:23
-
-
Save senapk/9154764cf0485023ac8bc5784c9a354f to your computer and use it in GitHub Desktop.
Pokemon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
) | |
type Pokemon struct { | |
nome string | |
vida int | |
ataque int | |
defesa int | |
} | |
func (a *Pokemon) String() string { | |
return fmt.Sprintf("{nome:%v, vida %v}", a.nome, a.vida) | |
} | |
func (a *Pokemon) atacar(b *Pokemon) bool { | |
if a.vida <= 0 || b.vida <= 0 || a == b { | |
return false | |
} | |
dano := a.ataque - b.defesa | |
if dano <= 0 { | |
dano = 1 | |
} | |
fmt.Printf("%v(%v) esta atacando %v(%v) com dano %v\n", a.nome, a.vida, b.nome, b.vida, dano) | |
b.vida -= dano | |
if b.vida <= 0 { | |
fmt.Printf("%v morreu\n", b.nome) | |
} | |
return true | |
} | |
func existeAlguemVivo(pokemons []*Pokemon) bool { | |
for _, pokemon := range pokemons { | |
if pokemon.vida > 0 { | |
return true | |
} | |
} | |
return false | |
} | |
func contarVivos(pokemons []*Pokemon) int { | |
total := 0 | |
for _, pokemon := range pokemons { | |
if pokemon.vida > 0 { | |
total += 1 | |
} | |
} | |
return total | |
} | |
func main() { | |
pokemons := []*Pokemon{ | |
{nome: "Pickachu", ataque: 5, vida: 10, defesa: 3}, | |
{nome: "Charmander", ataque: 7, vida: 8, defesa: 2}, | |
{nome: "Bubasauro", ataque: 4, vida: 12, defesa: 4}, | |
} | |
for contarVivos(pokemons) > 1 { | |
atacante := pokemons[rand.Intn(3)] | |
apanhante := pokemons[rand.Intn(3)] | |
if atacante.atacar(apanhante) { | |
fmt.Println(pokemons[0], pokemons[1], pokemons[2]) | |
fmt.Print("----------------------------------------------------") | |
fmt.Scanln() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment