Diagonal Movement Code for Python Chess Engine

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












I'm currently in the process of building my own Chess game engine and could really use some suggestions on how to make this segment of code for calculating diagonal moves more efficient. (This is obviously only for diagonals going Up-Right.)



As of now I'm using "Try-Except" to iterate by 1, and then my return statement filters out any off-board values. However this seems like a very bulky way of doing things.



import argparse, json

chessBoard = [[1, 1, 1, 1, 1, 1, 1, 1] for i in range(8)]

chess_map_from_alpha_to_index =
"a" : 0,
"b" : 1,
"c" : 2,
"d" : 3,
"e" : 4,
"f" : 5,
"g" : 6,
"h" : 7


chess_map_from_index_to_alpha =
0: "a",
1: "b",
2: "c",
3: "d",
4: "e",
5: "f",
6: "g",
7: "h"



def getBishopMoves(pos, chessBoard):
column, row = list(pos.strip().lower())
row = int(row) - 1
column = chess_map_from_alpha_to_index[column]
i,j = row, column
solutionMoves =

#Up-Right Diagonal
try:
temp = chessBoard[i + 1][j + 1]
solutionMoves.append([i + 1, j + 1])
except:
pass
try:
temp = chessBoard[i + 2][j + 2]
solutionMoves.append([i + 2, j + 2])
except:
pass
try:
temp = chessBoard[i + 3][j + 3]
solutionMoves.append([i + 3, j + 3])
except:
pass
try:
temp = chessBoard[i + 4][j + 4]
solutionMoves.append([i + 4, j + 4])
except:
pass
try:
temp = chessBoard[i + 5][j + 5]
solutionMoves.append([i + 5, j + 5])
except:
pass
try:
temp = chessBoard[i + 6][j + 6]
solutionMoves.append([i + 6, j + 6])
except:
pass
try:
temp = chessBoard[i + 7][j + 7]
solutionMoves.append([i + 7, j + 7])
except:
pass
try:
temp = chessBoard[i + 7][j + 7]
solutionMoves.append([i + 7, j + 7])
except:
pass

temp = [i for i in solutionMoves if i[0] >=0 and i[1] >=0]
solutionMoves = ["".join([chess_map_from_index_to_alpha[i[1]], str(i[0] + 1)]) for i in temp]
solutionMoves.sort()
return solutionMoves






share|improve this question

















  • 2




    Can you give an example of a call to getBishopMoves()?
    – Daniel
    Jun 24 at 7:43










  • There's some JSON code I left out but the call is essentially moveEngine.py -p "bishop" -l "a1"
    – Kevin Hebert
    Jun 24 at 14:36










  • Sorry, but that's not really helpful, since we don't know how those arguments are parsed. Can you add an actual example of input / output when calling getBishopMoves()?
    – Daniel
    Jun 24 at 16:55
















up vote
4
down vote

favorite












I'm currently in the process of building my own Chess game engine and could really use some suggestions on how to make this segment of code for calculating diagonal moves more efficient. (This is obviously only for diagonals going Up-Right.)



As of now I'm using "Try-Except" to iterate by 1, and then my return statement filters out any off-board values. However this seems like a very bulky way of doing things.



import argparse, json

chessBoard = [[1, 1, 1, 1, 1, 1, 1, 1] for i in range(8)]

chess_map_from_alpha_to_index =
"a" : 0,
"b" : 1,
"c" : 2,
"d" : 3,
"e" : 4,
"f" : 5,
"g" : 6,
"h" : 7


chess_map_from_index_to_alpha =
0: "a",
1: "b",
2: "c",
3: "d",
4: "e",
5: "f",
6: "g",
7: "h"



def getBishopMoves(pos, chessBoard):
column, row = list(pos.strip().lower())
row = int(row) - 1
column = chess_map_from_alpha_to_index[column]
i,j = row, column
solutionMoves =

