def initUI(self): self.chessboard = ChessBoard() # 棋盘类 self.palette1 = QPalette() # 设置棋盘背景 self.palette1.setBrush(QPalette.Background, QtGui.QBrush(QtGui.QPixmap('new board.jpg'))) self.setPalette(self.palette1) # self.setStyleSheet("board-image:url(img/chessboard.jpg)") # 不知道这为什么不行 self.setCursor(Qt.PointingHandCursor) # 鼠标变成手指形状 self.sound_piece = QSound("luozi.wav") # 加载落子音效 self.sound_win = QSound("win.wav") # 加载胜利音效 self.sound_defeated = QSound("defeated.wav") # 加载失败音效 self.thread = Searcher(skt, 0) self.resize(813, 591) # 固定大小 540*540 #self.setMinimumSize(QtCore.QSize(WIDTH, HEIGHT)) #self.setMaximumSize(QtCore.QSize(WIDTH, HEIGHT)) self.setWindowTitle("单人模式") self.setWindowIcon(QIcon('black.png')) self.pushButton_back = QPushButton(self) self.pushButton_back.setText("退出游戏") self.pushButton_back.move(10, 550) self.pushButton_back.clicked.connect(self.on_pushButton_back_clicked) self.black = QPixmap('black.png') self.white = QPixmap('white.png') self.piece_now = BLACK self.my_turn = True self.step = 0 self.x, self.y = 1000, 1000 # self.mouse_point = LaBel(self) # self.mouse_point.setScaledContents(True) # self.mouse_point.setPixmap(self.black) # self.mouse_point.setGeometry(270, 270, PIECE, PIECE) self.pieces = [QLabel(self) for i in range(225)] for piece in self.pieces: piece.setVisible(True) piece.setScaledContents(True) # self.mouse_point.raise_() self.ai_down = True # self.setMouseTracking(True) self.show()
class Game: def __init__(self): self.board = ChessBoard() self.engine = Engine() pass def play_game(self, withAI=False, verbose=False): while not self.is_game_over(): if verbose: print self.board if withAI: if self.board.is_whites_turn(): self.play_move() else: self.play_ai_move() else: self.play_move() print str(self.board) + "\n" + "Game Over! {}".format(self.board.outcome) def auto_play(self): while not self.is_game_over(): self.play_ai_move() print str(self.board) + "\n" + "Game Over! {}".format(self.board.outcome) def is_game_over(self): return self.board.is_game_over def play_move(self): move = raw_input("Enter move: (example: e2e4): ") if not self.board.attempt_to_make_move(move): print "\n Invalid Move! \n" def play_ai_move(self): origin, destination = self.engine.get_one_ply_materialistic_move(self.board) self.board.execute_move(origin, destination) print "Computer plays: {}{}".format(origin, destination)
class single_player(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.chessboard = ChessBoard() # 棋盘类 self.palette1 = QPalette() # 设置棋盘背景 self.palette1.setBrush(QPalette.Background, QtGui.QBrush(QtGui.QPixmap('new board.jpg'))) self.setPalette(self.palette1) # self.setStyleSheet("board-image:url(img/chessboard.jpg)") # 不知道这为什么不行 self.setCursor(Qt.PointingHandCursor) # 鼠标变成手指形状 self.sound_piece = QSound("luozi.wav") # 加载落子音效 self.sound_win = QSound("win.wav") # 加载胜利音效 self.sound_defeated = QSound("defeated.wav") # 加载失败音效 self.thread = Searcher(skt, 0) self.resize(813, 591) # 固定大小 540*540 #self.setMinimumSize(QtCore.QSize(WIDTH, HEIGHT)) #self.setMaximumSize(QtCore.QSize(WIDTH, HEIGHT)) self.setWindowTitle("单人模式") self.setWindowIcon(QIcon('black.png')) self.pushButton_back = QPushButton(self) self.pushButton_back.setText("退出游戏") self.pushButton_back.move(10, 550) self.pushButton_back.clicked.connect(self.on_pushButton_back_clicked) self.black = QPixmap('black.png') self.white = QPixmap('white.png') self.piece_now = BLACK self.my_turn = True self.step = 0 self.x, self.y = 1000, 1000 # self.mouse_point = LaBel(self) # self.mouse_point.setScaledContents(True) # self.mouse_point.setPixmap(self.black) # self.mouse_point.setGeometry(270, 270, PIECE, PIECE) self.pieces = [QLabel(self) for i in range(225)] for piece in self.pieces: piece.setVisible(True) piece.setScaledContents(True) # self.mouse_point.raise_() self.ai_down = True # self.setMouseTracking(True) self.show() def on_pushButton_back_clicked(self): self.close() def paintEvent(self, event): self.qp = QPainter() self.qp.begin(self) self.drawLines(self.qp) self.qp.end() #def mouseMoveEvent(self, e): # self.lb1.setText(str(e.x()) + ' ' + str(e.y())) # self.mouse_point.move(e.x() - 16, e.y() - 16) def mousePressEvent(self, e): if e.button() == Qt.LeftButton and self.ai_down == True: x, y = e.x(), e.y() i, j = self.coordinate_transform_pixel2map(x, y) if not i is None and not j is None: if self.chessboard.get_xy_on_logic_state(i, j) == EMPTY: self.draw(i, j) self.ai_down = False board = self.chessboard.board() self.AI = AI(board) self.AI.finishSignal.connect(self.AI_draw) self.AI.start() def AI_draw(self, i, j): if self.step != 0: self.draw(i, j) self.x, self.y = self.coordinate_transform_map2pixel(i, j) self.ai_down = True self.update() def draw(self, i, j): x, y = self.coordinate_transform_map2pixel(i, j) if self.piece_now == BLACK: self.pieces[self.step].setPixmap(self.black) self.piece_now = WHITE self.chessboard.draw_xy(i, j, BLACK) else: self.pieces[self.step].setPixmap(self.white) self.piece_now = BLACK self.chessboard.draw_xy(i, j, WHITE) self.pieces[self.step].setGeometry(x, y, PIECE, PIECE) self.sound_piece.play() self.step += 1 self.winner = self.chessboard.anyone_win(i, j) if self.winner != EMPTY: # self.mouse_point.clear() self.gameover(self.winner) def drawLines(self, qp): if self.step != 0: pen = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine) qp.setPen(pen) qp.drawLine(self.x - 5, self.y - 5, self.x + 3, self.y + 3) qp.drawLine(self.x + 3, self.y, self.x + 3, self.y + 3) qp.drawLine(self.x, self.y + 3, self.x + 3, self.y + 3) def coordinate_transform_map2pixel(self, i, j): return MARGIN + j * GRID - PIECE / 2, MARGIN + i * GRID - PIECE / 2 def coordinate_transform_pixel2map(self, x, y): i, j = int(round((y - MARGIN) / GRID)), int(round((x - MARGIN) / GRID)) if i < 0 or i >= 15 or j < 0 or j >= 15: return None, None else: return i, j def gameover(self, winner): if winner == BLACK: self.sound_win.play() reply = QMessageBox.question(self, '你赢了!', '开始新的一局?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) else: self.sound_defeated.play() reply = QMessageBox.question(self, '你输了!', '开始新的一局?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: # 复位 self.piece_now = BLACK # self.mouse_point.setPixmap(self.black) self.step = 0 for piece in self.pieces: piece.clear() self.chessboard.reset() self.update() else: self.close()
#automatic tester file from Board import ChessBoard Chess = ChessBoard() Chess.setUpBoard() print(Chess) #start of movement/move 1 #e4 Chess.performMoveWhite(4,1,4,3) print(Chess) #d5 Chess.performMoveBlack(3,6,3,4) print(Chess) #e4xd5 Chess.performMoveWhite(4,3,3,4) print(Chess) #e5 Chess.performMoveBlack(4,6,4,4) print(Chess) #d5xe5 Chess.performMoveWhite(3,4,4,5) print(Chess) #Qe7 Chess.performMoveBlack(3,7,4,6) print(Chess) #Nf3 Chess.performMoveWhite(6,0,5,2) print(Chess)
def __init__(self): self.board = ChessBoard() self.engine = Engine() pass