移动角色并将移动应用到棋盘上

发布于 2024-11-08 17:12:23 字数 747 浏览 0 评论 0原文

大家好,我之前问过一个关于如何创建的问题一个用Python编写的棋盘游戏的二维网格,现在在制作这个棋盘游戏时遇到了另一个问题。

我想做的是移动棋盘上的棋子(数字或字母),并将该移动应用到棋盘上,以便下次轮到同一个玩家时。 基本上,

1 . . . .
2 . . . .
3 . . . .
4 . . . .
. a b c d
Letters' turn: an
1 . . . .
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

你明白了......

我有棋盘,但是,我不知道如何移动棋子并将它们保持在适当的位置,以便它们可以在下一轮继续移动。

我有职能:

def doMove(currentPlayer, board):
def isValidMove(board, move):
and def applyMove(board, move):

您能提供的任何建议都会很棒!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云柯 2024-11-15 17:12:23

我相信你缺少 python 中类的一个关键特性:内部状态。这是一个简单的示例:

class Counter():
  def __init__(self):
    self.internalCount = 0;
    return
  def addToInternalCount(self, val):
    self.internalCount = self.internalCount + val
    return self.internalCount

请注意,如果我创建一个新的计数器,我可以多次调用 addToInternalCount,并且内部计数数据在调用之间保持不变。另请注意,就状态而言,类没有什么特别之处。函数外部的任何变量都可能已更新,并且该值将在调用期间保持不变。

InternalCount 有什么特别之处吗?并不真地。这可以很容易地表示 x 轴上的位置。请尝试一下。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文