-
-
Save mikezink/84d77edefbda55811058c1583ac8279c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from Graph import Graph | |
from pythonds.graphs import Graph | |
def genLegalMoves(x,y,bdSize): | |
newMoves = [] | |
moveOffsets = [(-1,-2),(-1,2),(-2,-1),(-2,1), | |
( 1,-2),( 1,2),( 2,-1),( 2,1)] | |
for i in moveOffsets: | |
newX = x + i[0] | |
newY = y + i[1] | |
if legalCoord(newX,bdSize) and \ | |
legalCoord(newY,bdSize): | |
newMoves.append((newX,newY)) | |
return newMoves | |
def legalCoord(x,bdSize): | |
if x >= 0 and x < bdSize: | |
return True | |
else: | |
return False | |
def knightGraph(bdSize): | |
ktGraph = Graph() | |
for row in range(bdSize): | |
for col in range(bdSize): | |
nodeId = posToNodeId(row,col,bdSize) | |
newPositions = genLegalMoves(row,col,bdSize) | |
for e in newPositions: | |
nid = posToNodeId(e[0],e[1],bdSize) | |
ktGraph.addEdge(nodeId,nid) | |
return ktGraph | |
def posToNodeId(row, column, board_size): | |
return (row * board_size) + column | |
kG = knightGraph(8) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment