Python - chess engine start giving astonishing errors!

I am bewildered by some of my Python bugs today, with a new approach of my Python chess engine.

The ChessBoard class doesn't connect with my ChessPiece data

The board tries to instantiate ChessPiece objects that are a white pawn, like ChessPiece.PIECE_PAWN
PIECE_PAWN is a totally valid data member in the ChessPiece class,
but the error states it is a non-existent attribute - PIECE_COLOR... too

Maybe I'm being poisoned by my Java old habits, eg there are no public static equivalents

How does my code look, also, style wise? My code must be stylistically proper! What have I missed, attached?
Does my ABC player look okay?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Florian LenschowCommented:
In you import the module ChessPiece and try to access it. You need to use
from ChessPiece import ChessPiece as cp 

Open in new window

using the alias to avoid namespace pollution. This gives you the actual class. Then you can access your class using cp.Chesspiece.PIECE_PAWN. If you want to access your class the way you did you just skip the as cp part, but you need to make sure you dont import anything that isn't already defined in the current namespace

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Florian Lenschow gave you the solution.

This is just an additional comment. (I don't need points for this)

If your file name would have followed the 'only lower case and underscore naming convention' you'd never fallen into this particular issue.

But even some official python packets have similiar issues despite a 'standard file name naming convention'  (as the packet name (file name) is identical to an object within this packet)

I'm not a big fan of this, but you will encounter it  and it can create confusion for persons reading the code.

One of the official modules having that issue is the datetime packet, which has an object named datetime inside.

some people do

import datetime # just import the package and prefix each object form the packet
now =
today =

Open in new window

others do
from datetime import datetime #don't import he packet, but just the object datetime
from datetime import date
now =
today =

Open in new window

beavoidAuthor Commented:
It worked.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Just saw now, that you asked also to look at your code.

You might do following for your chess board:

class ChessBoard:
    def __init__(self):
        """ create empty 8x8 board and populate it with chess pieces  """

        # create empty board
        board = [[None for i in range(8)] for j in range(8)]

        # white, black pawns
        for i in range(8):
            board[1][i] = ChessPiece(ChessPiece.PIECE_PAWN, ChessPiece.COLOR_WHITE)
            board[6][i] = ChessPiece(ChessPiece.PIECE_PAWN, ChessPiece.COLOR_BLACK)

        # add major pieces for black and white
        for row, color in [ (0, ChessPiece.COLOR_WHITE), (7, ChessPiece.COLOR_BLACK):
            board[row][0] = ChessPiece(ChessPiece.PIECE_ROOK, color)
            board[row][1] = ChessPiece(ChessPiece.PIECE_KNIGHT, color)
            board[row][2] = ChessPiece(ChessPiece.PIECE_BISHOP, color)
            board[row][3] = ChessPiece(ChessPiece.PIECE_QUEEN, color)
            board[row][4] = ChessPiece(ChessPiece.PIECE_KING, color)
            board[row][5] = ChessPiece(ChessPiece.PIECE_BISHOP, color)
            board[row][6] = ChessPiece(ChessPiece.PIECE_KNIGHT, color)
            board[row][7] = ChessPiece(ChessPiece.PIECE_ROOK, color)

Open in new window

For chessgame nothing really different, just slightly different white spaces and the suggestion, that the creation of an
object does not immediately start the game.

I personally would separate game creation and game start. It is only one line more to write and gives you more freedom in
some cases.

import ChessBoard

class ChessGame:
    def __init__(self, white_player, black_player):
        """ takes a freshly populated chess board
            and assigns players

        self.board = ChessBoard.ChessBoard()
        self.white_player = white_player
        self.black_player = black_player

    def begin_game(self) :
        print('game loop, white move')

if __name__ == "__main__":
    game = ChessGame(None, None)

Open in new window

same for chess player only minor white space / case changes.

from abc import ABC, abstractmethod

class ChessPlayer(ABC):

    def __init__(self, color):

        self.color = color
        self.move_count = 0

    def get_white_move(self, board, moves):

    def get_black_move(self, board, moves):

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.