1# Chess960 (Fischer Random Chess)
2
3import random
4
5from pychess.Utils.const import FISCHERRANDOMCHESS, VARIANTS_SHUFFLE
6from pychess.Utils.Board import Board
7from pychess.Utils.const import reprFile
8
9
10class FischerandomBoard(Board):
11    variant = FISCHERRANDOMCHESS
12    __desc__ = _(
13        "http://en.wikipedia.org/wiki/Chess960\n" +
14        "FICS wild/fr: http://www.freechess.org/Help/HelpFiles/wild.html")
15    name = _("Fischer Random")
16    cecp_name = "fischerandom"
17    need_initial_board = True
18    standard_rules = False
19    variant_group = VARIANTS_SHUFFLE
20
21    def __init__(self, setup=False, lboard=None):
22        if setup is True:
23            Board.__init__(self, setup=self.shuffle_start(), lboard=lboard)
24        else:
25            Board.__init__(self, setup=setup, lboard=lboard)
26
27    def shuffle_start(self):
28        """ Create a random initial position.
29            The king is placed somewhere between the two rooks.
30            The bishops are placed on opposite-colored squares."""
31
32        positions = [1, 2, 3, 4, 5, 6, 7, 8]
33        board = [''] * 8
34        castl = ''
35
36        bishop = random.choice((1, 3, 5, 7))
37        board[bishop - 1] = 'b'
38        positions.remove(bishop)
39
40        bishop = random.choice((2, 4, 6, 8))
41        board[bishop - 1] = 'b'
42        positions.remove(bishop)
43
44        queen = random.choice(positions)
45        board[queen - 1] = 'q'
46        positions.remove(queen)
47
48        knight = random.choice(positions)
49        board[knight - 1] = 'n'
50        positions.remove(knight)
51
52        knight = random.choice(positions)
53        board[knight - 1] = 'n'
54        positions.remove(knight)
55
56        rook = positions[0]
57        board[rook - 1] = 'r'
58        castl += reprFile[rook - 1]
59
60        king = positions[1]
61        board[king - 1] = 'k'
62
63        rook = positions[2]
64        board[rook - 1] = 'r'
65        castl += reprFile[rook - 1]
66
67        fen = ''.join(board)
68        fen = fen + '/pppppppp/8/8/8/8/PPPPPPPP/' + fen.upper(
69        ) + ' w ' + castl.upper() + castl + ' - 0 1'
70        return fen
71
72
73if __name__ == '__main__':
74    frcBoard = FischerandomBoard(True)
75    for i in range(10):
76        print(frcBoard.shuffle_start())
77