moth/example-puzzles/example/3/mkpuzzle

61 lines
1.6 KiB
Plaintext
Raw Permalink Normal View History

2020-09-11 17:33:43 -06:00
#! /usr/bin/python3
2020-09-11 13:03:19 -06:00
2020-09-11 17:33:43 -06:00
import argparse
import json
import os
import random
import shutil
import sys
2020-09-11 13:03:19 -06:00
2020-09-14 18:23:56 -06:00
random.seed(os.getenv("SEED", ""))
2020-09-11 17:33:43 -06:00
words = ["apple", "pear", "peach", "tangerine", "orange", "potato", "carrot", "pea"]
answer = ' '.join(random.sample(words, 4))
2020-09-14 18:23:56 -06:00
def puzzle():
2020-09-11 17:33:43 -06:00
number = random.randint(20, 500)
obj = {
"Authors": ["neale"],
"Body": (
"<p>Dynamic puzzles are provided with a JSON-generating <code>mkpuzzles</code> program in the puzzle directory.</p>"
"<p>You can write <code>mkpuzzles</code> in any language you like. This puzzle was written in Python 3.</p>"
"<p>Here is some salad:<img src='salad.jpg'></p>"
),
"Attachments": ["salad.jpg"],
2020-09-11 17:33:43 -06:00
"Answers": [
answer,
2020-09-11 13:03:19 -06:00
],
2020-09-11 17:33:43 -06:00
"Debug": {
"Summary": "Dynamic puzzles",
"Hints": [
"Check the debug output to get the answer." ,
],
"Errors": [],
"Log": [
"%d is a positive integer" % number,
],
}
2020-09-11 13:03:19 -06:00
}
2020-09-11 17:33:43 -06:00
json.dump(obj, sys.stdout)
2020-09-14 18:23:56 -06:00
def open_file(filename):
f = open(filename, "rb")
shutil.copyfileobj(f, sys.stdout.buffer)
def check_answer(check):
2020-10-16 14:18:44 -06:00
obj = {
"Correct": (answer == check)
}
json.dump(obj, sys.stdout)
2020-09-14 18:23:56 -06:00
if len(sys.argv) == 1:
2020-10-16 14:18:44 -06:00
raise RuntimeError("Command not provided")
elif sys.argv[1] == "puzzle":
2020-09-14 18:23:56 -06:00
puzzle()
elif sys.argv[1] == "file":
open_file(sys.argv[2])
elif sys.argv[1] == "answer":
check_answer(sys.argv[2])
else:
raise RuntimeError("Unknown command: %s" % sys.argv[1])