1

I am trying to get all possible locations a Knight can be placed based on its current location.

The following code is in python. There is a simple UI where user enters current location and the possible locations are then displayed.

I have done a lot of if & else. Can someone show a shorter way?

def getPostions():
    ch = p.get()[0]
    n = int(p.get()[1])
    posList = []

    posList = [ chr(ord(ch)-1)+str(int(n)-2), chr(ord(ch)-1)+str(int(n)+2),
                chr(ord(ch)+1)+str(int(n)-2), chr(ord(ch)+1)+str(int(n)+2),
                chr(ord(ch)-2)+str(int(n)-1), chr(ord(ch)-2)+str(int(n)+1),
                chr(ord(ch)+2)+str(int(n)-1), chr(ord(ch)+2)+str(int(n)+1)]

    if (ch == "a"):

        # del 0,1,4,5
        posList = [posList[2], posList[3], posList[6], posList[7]]

        if (n == 8):

            # del 3,7
            posList = [posList[0],posList[2]]

        elif (n == 7):

            # del 3
            posList = [posList[0],posList[2],posList[3]]

        elif (n == 1):

            # del 2,6
            posList = [posList[1],posList[3]]

        elif (n == 2):

            # del 2
            posList = [posList[1],posList[2],posList[3]]

    elif (ch == "b"):

        # del 4,5
        posList = [posList[0],posList[1],posList[2],posList[3],posList[6],posList[7]]

        if (n == 8):

            # del 1,3,7
            posList = [posList[0],posList[2],posList[4]]

        elif (n == 7):

            # del 1,3
            posList = [posList[0],posList[2],posList[4],posList[5]]

        elif (n == 1):

            # del 0,2,6
            posList = [posList[1],posList[3],posList[5]]

        elif (n == 2):

            # del 0,2
            posList = [posList[1],posList[3],posList[4],posList[5]]

    elif (ch == "h"):

        # del 2,3,6,7
        posList = [posList[0],posList[1],posList[4],posList[5]]

        if (n == 8):

            # del 1,5
            posList = [posList[0],posList[2]]

        elif (n == 7):

            # del 1
            posList = [posList[0],posList[2],posList[3]]

        elif (n == 1):

            # del 0,4
            posList = [posList[1],posList[3]]

        elif (n == 2):

            # del 0
            posList = [posList[1],posList[2],posList[3]]

    elif (ch == "g"):

        # del 6,7
        posList = [posList[0],posList[1],posList[2],posList[3],posList[4],posList[5]]

        if (n == 8):

            # del 1,3,5
            posList = [posList[0],posList[2],posList[4]]

        elif (n == 7):

            # del 1,3
            posList = [posList[0],posList[2],posList[4],posList[5]]

        elif (n == 1):

            # del 0,2,4
            posList = [posList[1],posList[3],posList[5]]

        elif (n == 2):

            # del 0,2
            posList = [posList[1],posList[3],posList[4],posList[5]]


    result['text'] = "Possible Locations for Knight are: " + ",".join(posList)
    result.pack(pady=30)
Blckknght
  • 100,903
  • 11
  • 120
  • 169
Avishek Dutta
  • 97
  • 2
  • 11
  • Unfortunately Python doesn't have switch statements, but you could try using a dictionary instead to simplify the code. See http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python – Charles Nov 29 '13 at 17:57
  • 1
    This question appears to be off-topic because it is about improving code that already works. It would be better received on http://codereview.stackexchange.com. – jwodder Nov 29 '13 at 18:48
  • check this 3 lines only http://stackoverflow.com/a/21012993/973699 – DevC Jan 09 '14 at 06:25

1 Answers1

1

After creating your list

posList = [ chr(ord(ch)-1)+str(int(n)-2), chr(ord(ch)-1)+str(int(n)+2),
                chr(ord(ch)+1)+str(int(n)-2), chr(ord(ch)+1)+str(int(n)+2),
                chr(ord(ch)-2)+str(int(n)-1), chr(ord(ch)-2)+str(int(n)+1),
                chr(ord(ch)+2)+str(int(n)-1), chr(ord(ch)+2)+str(int(n)+1)]

just filter it:

posList = [pos for pos in posList if pos[0] in 'abcdefgh' and pos[1] in '12345678']
Blckknght
  • 100,903
  • 11
  • 120
  • 169
Hyperboreus
  • 31,997
  • 9
  • 47
  • 87