Python - chess engine start giving astonishing errors!

Hi
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 members.ChessBoard.pyChessPiece.pyChessGame.pyChessPlayer.py

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?

Thanks
LVL 1
beavoidAsked:
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 ChessBoard.py 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
1

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
gelonidaCommented:
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 = datetime.datetime.now()
today = datetime.date.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 = datetime.now()
today = datetime.today()

Open in new window

0
beavoidAuthor Commented:
Thanks.
It worked.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

gelonidaCommented:
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

0
gelonidaCommented:
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__":
    print('main')
    game = ChessGame(None, None)
    game.begin_game()

Open in new window

0
gelonidaCommented:
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

    @abstractmethod
    def get_white_move(self, board, moves):
        pass

    @abstractmethod
    def get_black_move(self, board, moves):
        pass

Open in new window

0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.