美文网首页
golang实战小案例---我想你猜小游戏

golang实战小案例---我想你猜小游戏

作者: __豆约翰__ | 来源:发表于2021-12-09 12:45 被阅读0次

main.go

package main

import (
    "math/rand"
    "time"
)

func main() {
    // generate a random seed
    rand.Seed(time.Now().UnixNano())

    a := newAnswer()
    m := newMenu(a)
    m.run()

}

animal.go

package main

type animal struct {
    name         string
    hasScales    bool
    hasTail      bool
    hasLegs      bool
    livesInWater bool
}

func fish() animal {
    return animal{
        name:         "fish",
        hasScales:    true,
        hasTail:      true,
        hasLegs:      false,
        livesInWater: true,
    }
}

func lizard() animal {
    return animal{
        name:         "lizard",
        hasScales:    true,
        hasTail:      true,
        hasLegs:      true,
        livesInWater: false,
    }
}

func crab() animal {
    return animal{
        name:         "crab",
        hasScales:    false,
        hasTail:      false,
        hasLegs:      true,
        livesInWater: true,
    }
}

answer.go

package main

import (
    "math/rand"
)

type answer struct {
    chosen animal
    all    []animal
}

// newAnswer returns an answer
func newAnswer() answer {
    var a answer
    a.all = []animal{
        fish(),
        lizard(),
        crab(),
    }
    // generate a random integer based on the length of the slice
    randomInteger := rand.Intn(len(a.all) - 1)
    // assign the chosen animal
    a.chosen = a.all[randomInteger]
    return a
}

func (a answer) name() string {
    return a.chosen.name
}

func (a answer) hasScales() string {
    if a.chosen.hasScales == true {
        return "Yes, this animal has scales."
    }
    return "No, this animal does not have scales."
}

func (a answer) hasTail() string {
    if a.chosen.hasTail == true {
        return "Yes, this animal does have a tail."
    }
    return "No, this animal does not have a tail."
}

func (a answer) hasLegs() string {
    if a.chosen.hasLegs == true {
        return "Yes, this animal does have legs."
    }
    return "No, this animal does not have legs."
}

func (a answer) livesInWater() string {
    if a.chosen.livesInWater == true {
        return "Yes, this animal lives in the water."
    }
    return "No, this animal does not live in the water."
}

menu.go

package main

import (
    "bufio"
    "fmt"
    "log"
    "os"
    "strconv"
)

type menu struct {
    scanner *bufio.Scanner
    answer  answer
    player  string
}

func newMenu(a answer) menu {
    return menu{answer: a, scanner: bufio.NewScanner(os.Stdin)}
}

func (m menu) run() {
    fmt.Println("Hello, what is your name?")

    m.scanner.Scan()
    if err := m.scanner.Err(); err != nil {
        log.Println(err)
        os.Exit(1)
    }
    m.player = m.scanner.Text()
    fmt.Printf("\n\n")
    fmt.Println("Hello", m.player, "let's play a game!")
    fmt.Println("I will think of an animal and you will try to guess what it is.")
    fmt.Printf("The possible answers are: ")
    for i := range m.answer.all {
        fmt.Printf("%s ", m.answer.all[i].name)
    }
    fmt.Printf("\n")
    m.top()
}

func (m menu) top() {
    for {
        fmt.Printf("\n\n")
        fmt.Println("What would you like to do?")
        fmt.Println("1) Ask a question about the animal")
        fmt.Println("2) Guess the animal")
        fmt.Println("3) Quit the program")
        m.scanner.Scan()
        if err := m.scanner.Err(); err != nil {
            log.Println(err)
            os.Exit(1)
        }

        switch m.scanner.Text() {
        case "1":
            m.question()
            continue
        case "2":
            m.guess()
            continue
        case "3":
            fmt.Printf("Good bye %s, thanks for playing!\n", m.player)
            os.Exit(1)
        default:
            fmt.Println("I'm sorry, that isn't one of your choices. Please try again.")
        }
    }
}

func (m menu) question() {
    for {
        fmt.Printf("\n\n")
        fmt.Println("What would you like to ask?")
        fmt.Println("1) Does the animal have scales?")
        fmt.Println("2) Does the animal have a tail?")
        fmt.Println("3) Does the animal have legs?")
        fmt.Println("4) Does the animal live in the water?")
        m.scanner.Scan()
        if err := m.scanner.Err(); err != nil {
            log.Println(err)
            os.Exit(1)
        }
        switch m.scanner.Text() {
        case "1":
            fmt.Println(m.answer.hasScales())
            return
        case "2":
            fmt.Println(m.answer.hasTail())
            return
        case "3":
            fmt.Println(m.answer.hasLegs())
            return
        case "4":
            fmt.Println(m.answer.livesInWater())
            return
        default:
            fmt.Printf("\n\n")
            fmt.Println("I'm sorry, I didn't understand that answer. " +
                "Please enter a number that corresponds with your question.")
        }
    }
}
func (m menu) guess() {
    for {
        fmt.Printf("\n\n")
        fmt.Println("What animal am I thinking of?")
        for i := range m.answer.all {
            fmt.Printf("%v) %s\n", i+1, m.answer.all[i].name)
        }
        m.scanner.Scan()
        if err := m.scanner.Err(); err != nil {
            log.Println(err)
            os.Exit(1)
        }
        i, err := strconv.Atoi(m.scanner.Text())
        if err != nil || i > len(m.answer.all) || i < 1 {
            fmt.Println("I'm sorry, I didn't understand that answer. Please enter an answer by typing in a number.")
            continue
        }

        if m.answer.all[i-1].name == m.answer.chosen.name {
            fmt.Printf("\n\n")
            fmt.Printf("That's right %s! I was thinking of %s\n", m.player, m.answer.chosen.name)
            return
        }
        fmt.Printf("\n\n")
        fmt.Printf("Sorry %s, that wasn't the animal I was thinking of.\n", m.player)
        return
    }
}

相关文章

网友评论

      本文标题:golang实战小案例---我想你猜小游戏

      本文链接:https://www.haomeiwen.com/subject/ucdffrtx.html