Moving characters and applying move to board
Hey everyone, I previously asked a question on how to create a 2-D grid for a board game in Python and have now encountered another problem with making this board game.
What i'm trying to do is move pieces (numbers or letters) on my board and apply that move to the board for when it's that same players turn next. Basically,
1 . . . .
2 . . . .
3 . . . .
4 . . . .
. a b c d
Letters' turn: an
1 开发者_JAVA技巧. . . .
2 . . . .
3 . . . .
4 a . . .
. . b c d
Numbers' turn: 1e
. 1 . . .
2 . . . .
3 . . . .
4 a . . .
. . b c d
Letters' turn: an
. 1 . . .
2 . . . .
3 a . . .
4 . . . .
. . b c d
You get the idea...
I have the board but, I can't figure out how to move the pieces and keep them in place so they can continue their move on the next turn.
I have functions:
def doMove(currentPlayer, board):
def isValidMove(board, move):
and def applyMove(board, move):
Any advice you could lend would be great!
I believe you are missing a key feature of classes in python: internal state. Here's a simple example:
class Counter():
def __init__(self):
self.internalCount = 0;
return
def addToInternalCount(self, val):
self.internalCount = self.internalCount + val
return self.internalCount
Note that if I create a new Counter, I can call addToInternalCount many times and the internalCount data persists across calls. Also note that there is nothing special about a class as far as state goes. Any variable external to the function could have been updated and the value would have persisted across calls.
Is there anything special about internalCount? Not really. That could just as easily represent the position on the x-axis. Please have a go at it.
精彩评论