I tried to write a code in Go to make a mysterious number by the programm and it was be able to guess what number is automatically. However, it's always the same number find (and it's different of the mysterious number), could anyone help me to understand why?
The main.go:
package main
import (
"awesomeProject/magicalNumber/magicalNumberControler"
"awesomeProject/nombreMagique/nbrMagiqueController"
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
min := 0
max := 100
var nbrMagique = magicalNumberControler.RandomMagicalNumber(min, max)
var nbrTrouve = magicalNumberControler.RandomNewNumber(min, max)
nbrMagiqueController.MoreOrLess(nbrTrouve, nbrMagique)
fmt.Scanln()
}
the magicalNumber.go:
package magicalNumberControler
import (
"fmt"
"math/rand"
"time"
)
func RandomMagicalNumber(min int, max int) int {
rand.Seed(time.Now().UnixNano())
result := rand.Intn(max-min +1)+min
return result
}
func RandomNewNumber(min int, max int) int {
return rand.Intn(max-min +1)+min
}
func MoreOrLess(randomInt int, proposalInt int) string {
min := 0
max := 100
tries := 0
for {
if proposalInt == randomInt{
fmt.Printf("Bravo, vous avez trouvé le nombre %d en %d essai(s)\n", randomInt, tries)
break
}
if proposalInt > randomInt{
fmt.Printf("Le nombre à trouver est inférieur à %d\n", proposalInt)
proposalInt = RandomNewNumber(min, max) - proposalInt
tries++
continue
}
if proposalInt < randomInt{
fmt.Printf("Le nombre à trouver est supérieur à %d\n", proposalInt)
proposalInt = RandomNewNumber(min, max) - proposalInt
tries++
continue
}
}
return "fin de la partie"
}
Aucun commentaire:
Enregistrer un commentaire