Reputation: 9
So I've got the script:
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)
where position_to_xCoor is a method within the Chessboard object, which is part of the Gameboard class.
def position_to_xCoor(self, position):
for i in range(0, 8):
if listofrows[i] == position[0]:
row = i + 1
return row
Its supposed to take in a position on the chessboard (ex. A1, D3, B4) and return the x-coordinate (so A would be 1, C would be 3, etc.). However, whenever I pass the "piece" input into the method, I get a "takes in only one argument" error. What am I doing wrong?
The Gameboard class:
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
Upvotes: 1
Views: 744
Reputation: 112
If you use
coordinatesX = self.Chessboard.position_to_xCoor(piece)
then you're passing the self
argument implicitly as the first argument to the method. Since this is a static method you should be using:
coordinatesX = Gameboard.position_to_xCoor(piece)
Upvotes: 1
Reputation: 1089
Your position_to_xCoor is decorated as static, but takes self as the first argument. Static methods don't have a self argument.
Upvotes: 3