diff --git a/app/chess_sim/test.py b/app/chess_sim/test.py index c901463..4a7b9d7 100644 --- a/app/chess_sim/test.py +++ b/app/chess_sim/test.py @@ -3,6 +3,9 @@ from dataclasses import dataclass from typing import Iterator, Optional, List, Tuple import random +FILES = "abcdefgh" +RANKS = "12345678" + class PieceType(Enum): PAWN = auto() KNIGHT = auto() @@ -513,16 +516,24 @@ class ChessBoard: # https://en.wikipedia.org/wiki/Algebraic_notation_(chess) @staticmethod def pos_to_algebraic(pos: BoardPos) -> str: - file = chr(ord('a') + pos.y) - rank = str(8 - pos.x) - return f"{file}{rank}" + file = FILES[pos.y] + rank = RANKS[7 - pos.x] + return file + rank @staticmethod def algebraic_to_pos(s: str) -> BoardPos: if len(s) != 2: raise ValueError("invalid algebraic square") - col = ord(s[0].lower()) - ord('a') - row = 8 - int(s[1]) + + file_char = s[0].lower() + rank_char = s[1] + + if file_char not in FILES or rank_char not in RANKS: + raise ValueError("invalid algebraic square") + + col = FILES.index(file_char) + row = 7 - RANKS.index(rank_char) + return BoardPos((row, col)) # https://www.chess.com/de/terms/forsyth-edwards-notation-fen