How to Optimize Diagonal Movement Code for Chess Engine?
NickName:Kevin Hebert Ask DateTime:2018-06-24T08:43:45

How to Optimize Diagonal Movement Code for Chess Engine?

I'm currently in the process of building my own chess 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.

Any comments or advice on how to refactor this code would be greatly appreciated.

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

Copyright Notice:Content Author:「Kevin Hebert」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/51006126/how-to-optimize-diagonal-movement-code-for-chess-engine

More about “How to Optimize Diagonal Movement Code for Chess Engine?” related questions

How to Optimize Diagonal Movement Code for Chess Engine?

I'm currently in the process of building my own chess engine and could really use some suggestions on how to make this segment of code for calculating diagonal moves more efficient. (This is obviou...

Show Detail

How to move an element in Diagonal Movement in jQuery?

I know how to move up and down an element in jQuery. $("#div").animate({"left": "+=100"}, 1000); //move 100px to the right But I have no idea to move in diagonal movement. I'm doing chess board a..

Show Detail

Eight Queens - Diagonal Movement

I have an 8 by 8 chess board that I am trying to implement the eight queen puzzle. In my game I have made a function that checks for the movement of queens and once a button gets pressed in my boar...

Show Detail

Lock to diagonal movement

How can I lock diagonal movement with FabricJS? I know it can be locked vertical and horizontal, but I can't find an easy way to lock diagonal. What I need is the ability to move one canvas group ...

Show Detail

Queen movement in chess game(Javascript)

I am learning Javascript and as a project assignment making chess game in it. I have coded the logic for movement of Rook, Pawn, Knight and Bishop. Now I am stuck on Queen movement. A queen's move

Show Detail

Difference between Chess Engine and Chess Architecture

I'm reading a few technical papers on Chess Engine Development. I've come across terms viz. Computer-Chess Engine and Computer-Chess Architecture frequently. Chess Engine is basically a computer

Show Detail

How to track movement of a single chess piece?

I'm using python-chess and I would like to know what is a good way to track movement of let say a black King throughout a single match recorded in pgn format using python-chess. Essentially creatin...

Show Detail

What is the logic behind the equation of the diagonal of chess?

Hi everyone I am trying to solve the problem of N queen with the backtracking algorithm for getting solutions but the problem here is understanding the equation of the diagonal and how we get it...

Show Detail

Movement in unity; Non diagonal, zelda like

So I'm testing around with Unity and Visual Studio, and as a starter project I'm working to replicate Zelda (NES) and I'm starting with movement. So far, with much jank, I've figured out how to pre...

Show Detail

startDrag - stop diagonal movement

Is there a way to have a MovieClip with startDrag, but to force only horizontal and vertical (i.e. not diagonal) movement?

Show Detail