#Up-Right Diagonal
try:
temp = chessBoard[i + 1][j + 1]
solutionMoves.append([i + 1, j + 1])
except:
pass
try:
temp = chessBoard[i + 2][j + 2]
solutionMoves.append([i + 2, j + 2])
except:
pass
try:
temp = chessBoard[i + 3][j + 3]
solutionMoves.append([i + 3, j + 3])
except:
pass
try:
temp = chessBoard[i + 4][j + 4]
solutionMoves.append([i + 4, j + 4])
except:
pass
try:
temp = chessBoard[i + 5][j + 5]
solutionMoves.append([i + 5, j + 5])
except:
pass
try:
temp = chessBoard[i + 6][j + 6]
solutionMoves.append([i + 6, j + 6])
except:
pass
try:
temp = chessBoard[i + 7][j + 7]
solutionMoves.append([i + 7, j + 7])
except:
pass
try:
temp = chessBoard[i + 7][j + 7]
solutionMoves.append([i + 7, j + 7])
except:
pass

temp = [i for i in solutionMoves if i[0] >=0 and i[1] >=0]
solutionMoves = ["".join([chess_map_from_index_to_alpha[i[1]], str(i[0] + 1)]) for i in temp]
solutionMoves.sort()
return solutionMoves






share|improve this question

















  • 2




    Can you give an example of a call to getBishopMoves()?
    – Daniel
    Jun 24 at 7:43










  • There's some JSON code I left out but the call is essentially moveEngine.py -p "bishop" -l "a1"
    – Kevin Hebert
    Jun 24 at 14:36










  • Sorry, but that's not really helpful, since we don't know how those arguments are parsed. Can you add an actual example of input / output when calling getBishopMoves()?
    – Daniel
    Jun 24 at 16:55












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I'm currently in the process of building my own Chess game engine and could really use some suggestions on how to make this segment of code for calculating diagonal moves more efficient. (This is obviously only for diagonals going Up-Right.)



As of now I'm using "Try-Except" to iterate by 1, and then my return statement filters out any off-board values. However this seems like a very bulky way of doing things.



import argparse, json

chessBoard = [[1, 1, 1, 1, 1, 1, 1, 1] for i in range(8)]

chess_map_from_alpha_to_index =
"a" : 0,
"b" : 1,
"c" : 2,
"d" : 3,
"e" : 4,
"f" : 5,
"g" : 6,
"h" : 7


chess_map_from_index_to_alpha =
0: "a",
1: "b",
2: "c",
3: "d",
4: "e",
5: "f",
6: "g",
7: "h"



def getBishopMoves(pos, chessBoard):
column, row = list(pos.strip().lower())
row = int(row) - 1
column = chess_map_from_alpha_to_index[column]
i,j = row, column
solutionMoves =

#Up-Right Diagonal
try:
temp = chessBoard[i + 1][j + 1]
solutionMoves.append([i + 1, j + 1])
except:
pass
try:
temp = chessBoard[i + 2][j + 2]
solutionMoves.append([i + 2, j + 2])
except:
pass
try:
temp = chessBoard[i + 3][j + 3]
solutionMoves.append([i + 3, j + 3])
except:
pass
try:
temp = chessBoard[i + 4][j + 4]
solutionMoves.append([i + 4, j + 4])
except:
pass
try:
temp = chessBoard[i + 5][j + 5]
solutionMoves.append([i + 5, j + 5])
except:
pass
try:
temp = chessBoard[i + 6][j + 6]
solutionMoves.append([i + 6, j + 6])
except:
pass
try:
temp = chessBoard[i + 7][j + 7]
solutionMoves.append([i + 7, j + 7])
except:
pass
try:
temp = chessBoard[i + 7][j + 7]
solutionMoves.append([i + 7, j + 7])
except:
pass

temp = [i for i in solutionMoves if i[0] >=0 and i[1] >=0]
solutionMoves = ["".join([chess_map_from_index_to_alpha[i[1]], str(i[0] + 1)]) for i in temp]
solutionMoves.sort()
return solutionMoves






share|improve this question













I'm currently in the process of building my own Chess game engine and could really use some suggestions on how to make this segment of code for calculating diagonal moves more efficient. (This is obviously only for diagonals going Up-Right.)



As of now I'm using "Try-Except" to iterate by 1, and then my return statement filters out any off-board values. However this seems like a very bulky way of doing things.



