Go语言实现猜拳游戏:带你轻松玩转代码
package main
import ( "fmt" "math/rand" "time" )
func main() { var userScore, computerScore int options := []string{'石头', '剪刀', '布'} rand.Seed(time.Now().UnixNano())
fmt.Println('欢迎来到猜拳游戏!')
for {
fmt.Println('请输入你的选项:1.石头 2.剪刀 3.布')
var userOption int
fmt.Scanln(&userOption)
if userOption < 1 || userOption > 3 {
fmt.Println('输入错误,请重新输入!')
continue
}
computerOption := rand.Intn(3) + 1
fmt.Printf('你出了%s,电脑出了%s\n', options[userOption-1], options[computerOption-1])
if userOption == computerOption {
fmt.Println('平局!')
} else if (userOption == 1 && computerOption == 2) || (userOption == 2 && computerOption == 3) || (userOption == 3 && computerOption == 1) {
fmt.Println('你赢了!')
userScore++
} else {
fmt.Println('电脑赢了!')
computerScore++
}
fmt.Printf('当前得分:你:%d,电脑:%d\n', userScore, computerScore)
if userScore > computerScore {
fmt.Println('你目前处于领先地位!')
} else if userScore < computerScore {
fmt.Println('电脑目前处于领先地位!')
} else {
fmt.Println('目前双方得分相等!')
}
}
}
原文地址: http://www.cveoy.top/t/topic/ojHU 著作权归作者所有。请勿转载和采集!