0

I'm creating a chess game in which I decided to implement two class, the piece class an abstract class extends to various abstract operations(Pawn, King, Queen, Rook, Knight, Bishop), and every piece will be placed on a spot. The spot class represents one block of the 8x8 grid and an optional piece. In my spot class I take in an instance of the piece class in the constructor but I am getting an error "No statement effect errors". I am not sure why?

class Spot:

    def __init__(self, x, y,Piece piece):
        self.x = x
        self.y = y
        self.piece = piece

class Piece:
    killed = False
    white = False
    def __init__(self, white,killed):
        self.white = white
        self.killed = killed
    def iswhite(self):
        return self.white == True
    def iskilled(self):
        return self.killed == True
C.Nivs
  • 12,353
  • 2
  • 19
  • 44
jona
  • 11
  • 2
    I don't think `Piece piece` is valid. Can you show what your error message is? And what you are doing to create that error message? – C.Nivs Jul 13 '20 at 21:40
  • 2
    What error are you encounter **exactly**? Please post the full error message including the stack trace. Note, the above code wouldn't even *compile* and would throw a SyntaxError due to `Piece piece`. – juanpa.arrivillaga Jul 13 '20 at 21:47

1 Answers1

1

In Python, you don't need to declare the type of your arguments, like in Java, C#, C++, etc. Python is dynamically typed, so the interpreter will figure out what objects you're passing during run time.

Change your code as follows:

class Spot:
    def __init__(self, x, y, piece):
        self.x = x
        self.y = y
        self.piece = piece

If you really want to specify the data types, you can use a feature of Python called type hinting as follows:

class Spot:
    def __init__(self, x: int, y: int, piece: Piece):
        self.x = x
        self.y = y
        self.piece = piece

A few other pointers:

  • You don't need to compare boolean operators using x == True, you can simply return the boolean variable x, which will have the same effect.
  • You should always use snake_case in Python, so is_white instead of iswhite.
  • You don't need to initialize the variables in the Piece class like that. You can do that in the __init__ method directly using default arguments. So if a user does not provide the argument, the default value will be used for that argument.
class Piece:

    def __init__(self, white=False, killed=False):
        self.white = white
        self.killed = killed

    def is_white(self):
        return self.white

    def is_killed(self):
        return self.killed
DV82XL
  • 5,350
  • 5
  • 30
  • 59
  • @jona does this response answer your question? If so please click the green check mark on the left to indicate that it has been answered. – DV82XL Jul 15 '20 at 23:36