1A2B Play a game of 1A2B

This commit is contained in:
xtaodada 2022-07-24 15:37:02 +08:00
parent ace5189ae5
commit 5b1449fd10
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
2 changed files with 91 additions and 0 deletions

81
1A2B/main.py Normal file
View File

@ -0,0 +1,81 @@
import secrets
from typing import List
from pagermaid.enums import Message
from pagermaid.listener import listener
games = {}
class Game:
password: List[int]
times: int
def __init__(self):
self.times = 0
self.gen_password()
def gen_password(self):
ans = []
while len(ans) != 4:
n = secrets.choice(range(10))
if n not in ans:
ans.append(n)
self.password = ans
@staticmethod
def check_input(answer: str):
numbers = " ".join(answer).split()
if len(numbers) != 4:
return False
data = [0, 0, 0, 0]
for i in range(4):
data[i] = int(numbers[i])
return data
def check_answer(self, answer: str):
nums = self.check_input(answer)
if not nums:
raise ValueError("Invalid input")
a, b = 0, 0
for n in nums:
if n in self.password:
if nums.index(n) == self.password.index(n):
a += 1
else:
b += 1
self.times += 1
return a, b
@listener(command="1A2B",
groups_only=True,
description="Play a game of 1A2B",
usage="<start/stop/answer>")
async def play_game_1a2b(message: Message):
if not message.arguments:
return await message.edit("Please specify a command.")
game = games.get(message.chat.id, None)
if message.arguments == "start":
if game:
return await message.edit("Game already started.")
games[message.chat.id] = Game()
return await message.edit("Game started.")
if message.arguments == "stop":
if not game:
return await message.edit("Game not started.")
del games[message.chat.id]
return await message.edit("Game stopped.")
if message.arguments == "answer":
if not game:
return await message.edit("Game not started.")
answer = "".join(map(str, game.password))
return await message.edit(f"The answer is: {answer}\n\nGame over.")
if game:
try:
a, b = game.check_answer(message.arguments)
except ValueError:
return await message.edit("You need to guess 4 numbers between 0 ~ 9.\nFor example: 1234")
if a == 4:
return await message.edit("You Win!\n\nGame over.")
return await message.edit("%d: %dA%dB" % (game.times, a, b))

View File

@ -459,6 +459,16 @@
"supported": true, "supported": true,
"des-short": "解除封禁一位用户。", "des-short": "解除封禁一位用户。",
"des": "解除封禁一位用户。指令:,unban" "des": "解除封禁一位用户。指令:,unban"
},
{
"name": "1A2B",
"version": "1.0",
"section": "chat",
"maintainer": "xtaodada",
"size": "2.49 kb",
"supported": true,
"des-short": "Play a game of 1A2B",
"des": "Play a game of 1A2B, command:,1a2b"
} }
] ]
} }