需要1个参数(2个给定)错误?

时间:2015-11-27 22:02:15

标签: python methods

所以我得到了剧本:

    piece = raw_input(self.Player1["Name"] + ", pick a piece to move. Enter the coordinates (ex. A3, D4, etc.)." + "\n")
            coordinatesX = self.Chessboard.position_to_xCoor(piece) 
            coordinatesY = self.Chessboard.position_to_yCoor(piece)

其中position_to_xCoor是Chessboard对象中的一个方法,它是Gameboard类的一部分。

def position_to_xCoor(self, position):
    for i in range(0, 8):
        if listofrows[i] == position[0]:
            row = i + 1
    return row

它应该占据棋盘上的位置(例如A1,D3,B4)并返回x坐标(因此A将是1,C将是3,等等)。但是,每当我通过""输入到方法中,我得到一个"只接受一个参数"错误。我做错了什么?

Gameboard类:

class Gameboard(object):

    Matrix = [[0 for x in range(9)] for j in range(9)] 
    Board = [[0 for x in range(9)] for j in range(9)] 

    listOfColumns = ["A", "B", "C", "D", "E", "F", "G", "H"]

    def __init__(self):
        for row in range(0, 9):
            for column in range(0, 9):
                self.Matrix[row][column] = Square(row, column)
                self.Matrix[row][column].Occupied = True 

        for column in range(1, 9):
            self.addPiece("Black", "Pawn", 1, column)
            self.addPiece("White", "Pawn", 6, column)
        row = 0
        self.addPiece("Black", "Rook", row, 1)
        self.addPiece("Black", "Knight", row, 2)
        self.addPiece("Black", "Bishop", row, 3)
        self.addPiece("Black", "Bishop", row, 6)
        self.addPiece("Black", "Knight", row, 7)
        self.addPiece("Black", "Rook", row, 8)

        row = 7
        self.addPiece("White", "Rook", row, 1)
        self.addPiece("White", "Knight", row, 2)
        self.addPiece("White", "Bishop", row, 3)
        self.addPiece("White", "Bishop", row, 6)
        self.addPiece("White", "Knight", row, 7)
        self.addPiece("White", "Rook", row, 8)

        self.addPiece("White", "Queen", 7, 4)
        self.addPiece("White", "King", 7, 5)
        self.addPiece("Black", "Queen", 0, 4)
        self.addPiece("Black", "King", 0, 5)

    def __str__(self):
        row1 = 0
        for number in range(8, -1, -1):
            self.Board[row1][0] = number
            row1 += 1

        for row in range (0, 9):
            for column in range(1, 9):
                self.Board[row][column] = self.Matrix[row][column].Piece

        displayBoard = ""
        for row in range (0, 8):
            displayBoard += "   " + "---" * 13 + "\n"
            for column in range(0, 9):
                displayBoard += str(self.Board[row][column]) + " | "
            displayBoard += "\n"
        displayBoard += "   " + "---" * 13 + "\n"
        displayBoard += "   "
        for column in range(0, 8):
            displayBoard += "  " + self.listOfColumns[column] + "  "

        return displayBoard

    def addPiece(self, color, piece_type, row, column):
        Information = {
            "Type" : piece_type,
            "Color" : color,
            }
        self.Matrix[row][column].setPiece(ChessPiece(Information))

    @staticmethod
    def coor_to_position(a, b):
        row = self.listofrows[a]
        return row + str(b)

    @staticmethod   
    def position_to_xCoor(self, position):
        for i in range(0, 8):
            if listofrows[i] == position[0]:
                row = i + 1
        return row

    @staticmethod
    def position_to_yCoor(position):
        column = int(position[1])
        return column

    def validMove(color, piece, row1, column1, row2, column2):
        slopes = {
            "Bishop" : [1, -1], # can diagonally
            "Knight" : [2, -2, 0.5, -0.5],
            "King"   : [0, 1, -1, 10], #10 signifies the vertical movement
            "Pawn"   : [10],
            "Queen"  : [1, -1, 0, 10], # can move across the board
            "Rook"   : [0, 10], # can move across the board
        }

        if (column1 - column2) != 0: 
            slope = (row2 - row1)/(column2 - column1)
            if slope == any(self.slopes[piece.Type]): #Is the slope correct for the given piece?
                Occupied = False
                if piece.Type == "Bishop":
                    if slope == 1:
                        Occupied = blockedMove(color, piece, 1, 1, column1, column2, row1, row2)
                    else:
                        Occupied = blockedMove(color, piece, -1, 1, column1, column2, row1, row2)   

                if piece.Type == "Knight":
                    if (row2 - row1 == any([2, -2, 1, -1])):
                        if slope == any(slopes["Knight"]) and piece.Color != color:
                            Occupied = False
                if piece.Type == "King":
                    if (row2 - row1 == any([1, -1])):
                        if slope == any([-1, 1, 0]) and piece.Color != color:
                            Occupied = False
                if piece.Type == "Queen":
                    if slope == 1:
                        Occupied = blockedMove(color, piece, 1, 1, column1, column2, row1, row2)
                    if slope == -1:
                        Occupied = blockedMove(color, piece, -1, 1, column1, column2, row1, row2)
                    if slope == 0:
                        Occupied = blockedMove(color, piece, 0, 1, column1, column2, row1, row2)
                if piece.Type == "Rook":
                        Occupied = blockedMove(color, piece, 0, 1, column1, column2, row1, row2)
            if Occupied == False: #If all the spaces in the path are empty, return True
                return True
            else: 
                return False
        elif (column1 - column2) == 0:
            slope = 10
            if slope == any(self.slopes[piece.Type]): #Is the slope correct?
                Occupied = False
                if piece.Type == "King":
                    if (row2 - row1 == any([1, -1])) and piece.Color != color:
                        Occupied = False
                if piece.Type == "Pawn":
                    if color == "White":
                        if (row2 - row1 == any([1, 2])) and piece.Color != color:
                            Occupied = False
                    if color == "Black":
                        if (row2 - row1 == any([-1, -2])) and piece.Color != color:
                            Occupied = False
                if piece.Type == "Queen":
                    Occupied = blockedMove(color, piece, 1, 0, column1, column2, row1, row2)    
                if piece.Type == "Rook":
                    Occupied = blockedMove(color, piece, 1, 0, column1, column2, row1, row2)
            if Occupied == False: #If all the spaces in the path are empty, return True
                return True
            else: 
                return False

    def blockedMove(color, piece, rise, run, column1, column2, row1, row2):
        rise1 = rise
        run1 = run
        Occupied = False
        if column2 > column1:
            while row1 != row2 and column1 != column2 and Occupied == False:
                column1 += run
                row1 += rise
                if self.Matrix[column1 + run1][row1 + rise1].Occupied == True:
                    Occupied = True
        else:
            while row1 != row2 and column1 != column2 and Occupied == False:
                column1 -= run
                row1 -= rise
                if self.Matrix[column1 - run1][row - rise1].Occupied == True:
                    Occupied = True

        if column1 == column2 and row1 == row2 and piece.Color != color and Occupied == False:
            return False
        else: 
            return True

2 个答案:

答案 0 :(得分:3)

您的position_to_xCoor被装饰为静态,但将self作为第一个参数。静态方法没有自我论证。

答案 1 :(得分:1)

如果您使用

coordinatesX = self.Chessboard.position_to_xCoor(piece) 

然后你将self参数隐式地作为方法的第一个参数传递。由于这是一个静态方法,您应该使用:

coordinatesX = Gameboard.position_to_xCoor(piece)