import argparse, json

chessBoard = [[1, 1, 1, 1, 1, 1, 1, 1] for i in range(8)]

chess_map_from_alpha_to_index =
"a" : 0,
"b" : 1,
"c" : 2,
"d" : 3,
"e" : 4,
"f" : 5,
"g" : 6,
"h" : 7


chess_map_from_index_to_alpha =
0: "a",
1: "b",
2: "c",
3: "d",
4: "e",
5: "f",
6: "g",
7: "h"



def getBishopMoves(pos, chessBoard):
column, row = list(pos.strip().lower())
row = int(row) - 1
column = chess_map_from_alpha_to_index[column]
i,j = row, column
solutionMoves =

#Up-Right Diagonal
try:
temp = chessBoard[i + 1][j + 1]
solutionMoves.append([i + 1, j + 1])
except:
pass
try:
temp = chessBoard[i + 2][j + 2]
solutionMoves.append([i + 2, j + 2])
except:
pass
try:
temp = chessBoard[i + 3][j + 3]
solutionMoves.append([i + 3, j + 3])
except:
pass
try:
temp = chessBoard[i + 4][j + 4]
solutionMoves.append([i + 4, j + 4])
except:
pass
try:
temp = chessBoard[i + 5][j + 5]
solutionMoves.append([i + 5, j + 5])
except:
pass
try:
temp = chessBoard[i + 6][j + 6]
solutionMoves.append([i + 6, j + 6])
except:
pass
try:
temp = chessBoard[i + 7][j + 7]
solutionMoves.append([i + 7, j + 7])
except:
pass
try:
temp = chessBoard[i + 7][j + 7]
solutionMoves.append([i + 7, j + 7])
except:
pass

temp = [i for i in solutionMoves if i[0] >=0 and i[1] >=0]
solutionMoves = ["".join([chess_map_from_index_to_alpha[i[1]], str(i[0] + 1)]) for i in temp]
solutionMoves.sort()
return solutionMoves








share|improve this question












share|improve this question




share|improve this question








edited Jun 24 at 7:00









Jamal♦

30.1k11114225




30.1k11114225









asked Jun 24 at 6:45









Kevin Hebert

242




242







  • 2




    Can you give an example of a call to getBishopMoves()?
    – Daniel
    Jun 24 at 7:43










  • There's some JSON code I left out but the call is essentially moveEngine.py -p "bishop" -l "a1"
    – Kevin Hebert
    Jun 24 at 14:36










  • Sorry, but that's not really helpful, since we don't know how those arguments are parsed. Can you add an actual example of input / output when calling getBishopMoves()?
    – Daniel
    Jun 24 at 16:55












  • 2




    Can you give an example of a call to getBishopMoves()?
    – Daniel
    Jun 24 at 7:43










  • There's some JSON code I left out but the call is essentially moveEngine.py -p "bishop" -l "a1"
    – Kevin Hebert
    Jun 24 at 14:36










  • Sorry, but that's not really helpful, since we don't know how those arguments are parsed. Can you add an actual example of input / output when calling getBishopMoves()?
    – Daniel
    Jun 24 at 16:55







2




2




Can you give an example of a call to getBishopMoves()?
– Daniel
Jun 24 at 7:43




Can you give an example of a call to getBishopMoves()?
– Daniel
Jun 24 at 7:43












There's some JSON code I left out but the call is essentially moveEngine.py -p "bishop" -l "a1"
– Kevin Hebert
Jun 24 at 14:36




There's some JSON code I left out but the call is essentially moveEngine.py -p "bishop" -l "a1"
– Kevin Hebert
Jun 24 at 14:36












Sorry, but that's not really helpful, since we don't know how those arguments are parsed. Can you add an actual example of input / output when calling getBishopMoves()?
– Daniel
Jun 24 at 16:55




Sorry, but that's not really helpful, since we don't know how those arguments are parsed. Can you add an actual example of input / output when calling getBishopMoves()?
– Daniel
Jun 24 at 16:55










1 Answer
1






active

oldest

votes

















