Giter Club home page Giter Club logo

chess_py's Introduction

chess_py

Build Status PyPI version License Twitter


8 ║♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
7 ║♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
6 ║… … … … … … … …
5 ║… … … … … … … …
4 ║… … … … … … … …
3 ║… … … … … … … …
2 ║♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
1 ║♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
--╚═══════════════
——-a b c d e f g h

License

chess_py is available under the MIT license. See the LICENSE file for more info. Copyright © 2016 Aubhro Sengupta. All rights reserved.

Introduction

Chess_py is an open source chess client and framework written in python. Can create chess matches and provide players with data regarding current position, possible moves and eventual result of the game. External players (either computer or human) are required to play. Works in both Python2 and Python3.

##Installation

To use as a standalone chess client, download or clone, navigate inside chess_py directory, and type the following.

Works on Python 2 and 3

python main.py

To use as an importable framework in your own python code, you can install manually or using pip. #####To install manually, download or clone, chess_py directory, and type the following.

python setup.py install

#####Or use pip

pip install chess_py

Great! How do you use it?

Chess_py has the capability of creating games between players, either human (algebraic notatioon input through console), or AI (through method generate_move(position))

color.white is defined to be True and color.black is defined to be False in color.py

# Set up
from chess_py import Color, Player, Game, Color, Board, Move

# Creates color objects to initialize players with.
white_color = Color.init_white()
black_color = Color.init_black()

If you want to set up a game with two humans, use Player class to initialize a white and a black human.Player objects.

# Creates a game with human players playing white and black
new_game = Game(Player(white_color), Player(black_color))

# Starts game and stores result
# Result is of type integer 
# 1 - White wins
# 0.5 - Draw
# 0 - Black Wins
result = new_game.play()

To use a custom player with chess_py, the player must be a python class with a generate_move() method.

# Creates a game with a human player playing white and a separate software playing black
new_game = Game(human.Player(color_white), your_class(color_black))
result = new_game.play()

# In your_class . . . 
class your_class:
    def __init__(self, color):
      # color parameter can be accessed in several different ways
      
      self.color = color
      
      # color_string is either "white" or "black"
      color_string = color.string
      # color_bool is True if color is white, or False if color is black
      color_bool = color.color
      # Your code
    
    def generate_move(self, position):
      # position parameter is an object of type Board
      # Must return a list of objects of type Move
      moves = []
      
      # Move must be initialized with parameters location, piece, status
      # Optional parameters are start_rank (y coordinate from 1 to 7), 
      #start_file (x coordinate from 1 to 7), string, and promoted_to_piece 
      #(one of 6 objects of subclass Piece)
      # Here is an example of the move e4
      my_move = Move(Location(3, 4), position.piece_at_square(Location(1, 4)), 
      # notation_const.MOVEMENT)
      # Notice a couple things:
      # Location is initialized with rank (y coordinate from 1 to 7) first, 
      # then file (x coordinate from 1 to 7)
      # Status is a constant from notation_const 
      # Other constants include CAPTURE, PROMOTE, EN_PASSANT, etc.
      
      # All of that looks tedious. What if I just have a string storing the move in 
      # algebraic notation ie. Nf3 ?
      # Then do the following
      my_move = to_move(my_algebraic_notation_move_stored_as _string)
      # If your_class wanted to move e4
      return to_move("e4")
      
      # This works for any move written in algebraic notation
      return to_move("Nxf3")
      
      # Or even
      return to_move("e8=Q)
      
      # More helpful stuff . . .
      
      # Returns list of all possible moves
      possible = position.all_possible_moves(self.color)
      
      # So if I wanted to return the pawn move that ended on e4 using all_possible_moves . . .
      for move in possible:
        if move.location.equals(Location(3, 4)) and \
            move.piece.equals(position.piece_at_square(Location(1, 4)):
            return move
        # Basically all data types built in to chess_py have an equals()
        # All of them work as you would expect except for the piece equals() method
        # They only compare color of the piece and type,  but not lacation
        # piece_at_square() in Board returns the piece at a certain Location
      
      # Gets my king
      my_king = position.get_king(self.color)
      
      # Returns bool stating if my king is in check
      my_king.in_check(position)
      
      # Your code
      return moves

That's it for now! More will be coming shortly.

chess_py's People

Contributors

rk900 avatar

Watchers

James Cloos avatar Vikranth Srivatsa 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.