From a8ba3505473224c0696724a56898ed40ae19787f Mon Sep 17 00:00:00 2001 From: nea Date: Sun, 27 Nov 2022 21:04:03 +0100 Subject: drawbot --- index.html | 24 ++++++++++++++++++++++-- server.py | 24 +++++++++++++++++++----- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index a3b605d..ea4fdbc 100644 --- a/index.html +++ b/index.html @@ -13,6 +13,11 @@ margin: 0; } + #app { + margin: 0 auto; + width: fit-content; + } + .board { border: 2px solid black; font-size: 20px; @@ -45,13 +50,20 @@ border: crimson solid 1px; } + .status { + text-align: center; + font-family: sans-serif; + } + .invisible { display: none !important; } -
+
+

You are white

+
@@ -78,7 +90,7 @@ let board = document.createElement("table") board.className = "board" this.fields = {}; - for (let i = 0; i < 8; i++) { + for (let i = 7; i >= 0; i--) { let row = document.createElement("tr") row.className = "board-row" for (let j = 0; j < 8; j++) { @@ -97,6 +109,9 @@ board.appendChild(row) } elem.appendChild(board) + this.turnIndicator = document.createElement('p') + this.turnIndicator.className = "turn-indicator status" + elem.appendChild(this.turnIndicator) } playMove(fromField, toField) { @@ -120,6 +135,7 @@ } synchronizeBoard() { + this.turnIndicator.innerHTML = `${capitalize(this.boardState.turn)}s turn` for (let field in this.fields) { this.fields[field].innerHTML = "" if (this.boardState[field]) { @@ -135,6 +151,10 @@ } } + function capitalize(thing) { + return thing.replace(/\b./, x => x.toUpperCase()) + } + function indiciesToFieldName(row, col) { return "abcdefgh".at(col) + (row + 1) } diff --git a/server.py b/server.py index aa33d87..f320bc4 100644 --- a/server.py +++ b/server.py @@ -2,7 +2,6 @@ import asyncio import json import os import pathlib -import random import typing import weakref @@ -42,7 +41,15 @@ async def handle_socket(request: web.Request): req = json.loads(msg.data) m = req["method"] if m == "move": - user_move = chess.Move.from_uci(req['params']['move']) + try: + user_move = chess.Move.from_uci(req['params']['move']) + except ValueError: + await ws.send_json(dict( + event="reject_move", + board=board.fen(), + )) + continue + if user_move not in board.legal_moves: await ws.send_json(dict( event="reject_move", @@ -55,9 +62,16 @@ async def handle_socket(request: web.Request): board=board.fen(), lastmove=user_move.uci(), )) - await asyncio.sleep(1.5) - # candidates = await engine.analyse(board, chess.engine.Limit(time=1), multipv=100) - my_move: chess.Move = random.choice(list(board.legal_moves)) + candidates: typing.List[chess.engine.InfoDict] = await engine.analyse( + board, chess.engine.Limit(time=1), multipv=100) + + def appraise(sit: chess.engine.InfoDict): + score: chess.engine.PovScore = sit['score'] + numscore = score.relative.score(mate_score=100000) + return abs(numscore) + + most_drawy_move: chess.engine.InfoDict = min(candidates, key=appraise) + my_move: chess.Move = (most_drawy_move['pv'][0]) board.push(my_move) await ws.send_json(dict( event="computer_moved", board=board.fen(), -- cgit