feat: Add taunts

This commit is contained in:
sigmasternchen 2024-10-25 18:12:25 +02:00
parent 267ae7e682
commit b9256901f6
2 changed files with 38 additions and 10 deletions

View file

@ -1,39 +1,65 @@
from jinja2 import Environment, select_autoescape, FileSystemLoader from jinja2 import Environment, select_autoescape, FileSystemLoader
from generator.model import Board, NOT_DETERMINED from generator.model import Board, NOT_DETERMINED, WIN
from generator.tictactoe import calculate_best_move from generator.tictactoe import calculate_best_move
import random
env = Environment( env = Environment(
loader=FileSystemLoader(["./generator"]), loader=FileSystemLoader(["./generator"]),
autoescape=select_autoescape() autoescape=select_autoescape()
) )
template = env.get_template("template.html") template = env.get_template("template.html")
taunts = [
"Looks like youre just one step away from defeat! Better luck next time!",
"You might want to start practicing your congratulations speech for me!",
"I hope youre ready to accept your fate—because its coming fast!",
"You can almost hear the victory music playing for me, cant you?",
"Youre about to witness a masterclass in losing—starring you!",
"Youre on the express train to defeat, and Im the conductor!"
]
def render_board(initial_prefix, prefix, board): random.seed(1337)
def get_taunt(board, outcome):
if outcome == WIN and board.winner() == NOT_DETERMINED:
return random.choice(taunts)
else:
return ""
def render_board(initial_prefix, prefix, board, outcome):
with open("output/" + prefix + ".html", "w") as file: with open("output/" + prefix + ".html", "w") as file:
file.write(template.render(board=board, prefix=prefix, reset=initial_prefix + ".html")) file.write(
pass template.render(
board=board,
prefix=prefix,
reset=initial_prefix + ".html",
msg=get_taunt(board, outcome)
)
)
def generate_options(initial_prefix, prefix, board): def generate_options(initial_prefix, prefix, board, outcome):
render_board(initial_prefix, prefix, board) render_board(initial_prefix, prefix, board, outcome)
if board.winner() == NOT_DETERMINED: if board.winner() == NOT_DETERMINED:
for move in board.moves(): for move in board.moves():
future = board.apply(move) future = board.apply(move)
response, _ = calculate_best_move(future) response, outcome = calculate_best_move(future)
print(prefix, move, response) print(prefix, move, response)
generate_options( generate_options(
initial_prefix, initial_prefix,
prefix + str(move), prefix + str(move),
future.apply(response) if response else future future.apply(response) if response else future,
outcome
) )
if __name__ == "__main__": if __name__ == "__main__":
board = Board() board = Board()
generate_options("index", "index", board) generate_options("index", "index", board, NOT_DETERMINED)

View file

@ -10,6 +10,8 @@
{%- set winner = board.winner() -%} {%- set winner = board.winner() -%}
<h3>{{ msg }}</h3>
<a href={{ reset }}>Start over</a>{{- "" -}} <a href={{ reset }}>Start over</a>{{- "" -}}
<br /><br />{{- "" -}} <br /><br />{{- "" -}}
<table border=1 cellpadding=10> <table border=1 cellpadding=10>
@ -48,4 +50,4 @@
<hr />{{- "" -}} <hr />{{- "" -}}
<a href="https://github.com/sigmasternchen/html-tictactoe">Source Code</a>{{- "" -}} <a href="https://github.com/sigmasternchen/html-tictactoe">Source Code</a>{{- "" -}}
</body>{{- "" -}} </body>{{- "" -}}
</html> </html>