TypeError: 'NoneType' object is not callable
程序员文章站
2024-02-12 19:47:22
...
想写一个五子棋,但是在写一个类的时候,当不注释第八行的代码(self.is_end = None)
时,运行这段代码就会出现 TypeError: ‘NoneType’ object is not callable的错误,这是为什么呢
目前发现错误的原因:由于State类中,由于变量is_end和is_end函数使用了相同的名称,所以会出现调用出错,这是目前我发现的错误
import numpy as np
import time
BOzRD_ROWS = 15
BOARD_COLS = 15
class State:
def __init__(self):
self.data = np.zeros((BOARD_ROWS,BOARD_COLS))
# self.is_end = None 为什么在这里加这一句就会出现调用错误??
def next_state(self, row, col, symbol):
new_state = State()
new_state.data = self.data.copy()
new_state.data[row,col] = symbol
return new_state
# 最后落子在(row,col)处时是否结束比赛
def is_end(self, row, col, symbol):
directions = [[(-1,0),(1,0)],[(0,-1),(0,1)],[(-1,1),(1,-1)],[(-1,-1),(1,1)]]
for one_direction in directions:
count = 1
for direction in one_direction:
row_new = row
col_new = col
while True:
row_new += direction[0]
col_new += direction[1]
print(row_new,col_new)
if row_new < 0 or row_new > 14 or col_new < 0 or col_new > 14:
break
if self.data[row_new,col_new] == symbol:
count += 1
if count >= 5:
self.is_end = True
return self.is_end
else:
break
self.is_end = False
return self.is_end
def print_state(self):
for i in range(BOARD_ROWS):
out = str()
for j in range(BOARD_COLS):
if self.data[i,j] == 0:
token = '+ '
elif self.data[i,j] == 1:
token = '* '
else:
token = 'x '
out += token
print(out)
class Human_player():
def __init__(self):
self.state = None
self.symbol = None
def set_state(self, state):
self.state = state
def set_symbol(self, symbol):
self.symbol = symbol
def act(self):
location = input('please input you location: \n') # type: str
location_row = int(location.split(' ')[0]) - 1
location_col = int(location.split(' ')[1]) - 1
return [location_row, location_col, self.symbol]
def PVP():
state = State()
state.print_state()
player1 = Human_player()
player2 = Human_player()
player1.symbol = 1
player2.symbol = -1
while True:
[loc_row, loc_col, symbol] = player1.act()
state = state.next_state(loc_row,loc_col, symbol)
state.print_state()
is_end = state.is_end(loc_row, loc_col, symbol)
print(is_end)
if is_end:
return 0
[loc_row, loc_col, symbol] = player2.act()
state = state.next_state(loc_row, loc_col, symbol)
is_end = state.is_end(loc_row, loc_col, symbol)
state.print_state()
if is_end:
return 0
PVP()
推荐阅读
-
TypeError: 'NoneType' object is not callable
-
装饰器 TypeError: 'NoneType' object is not callable
-
解决:TypeError: 'list' object is not callable
-
报错:TypeError: 'NoneType' object is not callable问题解决
-
Debug3:使用DataFrame时报错TypeError: 'NoneType' object is not callable
-
TypeError: 'NoneType' object is not callable--python报错
-
subprocess 模块执行外部命令并获取它的输出&& TypeError: Object of type 'bytes' is not JSON serializable
-
U-Net运行报错merge6 = merge([drop4,up6], mode = 'concat'...) TypeError: 'module' object is not callable
-
【文件处理】——字典写入json文件或TXT文件,读取文件中的字典&TypeError: Object of type ‘ndarray‘ is not JSON serializable错误解决方法
-
【Tensorflow Erro】 TypeError: The value of a feed cannot be a tf.Tensor object.