html-tictactoe/generator/__main__.py

67 lines
1.9 KiB
Python
Raw Normal View History

2024-10-23 21:05:10 +00:00
from jinja2 import Environment, select_autoescape, FileSystemLoader
2024-11-02 14:02:48 +00:00
from generator.model import Board, NOT_DETERMINED, WIN, Move
2024-10-23 21:05:10 +00:00
from generator.tictactoe import calculate_best_move
2024-10-25 16:12:25 +00:00
import random
2024-10-23 21:05:10 +00:00
env = Environment(
loader=FileSystemLoader(["./generator"]),
autoescape=select_autoescape()
)
template = env.get_template("template.html")
2024-10-25 16:12:25 +00:00
taunts = [
2024-10-25 16:15:45 +00:00
"Looks like you're just one step away from defeat! Better luck next time!",
"You might want to start practicing your 'congratulations' speech for me!",
"I hope you're ready to accept your fate - because it's coming fast!",
"You can almost hear the victory music playing for me, can't you?",
"You're about to witness a masterclass in losing - starring you!",
"You're on the express train to defeat, and I'm the conductor!"
2024-10-25 16:12:25 +00:00
]
random.seed(1337)
def get_taunt(board, outcome):
if outcome == WIN and board.winner() == NOT_DETERMINED:
return random.choice(taunts)
else:
return ""
2024-10-23 21:05:10 +00:00
2024-11-02 14:02:48 +00:00
def render_board(prefix, old_board, board, outcome):
with open("output/" + prefix + old_board.get_id() + ".html", "w") as file:
2024-10-25 16:12:25 +00:00
file.write(
template.render(
board=board,
prefix=prefix,
2024-11-02 14:02:48 +00:00
reset=prefix + ".html",
msg=get_taunt(board, outcome),
Move=Move,
2024-10-25 16:12:25 +00:00
)
)
2024-10-23 21:05:10 +00:00
2024-11-02 14:02:48 +00:00
def generate_options(prefix, old_board, board, outcome):
render_board(prefix, old_board, board, outcome)
2024-10-23 21:05:10 +00:00
if board.winner() == NOT_DETERMINED:
for move in board.moves():
future = board.apply(move)
2024-10-25 16:12:25 +00:00
response, outcome = calculate_best_move(future)
2024-10-23 21:05:10 +00:00
print(board.get_id(), move, response)
2024-10-23 21:05:10 +00:00
generate_options(
2024-11-02 14:02:48 +00:00
prefix,
future,
2024-10-25 16:12:25 +00:00
future.apply(response) if response else future,
2024-11-02 14:02:48 +00:00
outcome
2024-10-23 21:05:10 +00:00
)
if __name__ == "__main__":
board = Board()
2024-11-02 14:02:48 +00:00
generate_options("index", board, board, NOT_DETERMINED)