编辑:问题解决了,我确实是个白痴,这确实是一个非常愚蠢的错误。解决的办法是在创建Joueur实例时忘记了提供UI实例。对不起,感谢所有尝试帮助我的人。
我一直在尝试下象棋游戏。我现在正在测试中,并且遇到了一个“怪异”错误(根据我的经验,我可能只是搞砸了,但我的情况并不完全是其他人对此错误的看法,我一直在搜索我的代码和说明文件的线索持续了几个小时没有成功:所以我在这里。
所以总结一下代码(我不会把以前已经成功运行过的代码放进去,只会显示应该相关的内容:]
我有一个董事会课程,基本上是我的模型和我的控制器。在构造函数中,我要求提供两个播放器,一个UI类和一个哈希类作为参数。在实现最后两个并将其添加到构造函数中之前,代码可以正常运行。
class Plateau:
def __init__(self, j1, j2, UI, Hash):
self.UI = UI
self.UI.ajoutePlateau(self)
self.Hash = Hash
self.Hash.hashPlateau(self)
# some code to deal with the rest
...
#
self.UI.initAffichage()
然后,我有一个UI界面和一个从其继承的ConsolUI(代码中的UITerminal)。可以预期的是,向用户展示了什么牌,以及向人类玩家(如果有)问他想玩什么的内容。
class UI:
def __init__(self):
self.Plateau = None
def ajoutePlateau(self, Plateau):
self.Plateau = Plateau
# a bunch of method that throw an exception and are overrided by the child class
...
#
class UITerminal(UI):
def __init__(self):
#super(UITerminal, self).__init__(self)
#super().__init__(self)
#UI.__init__(self)
#super(UITerminal, self).__init__()
super().__init__()
#UI.__init__()
# the method that had to be overridden
...
#
我还尝试了几个版本的UITerminal构造函数(在上面并加注释)。甚至什么也没有,因为甚至都不需要(我认为...)。
然后有哈希,其构建方式与UI相同:接口,子级。
class Hash:
def __init__(self):
pass
# a bunch of method that throw an exception and are overridden by the child class
...
#
class ZombristHash(Hash):
def __init__(self):
#super(ZombristHash, self).__init__(self)
#super().__init__(self)
#Hash.__init__(self)
#super(ZombristHash, self).__init__()
super().__init__()
#Hash.__init__()
# a bunch of code to init the zombristhash
...
#
与UI相同,我尝试了多种方法来调用接口构造函数。
然后我有了我的主线,它只有1行,是引发错误的原因:
p = Plateau(Humain("j1"), Humain("j2"), UITerminal(), ZombristHash())
错误是:
Traceback (most recent call last): File "plateau.py", line 298, in <module> p = Plateau(Humain("j1"), Humain("j2"), UITerminal(), ZombristHash())
TypeError: __init__() missing 1 required positional argument: 'UI'.
据我了解,他告诉我我没有给开发板构造函数一个UI作为参数,但是我这样做了,所以我不明白发生了什么。
我尝试了quamran的建议:
p = Plateau(None, None, None, None)
而且似乎认为现在还可以...
Traceback (most recent call last):
File "plateau.py", line 298, in <module>
p = Plateau(None, None, None, None)
File "plateau.py", line 13, in __init__
self.UI.ajoutePlateau(self)
AttributeError: 'NoneType' object has no attribute 'ajoutePlateau'
正如@Jacques Gaudin所注意到的,您需要更改此:
class ZombristHash(Hash):
def __init__(self):
Hash.__init__()
对此:
class ZombristHash(Hash):
def __init__(self):
super().__init__()
与UI相同。
最后,您得到的是这样的东西:
class Plateau:
def __init__(self, j1, j2, UI, Hash):
self.UI = UI
self.UI.ajoutePlateau(self)
self.Hash = Hash
class Hash:
def __init__(self):
pass
class ZombristHash(Hash):
def __init__(self):
super().__init__()
class UI:
def __init__(self):
self.Plateau = None
def ajoutePlateau(self, Plateau):
self.Plateau = Plateau
class UITerminal(UI):
def __init__(self):
# super(UITerminal, self).__init__()
super().__init__()
class Humain():
def __init__(self, j):
pass
p = Plateau(Humain("j1"), Humain("j2"), UITerminal(), ZombristHash())