0

I've tried this:

import chess_py as chess
board=chess.Board()

but in my kernel it gives this error:

  File "C:\Users\raghu\anaconda3\lib\site-packages\chess_py\core\board.py", line 67, in __init__
    self.king_loc_dict = {white: self.find_king(white),

  File "C:\Users\raghu\anaconda3\lib\site-packages\chess_py\core\board.py", line 341, in find_king
    return self.find_piece(King(input_color, Location(0, 0)))

  File "C:\Users\raghu\anaconda3\lib\site-packages\chess_py\core\board.py", line 310, in find_piece
    if not self.is_square_empty(loc) and \

  File "C:\Users\raghu\anaconda3\lib\site-packages\chess_py\core\board.py", line 183, in is_square_empty
    return self.position[location.rank][location.file] is None

IndexError: string index out of range
Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • 1
    Is that exception coming from the code exactly as you've shown it? Because [my read of that package's code](https://github.com/LordDarkula/chess_py/blob/14bebc2f8c49ae25c59375cc83d0b38d8ff7281d/chess_py/core/board.py) suggests that a `Board` object needs to be initialized with an argument. You wouldn't get to the parts of the code where your exception happens if you don't pass anything, that would be rejected as an error immediately, without any of the `Board` code running. If you just want a standard chess board, you probably want `board = chess.Board.init_default()`. – Blckknght May 20 '21 at 18:20
  • Thank you it works now – Kartheek Nagamangalam Jun 09 '21 at 19:37

1 Answers1

0

You can use a better chess library, python-chess, and use the same code.
pip install chess

import chess
board = chess.Board()

I'm working on a chess AI in Python, and python-chess is very powerful. This library helps me to know information about the game, and I don't have to re-write the game. I just have to write the engine.

Note that this library is the most used in python chess, so if you've got some question about programming chess, more people will be able to help you.

LittleCoder
  • 391
  • 1
  • 13