1

is anyone able to decipher this code and help explain how the code allows the knight to move across the board it would be appreciated. i don't really have any idea as to why these specific numbers so if you could also explain that, it would be appreciated.

# Notes - variables:
# chosen is the current position of the piece
# moves is a list of where the chosen piece can move
# taken_pieces is a list of where the chosen piece can capture
# board is a list of every space on the board and says what piece exists there
# turn is a number either 1 or 2 representing whose turn it is

turn = 1
wcastle = 0
bcastle = 0
chosen = None
moves = []
taken_pieces = []
wtake_piece = []
btake_piece = []
board = [None for i in range(64)]

def attacked_spaces(player,board):

attacked = []

    # Knight for both players
    if board[i][0] == 'N' and board[i][1] == str(player):
        x,y = i%8,i//8

        if x >= 2 and y <= 6:
            attacked.append((x-2)+(y+1)*8)
        if x >= 1 and y <= 5:
            attacked.append((x-1)+(y+2)*8)

        if x <= 6 and y <= 5:
            attacked.append((x+1)+(y+2)*8)
        if x <= 5 and y <= 6:
            attacked.append((x+2)+(y+1)*8)

        if x <= 5 and y >= 1:
            attacked.append((x+2)+(y-1)*8)
        if x <= 6 and y >= 2:
            attacked.append((x+1)+(y-2)*8)

        if x >= 1 and y >= 2:
            attacked.append((x-1)+(y-2)*8)
        if x >= 2 and y >= 1:
            attacked.append((x-2)+(y-1)*8)

    if board[i][0] == 'K' and board[i][1] == str(player):
        x,y = i%8,i//8

        if x >= 1 and y <= 6:
            attacked.append((x-1)+(y+1)*8)
        if y <= 6:
            attacked.append(x+(y+1)*8)
        if x <= 6 and y <= 6:
            attacked.append((x+1)+(y+1)*8)
        if x <= 6:
                attacked.append((x+1)+y*8)
        if x <= 6 and y >= 1:
            attacked.append((x+1)+(y-1)*8)
        if y >= 1:
            attacked.append(x+(y-1)*8)
        if x >= 1 and y >= 1:
            attacked.append((x-1)+(y-1)*8)
        if x >= 1:
            attacked.append((x-1)+y*8)

return attacked
sloth
  • 99,095
  • 21
  • 171
  • 219
Inderbir
  • 19
  • 5
  • [This](http://stackoverflow.com/a/2151141/3005167) is not Python, but it explains what happens, here. – MB-F Feb 10 '16 at 16:20

1 Answers1

0

It's a standard way of finding all squares that a knight is attacking on a board represented internally one-dimensionally. There are 8 separate cases. Here is some perl I wrote recently that does basically the same thing:

for ($i=0,$k=0; $i<$s; ++$i) {
  for ($j=0; $j<$s; ++$j,++$k) {
    my @moves;
    if ($i>1) {
      if ($j>0) { push @moves, $k-$s-$s-1 }
      if ($j<$s-1) { push @moves, $k-$s-$s+1 }
      }
    if ($j>1) {
      if ($i>0) { push @moves, $k-$s-2 }
      if ($i<$s-1) { push @moves, $k+$s-2 }
      }
    if ($i<$s-2) {
      if ($j>0) { push @moves, $k+$s+$s-1 }
      if ($j<$s-1) { push @moves, $k+$s+$s+1 }
      }
    if ($j<$s-2) {
      if ($i>0) { push @moves, $k-$s+2 }
      if ($i<$s-1) { push @moves, $k+$s+2 }
      }
Jeff Y
  • 2,437
  • 1
  • 11
  • 18