Link to home
Start Free TrialLog in
Avatar of James Hancock
James HancockFlag for United States of America

asked on

Why is my Python abc setup giving errors?

Hi
My PyCharm is giving surprising red-underlining on import abc, object, and from abc import
Up til now, my chess engine has been working on a player class, but I'd like it to be an abstract base class. I am re-designing it
Must I do any special entries in the Project Settings panel? my code below doesn't seem to be a problem,



My abc is :
import abc

class ChessPlayer(object):
    __metaclass__= abc.ABCMeta

    @abc.abstractmethod
    def get_white_move(self, white_moves):
        """white player chooses the best move for this turn"""
        return

    @abc.abstractmethod
    def get_black_move(self, black_moves):
        """black player chooses the best move for this turn"""
        return

Open in new window

My derivative class is:
import abc
from abc_base import ChessPlayer:


class RandomPlayer(Object):
# ToBeDone, once setup is correct

Open in new window


Thanks
SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of James Hancock

ASKER

Thanks a lot
I've done what you suggested, and there are some errors that I don't understand.

I did the name change for abc_base.py:

My RandomPlayer.py does appropriate implementations of the methods:
import abc, random
from abc_base import ChessPlayer


class RandomPlayer(ChessPlayer):
    def __init__(self):
        pass

    def get_white_move(self, white_moves):
            len =white_moves.len()
            print('get_white_move()')
            random_move_index = random.randint(0,len)
            white_move = white_moves[random_move_index]
            return white_move


    def get_black_move(self, black_moves):
        len = black_moves.len()
        print('get_black_move()')
        random_move_index = random.randint(0, len)
        black_move = black_moves[random_move_index]
        return black_move

Open in new window


Why does the instantiation of a randomPlayer fail in the engine class? :
import RandomPlayer


class ChessEngine:
    def __init__(self):

        white_player = RandomPlayer()


print('main')



engine = ChessEngine()

Open in new window



Thanks
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks