home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / gnome-games-data / glchess / chess / bitboard.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  6.7 KB  |  194 lines

  1. # -*- coding: utf-8 -*-
  2.  
  3. #
  4. #
  5. #      +---+---+---+---+---+---+---+---+
  6. #    8 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
  7. #      +---+---+---+---+---+---+---+---+
  8. #    7 | . |   | . |   | . |   | . |   |
  9. #      +---+---+---+---+---+---+---+---+
  10. #    6 |   | . |   | . |   | . |   | . |
  11. # r    +---+---+---+---+---+---+---+---+
  12. # a  5 | . |   | . |   | . |   | . |   |
  13. # n    +---+---+---+---+---+---+---+---+
  14. # k  4 |   | . |   | . |   | . |   | . |
  15. # s    +---+---+---+---+---+---+---+---+
  16. #    3 | . |   | . |   | . |   | . |   |
  17. #      +---+---+---+---+---+---+---+---+
  18. #    2 |   | . |   | . |   | . |   | . |
  19. #      +---+---+---+---+---+---+---+---+
  20. #    1 |56 |57 |58 |59 |60 |61 |62 |63 |
  21. #      +---+---+---+---+---+---+---+---+
  22. #
  23. #        a   b   c   d   e   f   g   h
  24. #                    files
  25.  
  26. def getIndex(square):
  27.     file = ord(square[0]) - ord('a')
  28.     rank = ord(square[1]) - ord('1')
  29.     return (7 - rank) * 8 + file
  30.  
  31. def getRankAndFile(index):
  32.     file = index % 8
  33.     rank = 7 - (index / 8)
  34.     return (file, rank)
  35.  
  36. def reflectField(field):
  37.     outField = 0
  38.     for i in xrange(64):
  39.         if field & LOCATIONS[i]:
  40.             (file, rank) = getRankAndFile(i)
  41.             outField |= LOCATIONS[rank * 8 + file]
  42.     return outField
  43.  
  44. def reflectMoves(inMoves, outMoves):
  45.     for i in xrange(64):
  46.         (file, rank) = getRankAndFile(i)
  47.         outMoves[rank * 8 + file] = reflectField(inMoves[i])
  48.  
  49. def printField(field):
  50.     string = '+---+---+---+---+---+---+---+---+\n'
  51.     rowCount = 0
  52.     colour = ' '
  53.     for l in LOCATIONS:
  54.         if field & l:
  55.             string += '|[%s]' % colour
  56.         else:
  57.             string += '| %s ' % colour
  58.         rowCount += 1
  59.         if rowCount == 8:
  60.             rowCount = 0
  61.             string += '|\n+---+---+---+---+---+---+---+---+\n'
  62.         else:
  63.             if colour == ' ':
  64.                 colour = '.'
  65.             else:
  66.                 colour = ' '
  67.     print string
  68.  
  69. LOCATIONS = [0x0000000000000001, 0x0000000000000002, 0x0000000000000004, 0x0000000000000008,
  70.              0x0000000000000010, 0x0000000000000020, 0x0000000000000040, 0x0000000000000080,
  71.              0x0000000000000100, 0x0000000000000200, 0x0000000000000400, 0x0000000000000800,
  72.              0x0000000000001000, 0x0000000000002000, 0x0000000000004000, 0x0000000000008000,
  73.              0x0000000000010000, 0x0000000000020000, 0x0000000000040000, 0x0000000000080000,
  74.              0x0000000000100000, 0x0000000000200000, 0x0000000000400000, 0x0000000000800000,
  75.              0x0000000001000000, 0x0000000002000000, 0x0000000004000000, 0x0000000008000000,
  76.              0x0000000010000000, 0x0000000020000000, 0x0000000040000000, 0x0000000080000000,
  77.              0x0000000100000000, 0x0000000200000000, 0x0000000400000000, 0x0000000800000000,
  78.              0x0000001000000000, 0x0000002000000000, 0x0000004000000000, 0x0000008000000000,
  79.              0x0000010000000000, 0x0000020000000000, 0x0000040000000000, 0x0000080000000000,
  80.              0x0000100000000000, 0x0000200000000000, 0x0000400000000000, 0x0000800000000000,
  81.              0x0001000000000000, 0x0002000000000000, 0x0004000000000000, 0x0008000000000000,
  82.              0x0010000000000000, 0x0020000000000000, 0x0040000000000000, 0x0080000000000000,
  83.              0x0100000000000000, 0x0200000000000000, 0x0400000000000000, 0x0800000000000000,
  84.              0x1000000000000000, 0x2000000000000000, 0x4000000000000000, 0x8000000000000000]
  85.              
  86. KNIGHT_MOVES = [0xFFFFFFFFFFFFFFFF] * 64
  87. WHITE_PAWN_MOVES = KNIGHT_MOVES[:]
  88. WHITE_PAWN_TAKES = KNIGHT_MOVES[:]
  89. BLACK_PAWN_MOVES = KNIGHT_MOVES[:]
  90. BLACK_PAWN_TAKES = KNIGHT_MOVES[:]
  91. ROOK_MOVES = KNIGHT_MOVES[:]
  92. BISHOP_MOVES = KNIGHT_MOVES[:]
  93. KNIGHT_MOVES = KNIGHT_MOVES[:]
  94. QUEEN_MOVES = KNIGHT_MOVES[:]
  95. WHITE_KING_MOVES = KNIGHT_MOVES[:]
  96. BLACK_KING_MOVES = KNIGHT_MOVES[:]
  97.  
  98. INBETWEEN_SQUARES = [None] * 64
  99. for i in xrange(64):
  100.     INBETWEEN_SQUARES[i] = [0L] * 64
  101.  
  102. def genMoves():    
  103.     for start in xrange(64):
  104.         (f0, r0) = getRankAndFile(start)
  105.         pawnField = 0
  106.         pawnTakeField = 0
  107.         rookField = 0
  108.         bishopField = 0
  109.         knightField = 0
  110.         queenField = 0
  111.         kingField = 0
  112.  
  113.         for end in xrange(64):
  114.             # Ignore null move
  115.             if end == start:
  116.                 continue
  117.  
  118.             (f1, r1) = getRankAndFile(end)
  119.             endField = LOCATIONS[end]
  120.             
  121.             ibField = 0
  122.             # Vertical, horizontal or diagonal
  123.             if f0 == f1 or r0 == r1 or abs(f0 - f1) == abs(r0 - r1):
  124.                 fstep = cmp(f1, f0)
  125.                 rstep = cmp(r1, r0)
  126.                 file = f0 + fstep
  127.                 rank = r0 + rstep
  128.                 count = max(abs(f0 - f1), abs(r0 - r1)) - 1
  129.                 for i in xrange(count):
  130.                     ibField |= LOCATIONS[(7 - rank) * 8 + file]
  131.                     file += fstep
  132.                     rank += rstep
  133.                 
  134.             INBETWEEN_SQUARES[start][end] = ibField
  135.  
  136.             # Pawn moves
  137.             if f0 == f1:
  138.                 if r1 - r0 == 1:
  139.                     pawnField |= endField
  140.                 # March
  141.                 if r0 == 1 and r1 == 3:
  142.                     pawnField |= endField
  143.                     
  144.             # Pawn takes
  145.             if abs(f0 - f1) == 1 and r1 - r0 == 1:
  146.                 pawnField |= endField
  147.                 pawnTakeField |= endField
  148.             
  149.             # Knight moves
  150.             if abs(f0 - f1) * abs(r0 - r1) == 2:
  151.                 knightField |= endField
  152.                 
  153.             # Do diagonals
  154.             if abs(f0 - f1) == abs(r0 - r1):
  155.                 bishopField |= endField
  156.                 queenField |= endField
  157.         
  158.             # Do orthogonals
  159.             if abs(f0 - f1) * abs(r0 - r1) == 0:
  160.                 rookField |= endField
  161.                 queenField |= endField
  162.  
  163.             # Standard king
  164.             if abs(f0 - f1) <= 1 and abs(r0 - r1) <= 1:
  165.                 kingField |= endField
  166.                 
  167.             # Castling
  168.             if r0 == r1 and r0 == 0 and f0 == 4 and abs(f0 - f1) == 2:
  169.                 kingField |= endField
  170.  
  171.         WHITE_PAWN_MOVES[start] = pawnField
  172.         WHITE_PAWN_TAKES[start] = pawnTakeField
  173.         ROOK_MOVES[start] = rookField
  174.         BISHOP_MOVES[start] = bishopField
  175.         KNIGHT_MOVES[start] = knightField
  176.         QUEEN_MOVES[start] = queenField
  177.         WHITE_KING_MOVES[start] = kingField
  178.  
  179.     reflectMoves(WHITE_PAWN_MOVES, BLACK_PAWN_MOVES)
  180.     reflectMoves(WHITE_PAWN_TAKES, BLACK_PAWN_TAKES)
  181.     reflectMoves(WHITE_KING_MOVES, BLACK_KING_MOVES)
  182.  
  183.     move = 60
  184.     #printField(WHITE_PAWN_MOVES[move])
  185.     #printField(WHITE_PAWN_TAKES[move])
  186.     #printField(BLACK_PAWN_MOVES[move])
  187.     #printField(BLACK_PAWN_TAKES[move])
  188.     #printField(ROOK_MOVES[move])
  189.     #printField(BISHOP_MOVES[move])
  190.     #printField(KNIGHT_MOVES[move])
  191.     #printField(QUEEN_MOVES[move])
  192.     #printField(WHITE_KING_MOVES[move])
  193.  
  194. genMoves()