Practice mode is now at /practice on server.py instead of a separate server, with a button in the bottom-right corner of each page to the other mode. Touch: no double-tap zoom, no sticky hover after a tap, no long-press selection on the board, and a pressed look on tapped squares. Co-Authored-By: Claude Opus 5.5 <[email protected]>
36 lines
1.4 KiB
Python
36 lines
1.4 KiB
Python
"""Moonchess practice mode: play against the computer. server.py serves it at /practice.
|
|
|
|
Stateless: the page sends the current position with every request and gets the new
|
|
position back, so there's no session to lose. The page decides whose turn belongs to
|
|
the computer; this module only applies moves and picks them.
|
|
"""
|
|
|
|
from aiohttp import web
|
|
|
|
from game import Game
|
|
from solver import Solver
|
|
|
|
solver = Solver() # solves every position up front; takes about a second
|
|
|
|
|
|
async def play(request):
|
|
"""Body: {history, turn, cell} to play `cell`, or {history, turn, accuracy} for a
|
|
computer move, where accuracy (0-1) is the chance it plays a perfect move. A computer
|
|
move's reply also says how it was chosen, as computerMove: {cell, how}."""
|
|
try:
|
|
body = await request.json()
|
|
game = Game.from_history(body["history"], body["turn"])
|
|
cell = body.get("cell")
|
|
accuracy = float(body.get("accuracy", 1))
|
|
except (ValueError, KeyError, TypeError, AttributeError):
|
|
return web.json_response({"error": "Invalid request"}, status=400)
|
|
|
|
computer_move = None
|
|
if cell is None and game.winner is None:
|
|
cell, how = solver.choose(game, accuracy)
|
|
computer_move = {"cell": cell, "how": how}
|
|
error = game.play(game.turn, cell)
|
|
if error:
|
|
return web.json_response({"error": error}, status=400)
|
|
return web.json_response({**game.to_dict(), "computerMove": computer_move})
|