概述
复刻的是b站upGM的秘密基地
开箱的那个小玩意
使用Python制作,这里感谢DeepSeek
拯救了我的注释,我写的注释only我可以看懂,由于图形化会有兼容性问题,所以我使用数字解决,也稍微更改了一点点规则,希望大家喜欢(〃ω〃)
代码附上
♾️ python 代码: import random
# ANSI颜色代码
COLOR = {
'绿': '\033[92m#\033[0m', # 亮绿色
'白': '\033[97m#\033[0m', # 亮白色
'红': '\033[91m#\033[0m', # 亮红色
'reset': '\033[0m'
}
def show_rules():
print(f"""
========= 游戏规则 =========
1. 密码为4位不重复的1-9数字
2. 每次猜测后会有颜色提示:
{COLOR['绿']} 绿色:数字正确且位置正确
{COLOR['白']} 白色:数字正确但位置错误
{COLOR['红']} 红色:数字不存在
注意:密码中的数字不重复,重复猜测的数字将单独判断
3. 困难模式只显示颜色数量统计
4. 输入必须是4位数字,每位为1-9,允许重复
============================
""")
def generate_secret():
return random.sample(range(1, 10), 4)
def get_guess_count():
while True:
try:
count = int(input("请输入猜测次数:"))
if count >= 1:
return count
print("次数必须大于0")
except ValueError:
print("请输入有效数字")
def get_hard_mode():
while True:
mode = input("启用困难模式?(y/n): ").lower()
if mode in ('y', 'n'):
return mode == 'y'
print("请输入 y 或 n")
def validate_guess(guess_str):
return (len(guess_str) == 4 and guess_str.isdigit() and
1 <= min(map(int, guess_str)) and 9 >= max(map(int, guess_str)))
def get_user_guess():
while True:
guess = input("请输入猜测(示例 1234):")
if validate_guess(guess):
return list(map(int, guess))
print("输入不符合要求!")
def compare_numbers(secret, guess, hard_mode):
secret = list(secret)
guess = list(guess)
result = []
matched = [False] * 4
# 第一轮匹配绿色
for i in range(4):
if guess[i] == secret[i]:
result.append('绿')
matched[i] = True
guess[i] = None # 标记已处理
# 第二轮匹配白色
for i in range(4):
if guess[i] is None:
continue
for j in range(4):
if not matched[j] and guess[i] == secret[j]:
result.append('白')
matched[j] = True
break
else:
result.append('红')
return (result.count('绿'), result.count('白'), result.count('红')) if hard_mode else result
def display_feedback(feedback, hard_mode):
if hard_mode:
print(f"提示:绿色{feedback[0]} 白色{feedback[1]} 红色{feedback[2]}")
else:
colored = [COLOR[c] for c in feedback]
print("提示:" + ' '.join(colored))
def main_game():
show_rules()
secret = generate_secret()
max_attempts = get_guess_count()
hard_mode = get_hard_mode()
for attempt in range(1, max_attempts + 1):
print(f"\n=== 第{attempt}次尝试 ===")
guess = get_user_guess()
if guess == secret:
print(COLOR['绿'] + "★ 破解成功!★" + COLOR['reset'])
return
feedback = compare_numbers(secret, guess, hard_mode)
display_feedback(feedback, hard_mode)
print(f"\n{COLOR['红']}游戏结束!正确答案:{''.join(map(str, secret))}{COLOR['reset']}")
if __name__ == "__main__":
print("\n=== 数字解密游戏 ===")
main_game()
感谢阅读😎
👍
💖
💯
💦
😄
🪙
博主
柒星👍
💖
💯
💦
😄
🪙