【问题标题】:NoneType' object has no attribute 'winner' PYTHON CHECKERS GAMENoneType' 对象没有属性 'winner' PYTHON CHECKERS GAME
【发布时间】:2020-11-26 14:17:22
【问题描述】:

以下是用于使用 python 制作跳棋 AI 的游戏类的代码,我不确定解决方案是什么,因为我已经尝试过推荐的方法。任何解决方案将不胜感激

import pygame
from constants import RED, WHITE, BLUE, SQUARE_SIZE
from board import Board


class Game:     # game class allows you to interface with the board and the pieces. it consists of a few methods
    def __init__(self, win):
        self._init()
        self.win = win

    def update(self):
        self.board.draw(self.win)
        self.draw_valid_moves(self.valid_moves)
        pygame.display.update()

    def _init(self):
        self.selected = None
        self.board = Board()
        self.turn = RED
        self.valid_moves = {}

    def winner(self):
        return self.board.winner()

    def reset(self):        # reset method to reset
        self._init()

    def select(self, row, col):     # if i haave selected something then this method will try and move what i have selected to the row and colimn selected. this select metho
        if self.selected:       # pressed piece will move to row/ column passed
            result = self._move(row, col)
            if not result:      # if the row oclumn was valid, as i already have something slected, then it will be moved, if not i will be able to try and select a different piece
                self.selected = None
                self.select(row, col)       # call method again

        piece = self.board.get_piece(row, col)
        if piece != 0 and piece.color == self.turn:
            self.selected = piece
            self.valid_moves = self.board.get_valid_moves(piece)
            return True

        return False

    def _move(self, row, col):# move method
        piece = self.board.get_piece(row, col)
        if self.selected and piece == 0 and (row, col) in self.valid_moves:# cannot move into a postioon that has another piece checks to see if piece sleected ( only if we have selcted sometihng and what we have sleceted is not another piece then move can be moved/ can move)
            self.board.move(self.selected, row, col)#movw selected peice to row and column
            skipped = self.valid_moves[(row, col)]
            if skipped:
                self.board.remove(skipped)
            self.change_turn()
        else:
            return False

        return True

    def draw_valid_moves(self, moves):#check all of the diagonals of where that checkers piece is. draw squares for all of these valid moves. to do this the row/ column need to be identiified so that a circle can be drawn for each move
        for move in moves:      # loops through all the keys of the dictionary which is the row/ columns indenitidied as a tuple
            row, col = move     # all moves are dictionary,
            pygame.draw.circle(self.win, BLUE,
                               (col * SQUARE_SIZE + SQUARE_SIZE // 2, row * SQUARE_SIZE + SQUARE_SIZE // 2), 15)

    def change_turn(self):      # change turn of players
        self.valid_moves = {}
        if self.turn == RED:
            self.turn = WHITE
        else:
            self.turn = RED
            # in order to implement the AI the game and board class are the most funcdemental aspects, this is because when implementing where the AI can actually move from a certain postion to do this a board needs to be established to score/ eveaulate the postions and know all the piece that could potenitally move

    def get_board(self):
        return self.board

    def ai_move(self, board):
        self.board = board
        self.change_turn()

【问题讨论】:

    标签: python pygame youtube artificial-intelligence minimax


    【解决方案1】:

    这可能是因为你拼错了构造方法。在 init 之前和之后需要两个下划线,如下所示:

    def __init__(self):
        self.selected = None
        self.board = Board()
        self.turn = RED
        self.valid_moves = {}
    

    【讨论】:

    • 您好,我尝试了您建议购买的东西,我认为这不是问题,因为它给了我另一个错误,指出“TypeError:__init__() 恰好需要 1 个参数(给定 2 个)”.. 有什么想法吗?
    • 这条消息的意思是,你用一个参数调用构造函数,但你的方法只定义了self(这是隐式传递的,没有额外的参数。然后你需要修复__init__并添加第二个参数。
    猜你喜欢
    • 2013-04-19
    • 2019-10-03
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多