File size: 4,555 Bytes
5fd8e56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import chess
import streamlit as st

class Chess:
    def __init__(self, width, fen):
        self.width = width
        self.fen = fen

    def __header__(self):
        return """
        <link rel="stylesheet"
            href="https://unpkg.com/@chrisoakman/[email protected]/dist/chessboard-1.0.0.min.css"
            integrity="sha384-q94+BZtLrkL1/ohfjR8c6L+A6qzNH9R2hBLwyoAfu3i/WCvQjzL2RQJ3uNHDISdU"
            crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
        <script src="https://unpkg.com/@chrisoakman/[email protected]/dist/chessboard-1.0.0.min.js"
            integrity="sha384-8Vi8VHwn3vjQ9eUHUxex3JSN/NFqUg3QbPyX8kWyb93+8AC/pPWTzj+nHtbC5bxD"
            crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.2/chess.js"
            integrity="sha384-s3XgLpvmHyscVpijnseAmye819Ee3yaGa8NxstkJVyA6nuDFjt59u1QvuEl/mecz"
            crossorigin="anonymous"></script>
        """

    def __sidetomove__(self):
        return 'white' if chess.Board(self.fen).turn else 'black'

    def __board_placeholder__(self):
        return f"""
        <div id="myBoard" style="width: {self.width}px"></div><br>
        <label><strong>Status:</strong></label>
        <div id="status"></div>
        """

    def puzzle_board(self):
        sidetomove = self.__sidetomove__()

        script1 = f"""
        // NOTE: this example uses the chess.js library:
        // https://github.com/jhlywa/chess.js

        var board = null
        var game = new Chess('{self.fen}')
        var $status = $('#status')
        var $fen = $('#fen')
        var $pgn = $('#pgn')
        """

        game_over_ = """
        // do not pick up pieces if the game is over
        if (game.game_over()) return false
        if ((game.turn() === 'w' && piece.search(/^b/) !== -1) ||
            (game.turn() === 'b' && piece.search(/^w/) !== -1)) return false
        """

        script2 = f"""
        function onDragStart (source, piece, position, orientation) {{{game_over_}}}
        """

        script3 = """
        function onDrop (source, target) {
          // see if the move is legal
          var move = game.move({
            from: source,
            to: target,
            promotion: 'q' // NOTE: always promote to a queen for example simplicity
          })

          // illegal move
          if (move === null) return 'snapback'

          if (window.parent) {
            console.log("in iframe")
            window.parent.stBridges.send("my-bridge", {'move': move, 'fen': game.fen(), 'pgn': game.pgn()});
          }
          else {
            console.log("not in iframe")
            window.stBridges.send("my-bridge", {'move': move, 'fen': game.fen(), 'pgn': game.pgn()});
          }
          updateStatus()
        }
        """

        script4 = """
        // update the board position after the piece snap
        // for castling, en passant, pawn promotion
        function onSnapEnd () {
          board.position(game.fen())
        }

        function updateStatus () {
          var status = ''

          var moveColor = 'White'
          if (game.turn() === 'b') {
            moveColor = 'Black'
          }

          if (game.in_checkmate()) {
            status = 'Game over, ' + moveColor + ' is in checkmate.'
          }

          // draw?
          else if (game.in_draw()) {
            status = 'Game over, drawn position'
          }

          // game still on
          else {
            status = moveColor + ' to move'

            // check?
            if (game.in_check()) {
              status += ', ' + moveColor + ' is in check'
            }
          }
          $status.html(status)
        }
        """

        config_ = f"""
        pieceTheme: 'https://chessboardjs.com/img/chesspieces/wikipedia/{{piece}}.png',
        position: '{self.fen}',
        orientation: '{sidetomove}',
        draggable: true,
        onDragStart: onDragStart,
        onDrop: onDrop,
        onSnapEnd: onSnapEnd
        """

        script5 = f"""
        var config = {{{config_}}}
        board = Chessboard('myBoard', config)

        updateStatus()
        """

        ret = []

        ret.append(self.__header__())
        ret.append(self.__board_placeholder__())
        ret.append('<script>')
        ret.append(script1)
        ret.append(script2)
        ret.append(script3)
        ret.append(script4)
        ret.append(script5)
        ret.append('</script>')

        return '\n'.join(ret)