Go-lang剪刀石头布游戏

问题描述 投票:1回答:1

我们的项目是使用Go制作实用的剪刀石头布游戏。我认为这是一个很好的地方,可以针对我可能犯的一些明显的错误提出一些建议。

我有几个问题。

  1. 无论用户输入什么,程序都会说我总是输入“ rock”。>
  2. 无论我输入什么程序也总是告诉我这是一个“领带”
  3. 因此,对我来说,我的if / else语句经常出现问题,但我不确定它在哪里以及确切地在哪里。我也知道我的PlayerPlay函数很丑陋,但是由于某些原因,当我最初在其中显示菜单时,它将继续循环回到我的菜单,而无需继续执行程序的其余部分。

package main

import (
        "fmt"
        "math/rand"
        "time"
)

func ComputerPlay() int {

    return rand.Intn(2) + 1
}

func PlayerPlay(play int) int {

    fmt.Scanln(&play)

    return play
}

func PrintPlay(playerName string, play int) {

    fmt.Printf("%s picked ", playerName)

    if play == 0 {
        fmt.Printf("rock\n")
    } else if play == 1 {
        fmt.Printf("paper\n")
    } else if play == 2 {
        fmt.Printf("scissors\n")
    }



    fmt.Printf("Computer has chose ")
            switch ComputerPlay() {
            case 0:
                    fmt.Println("rock\n")
            case 1:
                    fmt.Println("paper\n")
            case 2:
                    fmt.Println("scissors\n")
}

}


func ShowResult(computerPlay int, humanPlay int){

    var play int
    computerPlay = ComputerPlay()
    humanPlay = PlayerPlay(play)

        if humanPlay == 0 && humanPlay == 0 {
        fmt.Printf("It's a tie\n")
    } else if humanPlay == 0 && computerPlay == 1 {
        fmt.Printf(" Rock loses to paper\n")
    }   else if humanPlay == 0 && computerPlay == 2 {
        fmt.Printf("Rock beats scissors\n")
    }   else if humanPlay == 1 && computerPlay == 0 {
        fmt.Printf(" Paper beats rock\n")
    }   else if humanPlay == 1 && computerPlay == 1 {
        fmt.Printf("It's a tie!\n")
    }   else if humanPlay == 1 && computerPlay == 2 {
        fmt.Printf("Paper loses to scissors\n")
    } else if humanPlay == 2 && computerPlay == 0 {
        fmt.Printf("Scissors loses to rock\n")
    } else if humanPlay == 2 && computerPlay == 1 {
        fmt.Printf(" Scissors beats paper\n")
    } else if humanPlay == 2 && computerPlay == 2 {
        fmt.Printf(" It's a tie!\n")
    }


}

func main() {
        rand.Seed(time.Now().UnixNano())

        fmt.Printf("Welcome to Rock, Paper, Scissors\n\n")
        fmt.Printf("What is your name?\n")
        var playerName string
        fmt.Scanln(&playerName)

        fmt.Printf("Choose\n")
        fmt.Printf("0. Rock\n")
        fmt.Printf("1. paper\n")
        fmt.Printf("2. scissors\n")
        fmt.Printf("Your choice -> ")
        var play int
        PlayerPlay(play)
        PrintPlay(playerName, play)

        var computerPlay int
        ComputerPlay()
        ShowResult(computerPlay, play)

}

我们的项目是使用Go制作实用的剪刀石头布游戏。我认为这是一个很好的地方,可以针对我可能犯的一些明显的错误提出一些建议。我有几个...

go
1个回答
0
投票

问题是您在PlayerPlay函数中使用了按值传递参数,而不是按引用传递参数。

© www.soinside.com 2019 - 2024. All rights reserved.