Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1 release: major improvements, bug fixes and more #8

Merged
merged 22 commits into from
Jan 13, 2021

Conversation

genyrosk
Copy link
Owner

@genyrosk genyrosk commented Jan 12, 2021

Version 1 of gym-chess is an almost complete rewrite of the original code with a bunch of improvements, bug-fixes and tests. The original implementation has been renamed V0, but further development or maintenance of it is not planned.

Environment settings

As chess is a 2-player game, you can choose to play against a random bot or against yourself (self-play).

  • initial_state: initial board positions, the default value is the default chess starting board
  • opponent: can be "random", "none" or a function. Tells the environment whether to use a random bot, play against self or use a specific bot policy (default: "random")
  • log: True or False, specifies whether to log every move and render every new state (default: True)
  • player_color: "WHITE" or "BLACK", only useful if playing against a bot

New move specification:

Moves are now specified as either:

  • a tuple of coordinates ((from_x, from_y), (to_x, to_y))
  • or a string e.g. "CASTLE_KING_SIDE_WHITE", "CASTLE_QUEEN_SIDE_BLACK", "RESIGN"

Moves are pre-calculated for every new state and stored in possible_moves.

A basic script would look like this:

env = ChessEnvV1(log=False)
moves = env.possible_moves
action = env.move_to_action(moves[0])
state, reward, done, info = env.step(action)
env.render()

You can also calculate the possible steps separately for every player:

white_moves = env.get_possible_moves(player="WHITE")
black_moves = env.get_possible_moves(player="BLACK")

State

The state is the board with pieces.

>>> print(env.state)
[[-3, -5, -4, -2, -1, -4, -5, -3],
 [-6, -6, -6, -6, -6, -6, -6, -6],
 [0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 0],
 [6, 6, 6, 6, 6, 6, 6, 6],
 [3, 5, 4, 2, 1, 4, 5, 3]]

It can be rendered in a prettier way with the render() method:

>>> env.render()
    -------------------------
 8 |  ♖  ♘  ♗  ♕  ♔  ♗  ♘  ♖ |
 7 |  ♙  ♙  ♙  ♙  ♙  ♙  ♙  ♙ |
 6 |  .  .  .  .  .  .  .  . |
 5 |  .  .  .  .  .  .  .  . |
 4 |  .  .  .  .  .  .  .  . |
 3 |  .  .  .  .  .  .  .  . |
 2 |  ♟  ♟  ♟  ♟  ♟  ♟  ♟  ♟ |
 1 |  ♜  ♞  ♝  ♛  ♚  ♝  ♞  ♜ |
    -------------------------
      a  b  c  d  e  f  g  h

Every integer represents a piece. Positive pieces are white and negative ones are black.

Piece IDs are stored in constants that can be imported.

from gym_chess.envs.chess_v1 import (
    KING_ID,
    QUEEN_ID,
    ROOK_ID,
    BISHOP_ID,
    KNIGHT_ID,
    PAWN_ID,
)

The schema is:

EMPTY_SQUARE_ID = 0
KING_ID = 1
QUEEN_ID = 2
ROOK_ID = 3
BISHOP_ID = 4
KNIGHT_ID = 5
PAWN_ID = 6

Additional information can be found in other attributes of the environment:

env.current_player
env.white_king_castle_possible
env.white_queen_castle_possible
env.black_king_castle_possible
env.black_queen_castle_possible
env.white_king_on_the_board
env.black_king_on_the_board

Other features

  • The environment keeps track of all past states and can detect 3-fold repetitions, at which point the game is a draw.
  • Render method uses background colors to better visualize the move

Code linting and fixing

Code fixing is done with black with max line width of 100 characters with the command black -l 100 . No config needed.

Notes:

En-passant moves are not currently supported in the V1 environment.

@genyrosk genyrosk merged commit 1f9b99b into master Jan 13, 2021
@genyrosk genyrosk changed the title Version 2 Version 1 Jan 13, 2021
@genyrosk genyrosk changed the title Version 1 Version 1 release: major improvements, bug fixes and more Jan 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant