- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的国际象棋程序中,我有一个名为 Move 的类。它存储了工件被取出和放置的位置。这是什么棋子以及被捕获的棋子是什么。
问题是,为了获取被移动和捕获的棋子,我将整个 Board 对象传递给 __init__ 方法。因此,在我看来,我似乎应该将所有 Move 类方法存储到 Board 类,该类也有一个方法可以获取给定方 block 上的棋子。
我刚刚开始学习面向对象,因此非常感谢有关此的一些建议,也许还有一些更通用的设计决策。
这是我认为最好省略的 Move 类?
class Move(object):
def __init__(self, from_square, to_square, board):
""" Set up some move infromation variables. """
self.from_square = from_square
self.to_square = to_square
self.moved = board.getPiece(from_square)
self.captured = board.getPiece(to_square)
def getFromSquare(self):
""" Returns the square the piece is taken from. """
return self.from_square
def getToSquare(self):
""" Returns the square the piece is put. """
return self.to_square
def getMovedPiece(self):
""" Returns the piece that is moved. """
return self.moved
def getCapturedPiece(self):
""" Returns the piece that is captured. """
return self.captured
最佳答案
当您创建一个对象时,您正在创建一个事物。棋盘和棋盘上的棋子都是东西。当您希望与这些事物交互时,您需要一种方式来做到这一点 - 或一个动词。
这仅作为建议方法,以避免使用 Move
类。我打算做什么:
我从编写Board
开始;您可以稍后决定如何表示位置。
class Board:
def __init__(self):
self.board = [['-' for i in xrange(8)] for j in xrange(8)]
# You would have to add logic for placing objects on the board from here.
class Piece:
def __init__(self, name):
self.name = name
def move(self, from, to):
# You would have to add logic for movement.
def capture(self, from, to):
# You would have to add logic for capturing.
# It's not the same as moving all the time, though.
class Pawn(Piece):
def __init__(self, name=""):
Piece.__init__(self, name)
def move(self, from, to):
# A rule if it's on a starting block it can move two, otherwise one.
def capture(self, from, to):
# Pawns capture diagonal, or via en passant.
关于python - 国际象棋程序OO设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11711860/
我正在尝试开发一个简单的国际象棋引擎,但我一直在为它的性能而苦苦挣扎。我已经实现了带有alpha-beta修剪和迭代加深的Negamax(没有任何其他启发式方法),但是我无法获得超过3-4层的合理搜索
我目前正在开发国际象棋引擎,目前该引擎一直在工作,但要花很多时间才能产生移动。由于必须生成许多移动,因此检查检测花费的时间最长。 我尝试了很多事情后陷入困境,无法真正弄清楚如何提高效率。这是我的方法:
我正在尝试用 Java 为我的游戏板(国际象棋)建立初始状态。但是,当我运行该程序时,板似乎被翻转了?如果您查看初始状态方法,这就是我希望初始板启动的方式: public void initialBo
我正在尝试实现Knight-Distance中描述的绝对排名文件距离。来自Chess Programming Wiki ,但我对 ints a 和 b 应该在什么地方有点困惑 int knightDi
我一直在研究表格,以便可以使用 html 中的表格创建棋盘。这是我在网上找到的一些代码:HTML 代码: ♜ ♞ ♝ &
我有如下算法来防止玩家选择将其置于检查状态的移动: 假走(通过复制棋盘并在复制的棋盘上工作) 在走法被伪造之后,得到所有合法的对手走法。 如果合法走法之一可以攻击玩家的国王,则不能选择最初伪造的走法。
如果没有图片,这可能有点难以解释,但是,我正在检查国王是否在检查中。为此,我从国王的位置开始向上、向左、向下、向右,然后是所有对角线图案。 为了简化我的代码,我有一个路径检查器方法,它接受起始位置和结
我已经为我的国际象棋引擎实现了带有静态搜索的 alpha-beta 搜索。然而,在大多数情况下,静态搜索占用了总执行时间的 80-90%,正如我的分析器所显示的那样。我的修剪有问题吗? 我已经包含了
我正在编写一个简单的国际象棋游戏。我不会在这里全部发布,但我会为您提供必要的详细信息。 我通过点击一个上面有棋子的方 block 来移动,然后这个方 block 被选中,然后点击我想要棋子的地方移动。
嗨! 我正在尝试为我的国际象棋引擎编写一个 negamax 搜索算法,但我似乎无法让它工作。我以 wikipedias 伪代码为例,但不知何故它没有产生预期的结果。当我用 2 层运行它时,它改变了我的
我正在对国际象棋 AI 进行试验,目前正在尝试检测平局的可能性。 如果出现以下情况,玩家可以要求平局: 在每个玩家的最后 50 步棋中没有吃到或走棋。 相同的棋盘位置出现了 3 次。 因此,程序必须存
我正在用 Java 开发国际象棋程序并考虑以下问题: Rook 部分需要实现直线移动。 Bishop 部分需要实现沿对角线移动。 但是, Queen piece 需要实现上述两种运动模式。 我想不出一
我有一个问题。 我们有无限的棋盘,如何计算 n 次跳跃后你可能发现自己跳跃的一半的数量? 例如: 一次跳跃后我们有8个盒子 两次跳跃后我们有33个盒子 #include using namespace
这个问题已经有答案了: How do I generate all of a knight's moves? (10 个回答) 已关闭 9 年前。 我正在尝试根据当前位置获取骑士可以放置的所有可能位置
好吧,我先承认这个会有点长。我正在为 C# 编写国际象棋引擎,最终目标包括 UCI 实现。我已经做到了,给定一个棋盘,引擎将生成所有有效 Action 的列表;然而,我的评估代码似乎很挣扎,因为在与自
我正在实现一个国际象棋引擎,我已经编写了一个相当复杂的 alpha-beta 搜索例程,其中包含静态搜索和换位表。但是,我观察到一个奇怪的错误。 评估函数使用的是方 block 表,就像这个用于棋子的
我正在编写一个基本的 Java 国际象棋游戏并编写了以下类:Game、Player、Board、Square、Piece(每个特定棋子的父类(super class))和每个特定棋子类(例如 Pawn
我正在为类似于国际象棋的游戏编写 AI。棋盘为 10x10,每面 15 block 都有象棋相似的走法。 游戏中的一切都组织在对象中。瓷砖[][] 瓷砖; 10x10,每个 Tile 都有一个 pie
我正在制作游戏国际象棋,几乎得到了所有东西,但只有一件事:我需要制作它,这样玩家就不可能将棋子移动到检查中。我在解决这个问题时遇到了麻烦。 我现在用伪代码生成有效移动的是:getMoveLocatio
我正在尝试使用 alpha beta 剪枝实现一个国际象棋游戏。以下几乎可以正常工作,但它会返回错误的 Action 。 例如,可能会发生以下情况。 白色(用户)移动,白色王位 - a1/黑色(计算机
我是一名优秀的程序员,十分优秀!