当前位置:网站首页>【golang从入门到实践】石头剪刀布游戏
【golang从入门到实践】石头剪刀布游戏
2022-07-19 19:53:00 【会振刀的程序员】
功能介绍
石头剪子布,人机对战
效果展示
编程思路
1、用二维数组列出玩家所有赢的组合
2、获取用户输入出拳、rand函数获取电脑随机生成出拳
3、判断如果 用户、电脑组合情况符合二维数组赢的组合,则玩家赢;如果用户==电脑,则平局;除此之外,电脑赢
源代码
package main
import (
"fmt"
"math/rand"
"time"
)
var person string
var computer string
var dic = map[int]string{
0: "石头", 1: "剪刀", 2: "布"}
var win = [][]string{
{
"石头", "剪刀"}, {
"布", "石头"}, {
"剪刀", "布"}}
var flag string
var person_flag bool
func main() {
for {
fmt.Println("请出拳:")
fmt.Scan(&person)
rand.Seed(time.Now().UnixNano())
r := rand.Intn(2)
fmt.Println("person:", person, "computer", dic[r])
person_flag = false
for _, v := range win {
if v[0] == person && v[1] == dic[r] {
person_flag = true
}
}
if person == dic[r] {
fmt.Println("平局")
} else if person_flag == true {
fmt.Println("person赢")
} else {
fmt.Println("computer赢")
}
fmt.Println("Y(继续玩儿):")
fmt.Scan(&flag)
if flag == "Y" {
fmt.Println("----------")
time.Sleep(time.Duration(1) * time.Second)
} else {
fmt.Println("成功退出")
break
}
}
}
边栏推荐
- LVGL 8.2 Tabview
- 如何将 NFT 元数据从 IPFS 转移到智能合约中
- HTC全新VR一体机Vive Focus Plus发布:定价5699元!
- [development tutorial 3] open source Bluetooth heart rate waterproof sports Bracelet - development environment construction
- 小米100W快充、50W无线充电技术曝光!OPPO沈义人回应:无聊!
- robotframework实战(三)衍生——以百度搜索功能为例编写测试用例
- A review of the latest introduction to neural data compression
- Part of the second Shanxi Network Security Skills Competition (Enterprise Group) WP (V)
- Customize ViewGroup container horizontalscrollviewex
- Solution to the first game of 2022 Hangzhou Electric Multi school league
猜你喜欢
另类解读宏观形势:美联储或将很快结束加息进程,重回量化宽松?
NFT访问工具PREMINT遭黑,损失超37万美元
Part of the second Shanxi Network Security Skills Competition (Enterprise Group) WP (V)
How can Web3 enterprises use tokens to motivate employees?
Leetcode interview question 05.06 Integer conversion
Given the preorder traversal and the inorder traversal order of a binary tree, find the postorder traversal of the tree
Win10+libtorch+yolov5-6.0 deployment
Array of daily questions for Niuke
牛客每日刷题之数组
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
随机推荐
DNS原理及配置
Redis high availability: do you call this the principle of master-slave architecture data synchronization?
[development tutorial 5] crazy shell arm function mobile phone serial port experiment tutorial
源码 | OpenCV DNN + YOLOv7目标检测
短视频系统源码,uni-app项目中主要文件的加载顺序
Babang MS Marco! Transformer based hybrid list aware sorting model
Leetcode interview question 05.06 Integer conversion
NFT访问工具PREMINT遭黑,损失超37万美元
leetcode 面试题 05.06. 整数转换
NFT access tool premint was hacked, with a loss of more than 370000 US dollars
How to implement recursive function in wechat game making tool
【云图说】第248期 图解公网域名解析:轻松实现域名访问网站/邮箱
图表即代码:以代码化的方式构建新一代图形库 —— Feakin
对Coinbase中长期前景的冷静评估
getdata table 表格数据 join mysql方法
php 百度有钱花分期API
Part of the second Shanxi Network Security Skills Competition (Enterprise Group) WP (V)
Swagger 简单快速入门教程
DBC2000有什么作用?DBC2000的安装与配置
Redis high availability: you call this sentinel sentinel cluster principle