GithubHelp home page GithubHelp logo

gym-chess's Introduction

gym-chess

A simple chess environment for gym. It computes all available moves, including castling, en-passant, pawn promotions and 3-fold repetition draws.

8
7
6
5
4
3
2
1
a b c d e f g h

Setup

Install the environment:

pip3 install git+<URL_FOR_THIS_REPO>  

Play against yourself or against a random bot.

import gym
import gym_chess

env = gym.make('ChessVsRandomBot-v0')
env = gym.make('ChessVsSelf-v0')

Play

You can either get available moves and convert them into the action space, or get the available actions directly.

import random

# moves
moves = env.get_possible_moves(state, player)
m = random.choice(moves)
action = env.move_to_actions(m)

# actions
actions = env.get_possible_actions(state, player)
action = random.choice(actions)

A move is a dictionary that holds the id of the piece (from 1 to 16 for white and -1 to -16 for black), its current position and its new position.

move = {
    'piece_id': <int>,
    'pos': [<int>,<int>],
    'new_pos': [<int>,<int>]
}

NB: when a move is converted to an action and vice-versa, the 'pos' attribute is dropped (it doesn't provide any useful information since the current position of a given piece is recorded in the current state).

Pass an action to the environment and retrieve the new state, reward, done and info:

state, reward, done, __ = env.step(action)

Reset the environment:

initial_state = env.reset()

Visualise the chess board

Visualise the current state of the chess game:

env.render()

Show the moves that a piece can make given a set of moves (by any/all pieces on the board)

# Player 1 moves
piece = 6 # white queen
env.render_moves(state, piece, moves, mode='human')

You can also retrieve the list of squares that pieces are attacking and defending by specifying the "attack" option:

attacking_moves = env.get_possible_moves(state, player, attack=True)

gym-chess's People

Contributors

alyshmahell avatar genyrosk avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.