-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardWidget.py
121 lines (87 loc) · 3.45 KB
/
BoardWidget.py
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
from PyQt5 import QtWidgets, QtCore
import chess
import chess.pgn
from Cell import Cell
from PromotionDialog import PromotionDialog
from config import CELL_SIZE, FIELD_SIZE, WHITE_CELL_COLOR, BLACK_CELL_COLOR
class BoardWidget(QtWidgets.QWidget):
gameEnded = QtCore.pyqtSignal(str)
moveMade = QtCore.pyqtSignal(chess.Move)
def __init__(self, parent=None):
super().__init__(parent)
self.initUi()
self.gameStarted = False
def initUi(self):
self.resize(FIELD_SIZE, FIELD_SIZE)
self.cellsLayout = QtWidgets.QGridLayout(self)
self.initBoard()
def initBoard(self):
self.board = chess.Board()
for row in range(8):
for col in range(8):
cell = Cell(self, col, row, CELL_SIZE, CELL_SIZE)
cell.moveMade.connect(self.makeMove)
if (row + col) % 2 == 0:
cell.setColor(BLACK_CELL_COLOR)
else:
cell.setColor(WHITE_CELL_COLOR)
self.cellsLayout.addWidget(cell, 7 - row, col)
def updateBoard(self):
for row in range(8):
for col in range(8):
cell = self.cellsLayout.itemAtPosition(7 - row, col).widget()
square = chess.square(col, row)
piece = self.board.piece_at(square)
cell.setPiece(piece)
def startGame(self):
self.board.reset()
self.updateBoard()
self.gameStarted = True
def makeMove(self, firstCell, secondCell):
if self.gameStarted is False:
return
firstCellCoords = firstCell.getCoordinates()
secondCellCoords = secondCell.getCoordinates()
firstSquare = chess.square(*firstCellCoords)
secondSquare = chess.square(*secondCellCoords)
promotion = None
if self.board.piece_at(firstSquare).piece_type == chess.PAWN:
if secondCellCoords[1] == 0 or secondCellCoords[1] == 7:
promotion = chess.QUEEN
move = chess.Move(firstSquare, secondSquare, promotion=promotion)
if move in self.board.legal_moves:
if promotion is not None:
self.promotionDialog = PromotionDialog(self.parent(),
self.board.turn)
choosen = self.promotionDialog.exec()
if choosen == 0:
return
move.promotion = self.promotionDialog.getPromotion()
self.board.push(move)
self.moveMade.emit(move)
result = self.board.result()
if result != '*':
self.gameStarted = False
self.gameEnded.emit(result)
self.updateBoard()
def getCurrentTurn(self):
return self.board.turn
def isGameStarted(self):
return self.gameStarted
def forceLose(self, loser):
self.gameStarted = False
if loser == chess.WHITE:
self.gameEnded.emit("0-1")
else:
self.gameEnded.emit("1-0")
def hasInsufficientMaterial(self, color):
return self.board.has_insufficient_material(color)
def isDrawPossibleWithoutOpponentApproval(self):
return self.board.can_claim_draw()
def draw(self):
self.gameStarted = False
self.gameEnded.emit("1/2-1/2")
def getMovesQuantity(self):
return len(self.board.move_stack)
def getGame(self):
return chess.pgn.Game.from_board(self.board)