import pprint class Board: def __init__(self): self.board = [["r", "b", "n", "q", "k", "n", "b", "r"], ["p", "p", "p", "p", "p", "p", "p", "p"], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""], ["P", "P", "P", "P", "P", "P", "P", "P"], ["R", "Bb", "N", "Q", "K", "N", "B", "R"]] @staticmethod def isInsideBoard(x,y): if (x < 0) or (x > 8) or (y < 0) or (y > 8): return False else: return True ####### end of Board class class Bishop: def __init__(self, x, y): self.x = x # instance variable unique to each instance self.y = y def getDiagonalCoordinates(self): directions = [[1,1],[-1,1],[-1,-1],[1,-1]] moves = [] for direction in directions: #search for squares in every direction diagonally around the piece possible_x = self.x; #set the starting point back to the piece's actual location possible_y = self.y; for counter in range(0,8): possible_x = possible_x+direction[0]; possible_y = possible_y+direction[1]; if(Board.isInsideBoard(possible_x,possible_y)): #creates an instance, but it immediately dissapears. This IS NOT the main board used for the game, we're only using it for the helper method moves.append([possible_x,possible_y]) else: #the move isn't on the board. Don't add it to the moves list, and quit this loop break return moves def getPossibleMoves(self): possibleMoves = self.getDiagonalCoordinates() #later on subtract impossible moves due to other pieces here return possibleMoves def setPosition(self,x,y): self.x = x self.y = y ##### End of Bishop Class board = Board() #the copy of the board for the game. Not really used for anything right now B1 = Bishop(4,6) #make one instance of the bishop class and initialize it at a position B2 = Bishop(0,2) #make a different instance of the bishop class print("Current location if B1 - X: {} Y: {}".format(B1.x, B1.y)) pprint.pprint(B1.getPossibleMoves()) print("Current location if B2 - X: {} Y: {}".format(B2.x, B2.y)) pprint.pprint(B2.getPossibleMoves()) print("moving B1") B1.setPosition(1,3) print("Current location - X: {} Y: {}".format(B1.x, B1.y)) pprint.pprint(B1.getPossibleMoves())
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question