类型错误:“Board”对象不能解释为整数

时间:2021-05-07 19:34:12

标签: python python-3.x typeerror

问题出在第 check_win(self, player): 行的 class Board: 中的 for row in range(self, board_rows): 函数中 我的代码在这里:

import pygame
from pygame.locals import *
from pathlib import *
import numpy as np

'''
---------
CONSTANTS
---------
'''
base_directory = Path(__file__).parent.absolute()
icon_path = 'tic_tac_toe_resources/images/tic_tac_toe.ico'
full_icon_path = base_directory / icon_path
width = height = 600
line_width = 14
board_rows = 3
board_columns = 3
box_size = width // board_columns
circle_radius = box_size // 3
cross_box_space = box_size // 4
circle_width = cross_width = 24
screen_color = (28, 170, 156)
line_color = (23, 145, 135)
circle_color = (1, 111, 124)
cross_color = (65, 65, 65)

class Board:
  def __init__(self):
    self.screen = pygame.display.set_mode((width, height))
    self.icon = pygame.image.load(full_icon_path)
    pygame.display.set_icon(self.icon)
    pygame.display.set_caption('Tic Tac Toe by ZamaaN')
    self.screen.fill(screen_color)
    self.console_board = np.zeros((board_rows, board_columns))

  def draw_board(self):
    #? first horizontal line
    pygame.draw.line(self.screen, line_color, (11, box_size), (width - 11, box_size), line_width)

    #? second horizontal line
    pygame.draw.line(self.screen, line_color, (11, 2 * box_size), (width - 11, 2 * box_size), line_width)

    #? first vertical line
    pygame.draw.line(self.screen, line_color, (box_size, 11), (box_size, height - 11), line_width)

    #? second vertical line
    pygame.draw.line(self.screen, line_color, (2 * box_size, 11),
                     (2 * box_size, height - 11), line_width)

  def draw_figures(self):
    for row in range(board_rows):
        for column in range(board_columns):
            if self.console_board[row][column] == 1:
                pygame.draw.circle(self.screen, circle_color, (int(column * box_size + box_size // 2), int(row * box_size + box_size // 2)), circle_radius, circle_width)

            elif self.console_board[row][column] == 2:
                pygame.draw.line(self.screen, cross_color, (column * box_size + cross_box_space, row * box_size + box_size - cross_box_space), (column *box_size + box_size - cross_box_space, row * box_size + cross_box_space), cross_width)

                pygame.draw.line(self.screen, cross_color, (column * box_size + cross_box_space, row * box_size + cross_box_space), (column *box_size + box_size - cross_box_space, row * box_size + box_size - cross_box_space), cross_width)

  def mark_box(self, row, column, player):
    self.console_board[row][column] = player

  def is_box_available(self, row, column):
    return self.console_board[row][column] == 0

  def is_board_full(self):
    for row in range(board_rows):
        for column in range(board_columns):
            if self.console_board[row][column] == 0:
                return False
    
    return True

  def draw_vertical_winning_line(self, column, player):
    position_x = column * box_size + box_size // 2

    if player == 1:
        vertical_winning_line_color = circle_color
    elif player == 2:
        vertical_winning_line_color = cross_color

    pygame.draw.line(self.screen, vertical_winning_line_color, (position_x, 14), (position_x, height - 14), 14)

def draw_horizontal_winning_line(self, row, player):
    position_y = row * box_size + box_size // 2

    if player == 1:
        horizontal_winning_line_color = circle_color
    elif player == 2:
        horizontal_winning_line_color = cross_color

    pygame.draw.line(self.screen, horizontal_winning_line_color, (14, position_y), (width - 14, position_y), 14)

  def draw_ascending_diagonal_winning_line(self, player):
    if player == 1:
        ascending_diagonal_winning_line_color = circle_color
    elif player == 2:
        ascending_diagonal_winning_line_color = cross_color

    pygame.draw.line(self.screen, ascending_diagonal_winning_line_color, (14, height - 14), (width - 14, 14), 14)

  def draw_descending_diagonal_winning_line(self, player):
    if player == 1:
        descending_diagonal_winning_line_color = circle_color
    elif player == 2:
        descending_diagonal_winning_line_color = cross_color

    pygame.draw.line(self.screen, descending_diagonal_winning_line_color, (14, 14), (width - 14, height - 14), 14)

  def check_win(self, player):
    #? vertical win check
    for column in range(board_columns):
        if self.console_board[0][column] == player and self.console_board[1][column] == player and self.console_board[2][column] == player:
            self.draw_vertical_winning_line(column, player)
            return True

    #? horizontal win check
    for row in range(self, board_rows):
        if self.console_board[row][0] == player and self.console_board[row][1] == player and self.console_board[row][2] == player:
            self.draw_horizontal_winning_line(row, player)
            return True

    #? ascending diagonal win check
    if self.console_board[2][0] == player and self.console_board[1][1] == player and self.console_board[0][2] == player:
        self.draw_ascending_diagonal_winning_line(player)
        return True

    #? descending diagonal win check
    if self.console_board[0][0] == player and self.console_board[1][1] == player and self.console_board[2][2] == player:
        self.draw_descending_diagonal_winning_line(player)
        return True

    return False

class Game_TicTacToe:
  def __init__(self):
    pygame.init()
    self.board = Board()
    self.board.draw_board()
    self.player = 1

  def restart(self):
    self.board.screen.fill(screen_color)
    self.board.draw_board()

    self.player = 1

    for row in range(board_rows):
        for column in range(board_columns):
            self.board.console_board[row][column] = 0
    
  def run(self):
    running = True
    game_over = False

    while running:
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    running = False
                elif event.key == K_RETURN:
                    self.restart()
                    game_over = False

            elif event.type == pygame.MOUSEBUTTONDOWN and not game_over:
                mouse_x = event.pos[0]
                mouse_y = event.pos[1]
                clicked_row = int(mouse_y // box_size)
                clicked_column = int(mouse_x // box_size)

                if self.board.is_box_available(clicked_row, clicked_column):
                    self.board.mark_box(clicked_row, clicked_column, self.player)
                    if self.board.check_win(self.player):
                        game_over = True
                    
                    self.player = self.player % 2 + 1
                    self.board.draw_figures()
            
            elif event.type == pygame.QUIT:
                running = False

        pygame.display.update()

if __name__ == '__main__':
    game = Game_TicTacToe()
    game.run()

我收到这些错误:

Traceback (most recent call last):
File "e:\archive_root\CSE\Python\portfolio_py\projects_py\games_py\tic_tac_toe_py\tic_tac_toe.py", line 192, in <module>
game.run()
File "e:\archive_root\CSE\Python\portfolio_py\projects_py\games_py\tic_tac_toe_py\tic_tac_toe.py", line 179, in run     
if self.board.check_win(self.player):
File "e:\archive_root\CSE\Python\portfolio_py\projects_py\games_py\tic_tac_toe_py\tic_tac_toe.py", line 124, in check_win
for row in range(self, board_rows):
TypeError: 'Board' object cannot be interpreted as an integer

1 个答案:

答案 0 :(得分:4)

那里的 self 是多余的(阅读:错误)。您只需要行数即可迭代它们:

for row in range(board_rows):
相关问题