up vote
4
down vote














Go through the PEP-8 style guide. You have inconsistent naming
convention. A mixed case of camelCase and snake_case throws off
the developer. Follow snake_case for variables and camelCase for
class etc.




The first thing that comes to mind is a loop:



for pos in range(1, 8):
try:
temp = chessBoard[i + pos][j + pos]
solutionMoves.append([i + pos, j + pos])
except:
break


which about covers the whole of your try-except blocks.




However, with chess; I'd suggest using coordinate system to move around the board.



Design a class Point which takes $ (x, y) $ position and define the __add__, __sub__, __neg__ etc. as follows (rough, modify as per your needs):



class Point(tuple):
def __add__(self, other):
return Point(v + w for v, w in zip(self, other))

def __radd__(self, other):
return Point(w + v for v, w in zip(self, other))

def __sub__(self, other):
return Point(v - w for v, w in zip(self, other))

def __neg__(self):
return -1 * self

def __mul__(self, s):
return Vector(v * s for v in self)

def __rmul__(self, s):
return Vector(v * s for v in self)


Now, define your movement direction as a point vector:



DIR_TOP_RIGHT = Point(1, 1)


when you want to move inside the board, just add a multiple of direction to the current point:



current = Point(i, j)
new = current + (distance * DIR_TOP_RIGHT)


