[英]Function from class works when called directly, but returns error when called from a different class with an instance of original
我為一個名為 Game 的類編寫了一些代碼,該類使用了另一個名為 Board 的類。
Board
中的功能之一是check_horizontal_win(self, row, original_col)
。
當我直接在 Board 對象上使用它時,它工作正常。 當我嘗試將它與游戲板( Game.get_board().check_horizontal_win)
一起使用時,我得到:
AttributeError: 'function' object has no attribute 'check_horizontal_win'
我已經檢查過,我似乎沒有使用任何私人或非法的東西。 並且這兩種情況下的值是相同的。
這是第一堂課,游戲。 “main”中的最后一行代碼不起作用:
from board import *
PLAYER_A = 1
PLAYER_B = 2
INITIAL_ROW = 0
INITIAL_COLUMN = 0
FINAL_ROW = 5
FINAL_COLUMN = 6
ILLEGAL_LOCATION_MSG = "Illegal location."
ILLEGAL_MOVE_MSG = "Illegal move."
IS_GAME_WON = False
WINNER = None
class Game:
def __init__(self):
self.__current_player = PLAYER_A
self.__board = Board()
def make_move(self, column):
""" Makes move and updates board and current player, if column is a valid choice and game is ongoing. """
if self.__board.is_col_illegal(column):
raise Exception(ILLEGAL_MOVE_MSG)
self.do_move(column)
def do_move(self, column):
""" Actual implementation of the move. """
if self.__current_player == PLAYER_A:
self.__board.update_board(column, PLAYER_A)
self.__current_player = PLAYER_B
elif self.__current_player == PLAYER_B:
self.__board.update_board(column, PLAYER_B)
self.__current_player = PLAYER_A
def get_winner(self):
pass
def get_player_at(self, row, col):
""" Returns the player whose disc is at the given position in the game. """
if row < INITIAL_ROW or row > FINAL_ROW or col < INITIAL_COLUMN or col > FINAL_COLUMN:
raise Exception(ILLEGAL_LOCATION_MSG)
return self.__board.get_board()[row][col]
def get_current_player(self):
""" Returns current_player. """
return self.__current_player
def get_board(self):
""" Returns board."""
return self.__board
if __name__ == '__main__':
game = Game()
game.make_move(0)
game.make_move(1)
game.make_move(0)
game.make_move(2)
game.get_board.check_horizontal_win(0, 1)
這是 Board 類。 在這種情況下, main 中的函數確實有效:
NUM_ROWS = 6
NUM_COLUMNS = 7
INITIAL_VALUE = None
WIN_COUNT = 4
class Board:
def __init__(self):
self.__playboard = []
self.__available_rows_list = NUM_COLUMNS*[NUM_ROWS-1]
initial_row = NUM_COLUMNS * [INITIAL_VALUE]
for i in range(NUM_ROWS):
self.__playboard.append(initial_row.copy())
def get_playboard(self):
""" Returns board. """
return self.__playboard
def update_board(self, col, val):
""" Updates current status of board. """
row = self.__available_rows_list[col]
self.__playboard[row][col] = val
self.__update_row(col)
def __update_row(self, col):
""" Updates available_row_list. """
if self.__available_rows_list[col] > 0:
self.__available_rows_list[col] = self.__available_rows_list[col] - 1
else:
self.__available_rows_list[col] = None
def __is_col_available(self, col):
""" Checks if given col has empty spaces left on the playboard. """
if self.__available_rows_list[col] == None:
return False
return True
def __is_col_exist(self, col):
""" Checks if given column is within the capacity of the playboard. """
if col < 0 or col >= NUM_COLUMNS:
return False
return True
def is_col_illegal(self, col):
""" Checks if given column is an illegal option. """
if not self.__is_col_available(col) or not self.__is_col_exist(col):
return True
return False
def print_playboard(self):
for row in self.__playboard:
print(row)
def check_horizontal_win(self, row, original_col):
""" Checks if player has won in the horizontal direction. """
# check if row and col were valid? Or do it somewhere else?
count = 1
player = self.__playboard[row][original_col]
col = original_col
while self.__is_col_exist(col) and self.__playboard[row][col] == player:
count = count + 1
if count == WIN_COUNT:
return True
col = col + 1
# Then: undergo same process, this time in the opposite direction (the left).
col = original_col - 1
while self.__is_col_exist(col) and self.__playboard[row][col] == player:
count = count + 1
if count == WIN_COUNT:
return True
col = col - 1
if __name__ == '__main__':
board = Board()
board.update_board(0,1)
board.update_board(0, 1)
board.update_board(1, 2)
board.update_board(2, 2)
board.print_playboard()
board.check_horizontal_win(0, 1)
你寫了:
game.get_board.check_horizontal_win(0, 1)
做到這一點:
game.get_board().check_horizontal_win(0, 1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.