「已注销」 2022-10-27 22:09 采纳率: 100%
浏览 55
已结题

如何模拟用户登录,设计剪刀石头布游戏,模拟掷骰子?(语言-python)

img

img


希望能够用比较基础的python语言完成,大学生的课程,要有完整的代码截图和语言。

  • 写回答

1条回答 默认 最新

  • Ko-walski 2022-10-27 22:38
    关注

    骰子部分:

    import random
    
    DICE_ART = {
        1: (
            "┌─────────┐",
            "│         │",
            "│    ●    │",
            "│         │",
            "└─────────┘",
        ),
        2: (
            "┌─────────┐",
            "│  ●      │",
            "│         │",
            "│      ●  │",
            "└─────────┘",
        ),
        3: (
            "┌─────────┐",
            "│  ●      │",
            "│    ●    │",
            "│      ●  │",
            "└─────────┘",
        ),
        4: (
            "┌─────────┐",
            "│  ●   ●  │",
            "│         │",
            "│  ●   ●  │",
            "└─────────┘",
        ),
        5: (
            "┌─────────┐",
            "│  ●   ●  │",
            "│    ●    │",
            "│  ●   ●  │",
            "└─────────┘",
        ),
        6: (
            "┌─────────┐",
            "│  ●   ●  │",
            "│  ●   ●  │",
            "│  ●   ●  │",
            "└─────────┘",
        ),
    }
    DIE_HEIGHT = len(DICE_ART[1])
    DIE_WIDTH = len(DICE_ART[1][0])
    DIE_FACE_SEPARATOR = " "
    
    
    def parse_input(input_string):
        if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
            return int(input_string)
        else:
            print("Please enter a number from 1 to 6.")
            raise SystemExit(1)
    
    
    def roll_dice(num_dice):
    
        roll_results = []
        for _ in range(num_dice):
            roll = random.randint(1, 6)
            roll_results.append(roll)
        return roll_results
    
    
    def generate_dice_faces_diagram(dice_values):
        dice_faces = _get_dice_faces(dice_values)
        dice_faces_rows = _generate_dice_faces_rows(dice_faces)
    
        width = len(dice_faces_rows[0])
        diagram_header = " RESULTS ".center(width, "~")
    
        dice_faces_diagram = "\n".join([diagram_header] + dice_faces_rows)
        return dice_faces_diagram
    
    
    def _get_dice_faces(dice_values):
        dice_faces = []
        for value in dice_values:
            dice_faces.append(DICE_ART[value])
        return dice_faces
    
    
    def _generate_dice_faces_rows(dice_faces):
        dice_faces_rows = []
        for row_idx in range(DIE_HEIGHT):
            row_components = []
            for die in dice_faces:
                row_components.append(die[row_idx])
            row_string = DIE_FACE_SEPARATOR.join(row_components)
            dice_faces_rows.append(row_string)
        return dice_faces_rows
    
    
    num_dice_input = input("你想掷多少个骰子? [1-6] ")
    num_dice = parse_input(num_dice_input)
    
    roll_results = roll_dice(num_dice)
    
    dice_face_diagram = generate_dice_faces_diagram(roll_results)
    
    print(f"\n{dice_face_diagram}")
    
    
    

    其他由网友实现吧太麻烦🫠

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月27日
  • 已采纳回答 10月27日
  • 创建了问题 10月27日

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line