利用GO语言编译猜拳游戏并统计赢得次数,要求打印一个游戏主界面
时间: 2024-02-11 08:08:38 浏览: 20
好的,这是一个简单的利用GO语言编写的猜拳游戏,同时可以统计赢得次数。以下是主界面的代码:
```
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
var choice int
var count int
rand.Seed(time.Now().UnixNano())
for {
fmt.Println("欢迎来到猜拳游戏\n")
fmt.Println("1. 石头")
fmt.Println("2. 剪刀")
fmt.Println("3. 布")
fmt.Println("4. 退出游戏\n")
fmt.Println("请出拳:")
fmt.Scanln(&choice)
if choice == 4 {
break
}
if choice < 1 || choice > 3 {
fmt.Println("输入有误,请重新输入!")
continue
}
computer := rand.Intn(3) + 1
fmt.Printf("电脑出拳:%d\n", computer)
if (choice == 1 && computer == 2) || (choice == 2 && computer == 3) || (choice == 3 && computer == 1) {
fmt.Println("你赢了!")
count++
} else if choice == computer {
fmt.Println("平局!")
} else {
fmt.Println("你输了!")
}
fmt.Printf("你目前赢了 %d 次\n\n", count)
}
fmt.Println("谢谢游玩!")
}
```
在这个猜拳游戏中,我们首先打印了游戏的主界面,让用户选择石头、剪刀、布或退出游戏。然后我们接受用户的输入,判断用户是否输入了有效的数字,如果数字无效,就要求用户重新输入。接下来,我们使用随机数来生成电脑出拳的数字,然后根据用户和电脑出拳的数字来判断输赢,并统计赢得次数。最后,如果用户选择退出游戏,我们就跳出循环并结束程序。