Skip to content

Commit

Permalink
Create publish workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilherme-Vasconcelos committed Oct 27, 2023
1 parent d85fad4 commit 045edd2
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: All quality checks
name: CI

on:
push:
Expand All @@ -22,6 +22,11 @@ jobs:
python-version: '3.11'
cache: 'poetry'

- name: Validate project metadata
run: |
poetry check
poetry check --lock
- name: Install dependencies
run: poetry install

Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Publish

on:
push:
tags: [ "v*.*.*" ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Build and publish to pypi
uses: JRubics/poetry-publish@b71e946be561eaf8bfb7562ecc97c26fb8583070
with:
python_version: '3.11'
pypi_token: ${{ secrets.PYPI_TOKEN }}
6 changes: 3 additions & 3 deletions chessy/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Type(Enum):
KING = auto()


@dataclass(frozen=True)
@dataclass(frozen=True, slots=True)
class Piece:
ptype: Type
color: Color
Expand Down Expand Up @@ -188,7 +188,7 @@ def __lt__(self, other: Square) -> bool:
return self.value < other.value


@dataclass
@dataclass(slots=True)
class CastlingAvailability:
white_kingside: bool
white_queenside: bool
Expand All @@ -214,7 +214,7 @@ def disable_for_square(self, square: Square) -> None:
self.black_kingside = False


@dataclass(frozen=True)
@dataclass(frozen=True, slots=True)
class Move:
source: Square
target: Square
Expand Down
2 changes: 1 addition & 1 deletion chessy/core/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def setup_logging(
logging.config.dictConfig(logging_config)


@dataclass
@dataclass(frozen=True, slots=True)
class CliArgs:
debug: bool

Expand Down
6 changes: 3 additions & 3 deletions chessy/core/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ class UnreachablePositionError(BoardError):
pass


@dataclass(frozen=True)
@dataclass(frozen=True, slots=True)
class _MoveResult:
moved_piece: c.Piece
maybe_captured_piece: c.Piece | None
is_en_passant: bool
is_castling: bool


@dataclass(frozen=True)
@dataclass(frozen=True, slots=True)
class _RollbackableMove:
move: c.Move
move_result: _MoveResult
Expand All @@ -45,7 +45,7 @@ class _RollbackableMove:
previous_en_passant_target: c.Square | None


@dataclass
@dataclass(slots=True)
class Board:
_state: list[c.Piece | None]
active_color: c.Color
Expand Down
21 changes: 14 additions & 7 deletions chessy/core/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ def report_info(

class _NilInfoReporter(EvaluationInfoReporter):
def report_info(
self,
*,
depth: int,
best_evaluation: float,
pv: list[c.Move],
self, *, depth: int, best_evaluation: float, pv: list[c.Move]
) -> None:
pass

Expand Down Expand Up @@ -164,17 +160,28 @@ def _calculate_piece_counts(board: cb.Board) -> dict[c.Color, dict[Any, int]]:
if (piece := board.get_piece_by_square(square)) is not None:
piece_counts[piece.color][piece.ptype] += 1

# TODO: Account for doubled / blocked / isolated pawns.

return piece_counts

@classmethod
def _evaluate_score(cls, board: cb.Board) -> float:
# TODO: Enhance evaluation for openings and endgames.
# TODO: Account for doubled / blocked / isolated pawns.

white_mobility, black_mobility = cls._calculate_mobility(board)
piece_counts = cls._calculate_piece_counts(board)

if (
board.active_color == c.Color.WHITE
and white_mobility == 0
and piece_counts[c.Color.WHITE][c.Type.KING] == 1
) or (
board.active_color == c.Color.BLACK
and black_mobility == 0
and piece_counts[c.Color.BLACK][c.Type.KING] == 1
):
# Stalemate
return 0

weight = {
c.Type.KING: 200,
c.Type.QUEEN: 9,
Expand Down
2 changes: 1 addition & 1 deletion chessy/core/fen_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FenInvalidFullmoveNumberError(FenValidationError):
pass


@dataclass
@dataclass(frozen=True, slots=True)
class FenParseResult:
piece_placement: list[c.Piece | None]
active_color: c.Color
Expand Down
2 changes: 1 addition & 1 deletion chessy/core/movegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import chessy.core.board as cb


@dataclass
@dataclass(frozen=True, slots=True)
class _DirectionalAdd:
shift: int
prevented_files: Iterable[int] | None
Expand Down
12 changes: 9 additions & 3 deletions chessy/core/uci.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class _Info(_EngineCommand):
_initial_position_fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"


@dataclass(frozen=True)
@dataclass(frozen=True, slots=True)
class _UciEvaluationInfoReporter(ce.EvaluationInfoReporter):
_uci_engine: UciEngine

Expand All @@ -112,7 +112,12 @@ def report_info(
best_evaluation: float,
pv: list[c.Move],
) -> None:
cp = int(best_evaluation * 100)
if best_evaluation == float("-inf"):
cp = -100
elif best_evaluation == float("inf"):
cp = 100
else:
cp = int(best_evaluation * 100)
self._uci_engine._send_engine_command( # pyright: ignore[reportPrivateUsage]
_Info(depth, cp, pv)
)
Expand Down Expand Up @@ -253,7 +258,8 @@ def think() -> None:
case _:
ut.unreachable()

def _send_engine_command(self, command: _EngineCommand) -> None:
@staticmethod
def _send_engine_command(command: _EngineCommand) -> None:
match command:
# Without making thread-exclusive print calls, we could end up mixing
# commands in the same line. So do not use bare `print` here.
Expand Down
2 changes: 1 addition & 1 deletion chessy/prof/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import chessy.core.evaluator as ce


@dataclass
@dataclass(frozen=True, slots=True)
class CliArgs:
fen: str
depth: int
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "chessy"
version = "0.2.0"
version = "0.2.1"
description = "A simple chess engine"
authors = ["Guilherme Vasconcelos"]
license = "AGPL-3.0-or-later"
Expand Down Expand Up @@ -50,7 +50,7 @@ select = [
"PTH",
"PL",
"PERF",
"RUF"
"RUF",
]
# Same as black.
line-length = 88
Expand Down

0 comments on commit 045edd2

Please sign in to comment.