0

Current section I'm working on requires me to take coloured pieces from the bar and place them onto a board.

I've had to define the board through makeBoard and the values are stored in "myBoard"

What I'm struggling with is that on my next section "enterPiece" I've got it so I successfully take the relevant coloured pieces from the bar, detract it from the number and then allocate the piece to the board..

what I'm aiming to do is.. 0 < aPoint <= n (where n is the size of the board where I've defined in makeBoard), but I don't know how to get python to get the n from the variable myBoard

def enterPiece(aBoard, aColour, aPoint):
    c = aBoard
    if 0 < aPoint:
        for j in range(aPoint):
            c.removePieceFromPoint(aColour, 0)
            c.addPieceToPoint(aColour, aPoint)
            return True
        else:
            return False
Adam Smith
  • 52,157
  • 12
  • 73
  • 112

1 Answers1

4

it looks like n is Board.size, so you should be able to rewrite as:

def enterPiece(aBoard, aColour, aPoint):
    c = aBoard
    if 0 < aPoint <= aBoard.size:
        for j in range(aPoint):
            c.removePieceFromPoint(aColour, 0)
            c.addPieceToPoint(aColour, aPoint)
            return True
        else:
            return False
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • Not correct, here is defined getter `getSize`. If any getter is defined, he should be preferred more that direct variable access – Reishin Oct 09 '15 at 17:54
  • 2
    @Reishin maybe in other languages, but in Python he should instead just remove that getter. Getters and setters are an anti-pattern in Python. If you need their functionality, you should use [`@property`](https://docs.python.org/2/library/functions.html#property) – Adam Smith Oct 09 '15 at 17:54
  • no! if size is not supposed to be changed, that getter need to be present! And the code above mentioned that. The only one error there is that variable have name `size` not `_size` – Reishin Oct 09 '15 at 17:56
  • stop what?! Anti-Pattern? Please proof! – Reishin Oct 09 '15 at 17:57
  • 1
    @Reishin that's what properties are for. If it's not defined as a property and `size` isn't documented in any way as being a constant, then why assume it should be? More info http://stackoverflow.com/questions/2627002/whats-the-pythonic-way-to-use-getters-and-setters – Adam Smith Oct 09 '15 at 17:58
  • Well that's is only opinion from some one person without any normal argumentation, even some ppl agree. I'm not. But the thing that properties exists in python tells that they are well pythonic or supposed to be used to control r/w rights. About documented, why you decide that variable is not supposed to be constant? There is no doc that she is need to be variable too. – Reishin Oct 09 '15 at 18:06
  • 1
    @Reishin & Adam - I've tried .getSize and for some reason it doesnt work, but size does, I'll look to remove the getter –  Oct 09 '15 at 18:10
  • 1
    @JackCrane that's the normal way of doing things in Python. All attributes are accessible publicly anyway, so just access the attribute. If you needed to do something *special* with it, you could make it a property, but Reishin's assertion that the matter is somehow under debate is rather silly. – Adam Smith Oct 09 '15 at 19:59
  • well, you should try .getSize() , not .getSize – Reishin Oct 09 '15 at 20:03
  • @AdamSmith it's a big problem, that ppl thinking that can do everything in python without correct architecture of application. This makes code well unreadable, buggy and working really in unexpected. I'm not talking about little application in 100 lines, real application begins from 2k code lines and deep structure of classes. And in this case, public fields will play with you a bad game. – Reishin Oct 09 '15 at 20:08
  • 1
    @Reishin I'd look up some details of the language and see some working examples before writing off the massively popular language. You don't seem to have a strong enough grasp of Python to be able to judge its merits. If you require private fields in your objects, then Python will NEVER work for you (even implementing `Board._size` and `Board.getSize` does not stop other code from doing `b = Board(6); b._size = 12`.) – Adam Smith Oct 09 '15 at 20:18
  • 1
    @Reishin the closest thing to a "private" member attribute is using the double underscore prefix `__size`, but even that only does name mangling so `b = Board(6); b._Board__size = 12` still works. It's literally impossible in Python, but it doesn't cause problems for the thousands of mature applications written in the language. More information from the docs [can be found here](https://docs.python.org/2/tutorial/classes.html#private-variables-and-class-local-references) – Adam Smith Oct 09 '15 at 20:19