Next, define a method (inside a class ChessBoard which checks whether a point lies inside the board or not. This should be quite easy. This ChessBoard class should also be the one responsible for converting your Point object from $ (1, 1) $ format to B1 etc.






share|improve this answer





















  • I really appreciate the detailed response mate, though, I have to say a lot of that went over my head. Are there any articles you could suggest for me to get a better grasp on what you were saying? Mostly confused about what add, sub, neg, are supposed to represent. Also not really sure how the user would choose his "multiple of direction" through a GUI
    – Kevin Hebert
    Jun 24 at 14:38







  • 2




    @Kevin Hebert __add__(), __sub__(), and __neg__() are examples of magic methods. Essentially they allow you to operate on two instances of Point using common operators, like + corresponding to __add__(), - corresponding to __sub__(), etc. See also 'Data model: Special method names: basic customization'
    – Daniel
    Jun 24 at 16:53










  • Thx for the link; diving in now. Do you have any books you recommend? I'm feeling very overwhelmed already as I'm eventually trying to make this an online, multiplayer game...
    – Kevin Hebert
    Jun 24 at 17:14






  • 1




    @KevinHebert I don't know of any books specifically about magic methods, and I'm hesitant to recommend any books because I don't know your skill level. If you have trouble understanding classes, the Python tutorial page on classes may be a useful resource. If you just want an awesome and complete introductory course to Python, check out Learn Python the Hard Way. Apart from that, a search for 'Python classes' will get you pretty far.
    – Daniel
    Jun 26 at 8:43










  • Completely get that. I'll check out the link as my knowledge on classes is definitely a little fuzzy. As for my skill level, I would say it's rather minimal. I've done quite a few tutorials on Python / Django + front end development, along with taking a university class on C++. However, I really struggle in trying to piece together all my knowledge. That's part of the reason why I'm trying to build this chess engine (and eventually online/multiplayer chess game). I understand it's quite an undertaking but I think doing a real project like this is the only way I'll actually learn
    – Kevin Hebert
    Jun 26 at 21:01











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197153%2fdiagonal-movement-code-for-python-chess-engine%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote














Go through the PEP-8 style guide. You have inconsistent naming
convention. A mixed case of camelCase and snake_case throws off
the developer. Follow snake_case for variables and camelCase for
class etc.




The first thing that comes to mind is a loop:



for pos in range(1, 8):
try:
temp = chessBoard[i + pos][j + pos]
solutionMoves.append([i + pos, j + pos])
except:
break


which about covers the whole of your try-except blocks.




However, with chess; I'd suggest using coordinate system to move around the board.



Design a class Point which takes $ (x, y) $ position and define the __add__, __sub__, __neg__ etc. as follows (rough, modify as per your needs):



class Point(tuple):
def __add__(self, other):
return Point(v + w for v, w in zip(self, other))

def __radd__(self, other):
return Point(w + v for v, w in zip(self, other))

def __sub__(self, other):
return Point(v - w for v, w in zip(self, other))

def __neg__(self):
return -1 * self

def __mul__(self, s):
return Vector(v * s for v in self)

def __rmul__(self, s):
return Vector(v * s for v in self)


Now, define your movement direction as a point vector:



DIR_TOP_RIGHT = Point(1, 1)


when you want to move inside the board, just add a multiple of direction to the current point:



current = Point(i, j)
new = current + (distance * DIR_TOP_RIGHT)


Next, define a method (inside a class ChessBoard which checks whether a point lies inside the board or not. This should be quite easy. This ChessBoard class should also be the one responsible for converting your Point object from $ (1, 1) $ format to B1 etc.






share|improve this answer





















  • I really appreciate the detailed response mate, though, I have to say a lot of that went over my head. Are there any articles you could suggest for me to get a better grasp on what you were saying? Mostly confused about what add, sub, neg, are supposed to represent. Also not really sure how the user would choose his "multiple of direction" through a GUI
    – Kevin Hebert
    Jun 24 at 14:38







  • 2




    @Kevin Hebert __add__(), __sub__(), and __neg__() are examples of magic methods. Essentially they allow you to operate on two instances of Point using common operators, like + corresponding to __add__(), - corresponding to __sub__(), etc. See also 'Data model: Special method names: basic customization'
    – Daniel
    Jun 24 at 16:53










  • Thx for the link; diving in now. Do you have any books you recommend? I'm feeling very overwhelmed already as I'm eventually trying to make this an online, multiplayer game...
    – Kevin Hebert
    Jun 24 at 17:14






  • 1




    @KevinHebert I don't know of any books specifically about magic methods, and I'm hesitant to recommend any books because I don't know your skill level. If you have trouble understanding classes, the Python tutorial page on classes may be a useful resource. If you just want an awesome and complete introductory course to Python, check out Learn Python the Hard Way. Apart from that, a search for 'Python classes' will get you pretty far.
    – Daniel
    Jun 26 at 8:43










  • Completely get that. I'll check out the link as my knowledge on classes is definitely a little fuzzy. As for my skill level, I would say it's rather minimal. I've done quite a few tutorials on Python / Django + front end development, along with taking a university class on C++. However, I really struggle in trying to piece together all my knowledge. That's part of the reason why I'm trying to build this chess engine (and eventually online/multiplayer chess game). I understand it's quite an undertaking but I think doing a real project like this is the only way I'll actually learn
    – Kevin Hebert
    Jun 26 at 21:01















up vote
4
down vote














Go through the PEP-8 style guide. You have inconsistent naming
convention. A mixed case of camelCase and snake_case throws off
the developer. Follow snake_case for variables and camelCase for
class etc.




The first thing that comes to mind is a loop:



for pos in range(1, 8):
try:
temp = chessBoard[i + pos][j + pos]
solutionMoves.append([i + pos, j + pos])
except:
break


which about covers the whole of your try-except blocks.




However, with chess; I'd suggest using coordinate system to move around the board.



Design a class Point which takes $ (x, y) $ position and define the __add__, __sub__, __neg__ etc. as follows (rough, modify as per your needs):



class Point(tuple):
def __add__(self, other):
return Point(v + w for v, w in zip(self, other))

def __radd__(self, other):
return Point(w + v for v, w in zip(self, other))

def __sub__(self, other):
return Point(v - w for v, w in zip(self, other))

def __neg__(self):
return -1 * self

def __mul__(self, s):
return Vector(v * s for v in self)

def __rmul__(self, s):
return Vector(v * s for v in self)


Now, define your movement direction as a point vector:



DIR_TOP_RIGHT = Point(1, 1)


when you want to move inside the board, just add a multiple of direction to the current point:



current = Point(i, j)
new = current + (distance * DIR_TOP_RIGHT)


Next, define a method (inside a class ChessBoard which checks whether a point lies inside the board or not. This should be quite easy. This ChessBoard class should also be the one responsible for converting your Point object from $ (1, 1) $ format to B1 etc.






share|improve this answer





















  • I really appreciate the detailed response mate, though, I have to say a lot of that went over my head. Are there any articles you could suggest for me to get a better grasp on what you were saying? Mostly confused about what add, sub, neg, are supposed to represent. Also not really sure how the user would choose his "multiple of direction" through a GUI
    – Kevin Hebert
    Jun 24 at 14:38







  • 2




    @Kevin Hebert __add__(), __sub__(), and __neg__() are examples of magic methods. Essentially they allow you to operate on two instances of Point using common operators, like + corresponding to __add__(), - corresponding to __sub__(), etc. See also 'Data model: Special method names: basic customization'
    – Daniel
    Jun 24 at 16:53










  • Thx for the link; diving in now. Do you have any books you recommend? I'm feeling very overwhelmed already as I'm eventually trying to make this an online, multiplayer game...
    – Kevin Hebert
    Jun 24 at 17:14






  • 1




    @KevinHebert I don't know of any books specifically about magic methods, and I'm hesitant to recommend any books because I don't know your skill level. If you have trouble understanding classes, the Python tutorial page on classes may be a useful resource. If you just want an awesome and complete introductory course to Python, check out Learn Python the Hard Way. Apart from that, a search for 'Python classes' will get you pretty far.
    – Daniel
    Jun 26 at 8:43










  • Completely get that. I'll check out the link as my knowledge on classes is definitely a little fuzzy. As for my skill level, I would say it's rather minimal. I've done quite a few tutorials on Python / Django + front end development, along with taking a university class on C++. However, I really struggle in trying to piece together all my knowledge. That's part of the reason why I'm trying to build this chess engine (and eventually online/multiplayer chess game). I understand it's quite an undertaking but I think doing a real project like this is the only way I'll actually learn
    – Kevin Hebert
    Jun 26 at 21:01













up vote
4
down vote










up vote
4
down vote










Go through the PEP-8 style guide. You have inconsistent naming
convention. A mixed case of camelCase and snake_case throws off
the developer. Follow snake_case for variables and camelCase for
class etc.




The first thing that comes to mind is a loop:



for pos in range(1, 8):
try:
temp = chessBoard[i + pos][j + pos]
solutionMoves.append([i + pos, j + pos])
except:
break


which about covers the whole of your try-except blocks.




However, with chess; I'd suggest using coordinate system to move around the board.



Design a class Point which takes $ (x, y) $ position and define the __add__, __sub__, __neg__ etc. as follows (rough, modify as per your needs):



class Point(tuple):
def __add__(self, other):
return Point(v + w for v, w in zip(self, other))

def __radd__(self, other):
return Point(w + v for v, w in zip(self, other))

def __sub__(self, other):
return Point(v - w for v, w in zip(self, other))

def __neg__(self):
return -1 * self

def __mul__(self, s):
return Vector(v * s for v in self)

def __rmul__(self, s):
return Vector(v * s for v in self)


Now, define your movement direction as a point vector:



DIR_TOP_RIGHT = Point(1, 1)


when you want to move inside the board, just add a multiple of direction to the current point:



current = Point(i, j)
new = current + (distance * DIR_TOP_RIGHT)


Next, define a method (inside a class ChessBoard which checks whether a point lies inside the board or not. This should be quite easy. This ChessBoard class should also be the one responsible for converting your Point object from $ (1, 1) $ format to B1 etc.






share|improve this answer














Go through the PEP-8 style guide. You have inconsistent naming
convention. A mixed case of camelCase and snake_case throws off
the developer. Follow snake_case for variables and camelCase for
class etc.




The first thing that comes to mind is a loop:



for pos in range(1, 8):
try:
temp = chessBoard[i + pos][j + pos]
solutionMoves.append([i + pos, j + pos])
except:
break


which about covers the whole of your try-except blocks.




However, with chess; I'd suggest using coordinate system to move around the board.



Design a class Point which takes $ (x, y) $ position and define the __add__, __sub__, __neg__ etc. as follows (rough, modify as per your needs):



class Point(tuple):
def __add__(self, other):
return Point(v + w for v, w in zip(self, other))

def __radd__(self, other):
return Point(w + v for v, w in zip(self, other))

def __sub__(self, other):
return Point(v - w for v, w in zip(self, other))

def __neg__(self):
return -1 * self

def __mul__(self, s):
return Vector(v * s for v in self)

def __rmul__(self, s):
return Vector(v * s for v in self)


Now, define your movement direction as a point vector:



DIR_TOP_RIGHT = Point(1, 1)


when you want to move inside the board, just add a multiple of direction to the current point:



current = Point(i, j)
new = current + (distance * DIR_TOP_RIGHT)


Next, define a method (inside a class ChessBoard which checks whether a point lies inside the board or not. This should be quite easy. This ChessBoard class should also be the one responsible for converting your Point object from $ (1, 1) $ format to B1 etc.







share|improve this answer













share|improve this answer



share|improve this answer











answered Jun 24 at 10:30









hjpotter92

4,89611538




4,89611538











  • I really appreciate the detailed response mate, though, I have to say a lot of that went over my head. Are there any articles you could suggest for me to get a better grasp on what you were saying? Mostly confused about what add, sub, neg, are supposed to represent. Also not really sure how the user would choose his "multiple of direction" through a GUI
    – Kevin Hebert
    Jun 24 at 14:38







  • 2




    @Kevin Hebert __add__(), __sub__(), and __neg__() are examples of magic methods. Essentially they allow you to operate on two instances of Point using common operators, like + corresponding to __add__(), - corresponding to __sub__(), etc. See also 'Data model: Special method names: basic customization'
    – Daniel
    Jun 24 at 16:53










  • Thx for the link; diving in now. Do you have any books you recommend? I'm feeling very overwhelmed already as I'm eventually trying to make this an online, multiplayer game...
    – Kevin Hebert
    Jun 24 at 17:14






  • 1




    @KevinHebert I don't know of any books specifically about magic methods, and I'm hesitant to recommend any books because I don't know your skill level. If you have trouble understanding classes, the Python tutorial page on classes may be a useful resource. If you just want an awesome and complete introductory course to Python, check out Learn Python the Hard Way. Apart from that, a search for 'Python classes' will get you pretty far.
    – Daniel
    Jun 26 at 8:43










  • Completely get that. I'll check out the link as my knowledge on classes is definitely a little fuzzy. As for my skill level, I would say it's rather minimal. I've done quite a few tutorials on Python / Django + front end development, along with taking a university class on C++. However, I really struggle in trying to piece together all my knowledge. That's part of the reason why I'm trying to build this chess engine (and eventually online/multiplayer chess game). I understand it's quite an undertaking but I think doing a real project like this is the only way I'll actually learn
    – Kevin Hebert
    Jun 26 at 21:01

















  • I really appreciate the detailed response mate, though, I have to say a lot of that went over my head. Are there any articles you could suggest for me to get a better grasp on what you were saying? Mostly confused about what add, sub, neg, are supposed to represent. Also not really sure how the user would choose his "multiple of direction" through a GUI
    – Kevin Hebert
    Jun 24 at 14:38







  • 2




    @Kevin Hebert __add__(), __sub__(), and __neg__() are examples of magic methods. Essentially they allow you to operate on two instances of Point using common operators, like + corresponding to __add__(), - corresponding to __sub__(), etc. See also 'Data model: Special method names: basic customization'
    – Daniel
    Jun 24 at 16:53










  • Thx for the link; diving in now. Do you have any books you recommend? I'm feeling very overwhelmed already as I'm eventually trying to make this an online, multiplayer game...
    – Kevin Hebert
    Jun 24 at 17:14






  • 1




    @KevinHebert I don't know of any books specifically about magic methods, and I'm hesitant to recommend any books because I don't know your skill level. If you have trouble understanding classes, the Python tutorial page on classes may be a useful resource. If you just want an awesome and complete introductory course to Python, check out Learn Python the Hard Way. Apart from that, a search for 'Python classes' will get you pretty far.
    – Daniel
    Jun 26 at 8:43










  • Completely get that. I'll check out the link as my knowledge on classes is definitely a little fuzzy. As for my skill level, I would say it's rather minimal. I've done quite a few tutorials on Python / Django + front end development, along with taking a university class on C++. However, I really struggle in trying to piece together all my knowledge. That's part of the reason why I'm trying to build this chess engine (and eventually online/multiplayer chess game). I understand it's quite an undertaking but I think doing a real project like this is the only way I'll actually learn
    – Kevin Hebert
    Jun 26 at 21:01
















I really appreciate the detailed response mate, though, I have to say a lot of that went over my head. Are there any articles you could suggest for me to get a better grasp on what you were saying? Mostly confused about what add, sub, neg, are supposed to represent. Also not really sure how the user would choose his "multiple of direction" through a GUI
– Kevin Hebert
Jun 24 at 14:38





I really appreciate the detailed response mate, though, I have to say a lot of that went over my head. Are there any articles you could suggest for me to get a better grasp on what you were saying? Mostly confused about what add, sub, neg, are supposed to represent. Also not really sure how the user would choose his "multiple of direction" through a GUI
– Kevin Hebert
Jun 24 at 14:38





2




2




@Kevin Hebert __add__(), __sub__(), and __neg__() are examples of magic methods. Essentially they allow you to operate on two instances of Point using common operators, like + corresponding to __add__(), - corresponding to __sub__(), etc. See also 'Data model: Special method names: basic customization'
– Daniel
Jun 24 at 16:53




@Kevin Hebert __add__(), __sub__(), and __neg__() are examples of magic methods. Essentially they allow you to operate on two instances of Point using common operators, like + corresponding to __add__(), - corresponding to __sub__(), etc. See also 'Data model: Special method names: basic customization'
– Daniel
Jun 24 at 16:53












Thx for the link; diving in now. Do you have any books you recommend? I'm feeling very overwhelmed already as I'm eventually trying to make this an online, multiplayer game...
– Kevin Hebert
Jun 24 at 17:14




Thx for the link; diving in now. Do you have any books you recommend? I'm feeling very overwhelmed already as I'm eventually trying to make this an online, multiplayer game...
– Kevin Hebert
Jun 24 at 17:14




1




1




@KevinHebert I don't know of any books specifically about magic methods, and I'm hesitant to recommend any books because I don't know your skill level. If you have trouble understanding classes, the Python tutorial page on classes may be a useful resource. If you just want an awesome and complete introductory course to Python, check out Learn Python the Hard Way. Apart from that, a search for 'Python classes' will get you pretty far.
– Daniel
Jun 26 at 8:43




@KevinHebert I don't know of any books specifically about magic methods, and I'm hesitant to recommend any books because I don't know your skill level. If you have trouble understanding classes, the Python tutorial page on classes may be a useful resource. If you just want an awesome and complete introductory course to Python, check out Learn Python the Hard Way. Apart from that, a search for 'Python classes' will get you pretty far.
– Daniel
Jun 26 at 8:43












Completely get that. I'll check out the link as my knowledge on classes is definitely a little fuzzy. As for my skill level, I would say it's rather minimal. I've done quite a few tutorials on Python / Django + front end development, along with taking a university class on C++. However, I really struggle in trying to piece together all my knowledge. That's part of the reason why I'm trying to build this chess engine (and eventually online/multiplayer chess game). I understand it's quite an undertaking but I think doing a real project like this is the only way I'll actually learn
– Kevin Hebert
Jun 26 at 21:01





Completely get that. I'll check out the link as my knowledge on classes is definitely a little fuzzy. As for my skill level, I would say it's rather minimal. I've done quite a few tutorials on Python / Django + front end development, along with taking a university class on C++. However, I really struggle in trying to piece together all my knowledge. That's part of the reason why I'm trying to build this chess engine (and eventually online/multiplayer chess game). I understand it's quite an undertaking but I think doing a real project like this is the only way I'll actually learn
– Kevin Hebert
Jun 26 at 21:01













 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197153%2fdiagonal-movement-code-for-python-chess-engine%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation