我们的项目是使用Go制作实用的剪刀石头布游戏。我认为这是一个很好的地方,可以针对我可能犯的一些明显的错误提出一些建议。
我有几个问题。
因此,对我来说,我的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制作实用的剪刀石头布游戏。我认为这是一个很好的地方,可以针对我可能犯的一些明显的错误提出一些建议。我有几个...
问题是您在PlayerPlay函数中使用了按值传递参数,而不是按引用传递参数。