def mousePressEvent(self, event): if (event.buttons() != QtCore.Qt.LeftButton ) or (self.winner is not None) or ( self.whoseround != self.player_color) or (not self.is_gaming): return # 保证只在棋盘范围内响应 if event.x() >= 50 and event.x() <= 50 + 30 * 18 + 14 and event.y( ) >= 50 and event.y() <= 50 + 30 * 18 + 14: pos = Pixel2Chesspos(event) # 保证落子的地方本来没有人落子 if self.chessboard[pos[0]][pos[1]]: return # 实例化一个棋子并显示 c = Chessman(self.cfg.CHESSMAN_IMAGEPATHS.get(self.whoseround), self) c.move(event.pos()) c.show() self.chessboard[pos[0]][pos[1]] = c # 落子声音响起 self.drop_sound.play() # 最后落子位置标志对落子位置进行跟随 self.chessman_sign.show() self.chessman_sign.move(c.pos()) self.chessman_sign.raise_() # 记录这次落子 self.history_record.append([*pos, self.whoseround]) # 是否胜利了 self.winner = checkWin(self.chessboard) if self.winner: self.showGameEndInfo() return # 切换回合方(其实就是改颜色) self.nextRound()
def aiAct(self): if (self.winner is not None) or (self.whoseround == self.player_color) or (not self.is_gaming): return next_pos = self.ai_player.act(self.history_record) # 实例化一个棋子并显示 c = Chessman(self.cfg.CHESSMAN_IMAGEPATHS.get(self.whoseround), self) c.move(QPoint(*Chesspos2Pixel(next_pos))) c.show() self.chessboard[next_pos[0]][next_pos[1]] = c # 落子声音响起 self.drop_sound.play() # 最后落子位置标志对落子位置进行跟随 self.chessman_sign.show() self.chessman_sign.move(c.pos()) self.chessman_sign.raise_() # 记录这次落子 self.history_record.append([*next_pos, self.whoseround]) # 是否胜利了 self.winner = checkWin(self.chessboard) if self.winner: self.showGameEndInfo() return # 切换回合方(其实就是改颜色) self.nextRound()
def regret(self): if (self.winner is not None) or (len(self.history_record) == 0) or (not self.is_gaming) : return pre_round = self.history_record.pop(-1) self.chessboard[pre_round[0]][pre_round[1]].close() self.chessboard[pre_round[0]][pre_round[1]] = None c = Chessman(self.cfg.CHESSMAN_IMAGEPATHS.get(self.whoseround), self) c.move(QPoint(*Chesspos2Pixel(pre_round))) self.chessman_sign.move(c.pos()) self.chessman_sign.show() self.nextRound() if self.whoseround == self.player1_color: d = PushButton(self.cfg.BUTTON_IMAGEPATHS.get('turn1'), self) d.move(660, 170) d.show() else: d = PushButton(self.cfg.BUTTON_IMAGEPATHS.get('turn2'), self) d.move(660, 170) d.show()
def mousePressEvent(self, event): if (self.tcp_socket is None) or ( event.buttons() != QtCore.Qt.LeftButton ) or (self.winner is not None) or ( self.whoseround != self.player_color) or (not self.is_gaming): return # 保证只在棋盘范围内响应 if event.x() >= 50 and event.x() <= 50 + 30 * 18 + 14 and event.y( ) >= 50 and event.y() <= 50 + 30 * 18 + 14: pos = Pixel2Chesspos(event) # 保证落子的地方本来没有人落子 if self.chessboard[pos[0]][pos[1]]: return # 实例化一个棋子并显示 if self.whoseround == self.opponent_player_color: d = PushButton(self.cfg.BUTTON_IMAGEPATHS.get('turn2'), self) d.move(660, 170) d.show() else: d = PushButton(self.cfg.BUTTON_IMAGEPATHS.get('turn1'), self) d.move(660, 170) d.show() self.setup_ui() c = Chessman(self.cfg.CHESSMAN_IMAGEPATHS.get(self.whoseround), self) c.move(event.pos()) c.show() self.chessboard[pos[0]][pos[1]] = c # 落子声音响起 self.drop_sound.play() # 最后落子位置标志对落子位置进行跟随 self.chessman_sign.show() self.chessman_sign.move(c.pos()) self.chessman_sign.raise_() # 记录这次落子 self.history_record.append([*pos, self.whoseround]) # 发送给对方自己的落子位置 data = {'type': 'action', 'detail': 'drop', 'data': pos} self.tcp_socket.sendall(packSocketData(data)) # 是否胜利了 self.winner = checkWin(self.chessboard) if self.winner: self.showGameEndInfo() return # 切换回合方(其实就是改颜色) self.nextRound()
def responseForReceiveData(self, data): if data['type'] == 'action' and data['detail'] == 'exit': QMessageBox.information(self, '提示', '您的对手已退出游戏, 游戏将自动返回主界面') self.goHome() elif data['type'] == 'action' and data['detail'] == 'startgame': self.opponent_player_color, self.player_color = data['data'] self.whoseround = 'white' self.whoseround2nickname_dict = { self.player_color: self.nickname, self.opponent_player_color: self.opponent_nickname } res = QMessageBox.information( self, '提示', '对方请求(重新)开始游戏, 您为%s, 您是否同意?' % { 'white': '白子', 'black': '黑子' }.get(self.player_color), QMessageBox.Yes | QMessageBox.No) if res == QMessageBox.Yes: data = {'type': 'reply', 'detail': 'startgame', 'data': True} self.tcp_socket.sendall(packSocketData(data)) self.is_gaming = True self.setWindowTitle('可圈可点五子棋-联机对战') for i, j in product(range(19), range(19)): if self.chessboard[i][j]: self.chessboard[i][j].close() self.chessboard[i][j] = None self.history_record.clear() self.winner = None if self.winner_info_label: self.winner_info_label.close() self.winner_info_label = None self.chessman_sign.hide() else: data = {'type': 'reply', 'detail': 'startgame', 'data': False} self.tcp_socket.sendall(packSocketData(data)) elif data['type'] == 'action' and data['detail'] == 'drop': pos = data['data'] # 实例化一个棋子并显示 c = Chessman(self.cfg.CHESSMAN_IMAGEPATHS.get(self.whoseround), self) c.move(QPoint(*Chesspos2Pixel(pos))) c.show() self.chessboard[pos[0]][pos[1]] = c # 落子声音响起 self.drop_sound.play() # 最后落子位置标志对落子位置进行跟随 self.chessman_sign.show() self.chessman_sign.move(c.pos()) self.chessman_sign.raise_() # 记录这次落子 self.history_record.append([*pos, self.whoseround]) # 是否胜利了 self.winner = checkWin(self.chessboard) if self.winner: self.showGameEndInfo() return # 切换回合方(其实就是改颜色) self.nextRound() elif data['type'] == 'action' and data['detail'] == 'givein': self.winner = self.player_color self.showGameEndInfo() elif data['type'] == 'action' and data['detail'] == 'urge': self.urge_sound.play() elif data['type'] == 'action' and data['detail'] == 'regret': res = QMessageBox.information(self, '提示', '对方请求悔棋, 您是否同意?', QMessageBox.Yes | QMessageBox.No) if res == QMessageBox.Yes: pre_round = self.history_record.pop(-1) self.chessboard[pre_round[0]][pre_round[1]].close() self.chessboard[pre_round[0]][pre_round[1]] = None self.chessman_sign.hide() self.nextRound() data = {'type': 'reply', 'detail': 'regret', 'data': True} self.tcp_socket.sendall(packSocketData(data)) else: data = {'type': 'reply', 'detail': 'regret', 'data': False} self.tcp_socket.sendall(packSocketData(data)) elif data['type'] == 'reply' and data['detail'] == 'startgame': if data['data']: self.is_gaming = True self.setWindowTitle('可圈可点五子棋-联机对战') for i, j in product(range(19), range(19)): if self.chessboard[i][j]: self.chessboard[i][j].close() self.chessboard[i][j] = None self.history_record.clear() self.winner = None if self.winner_info_label: self.winner_info_label.close() self.winner_info_label = None self.chessman_sign.hide() QMessageBox.information( self, '提示', '对方同意开始游戏请求, 您为%s, 执白者先行.' % { 'white': '白子', 'black': '黑子' }.get(self.player_color)) else: QMessageBox.information(self, '提示', '对方拒绝了您开始游戏的请求.') elif data['type'] == 'reply' and data['detail'] == 'regret': if data['data']: pre_round = self.history_record.pop(-1) self.chessboard[pre_round[0]][pre_round[1]].close() self.chessboard[pre_round[0]][pre_round[1]] = None self.nextRound() QMessageBox.information(self, '提示', '对方同意了您的悔棋请求.') else: QMessageBox.information(self, '提示', '对方拒绝了您的悔棋请求.') elif data['type'] == 'nickname': self.opponent_nickname = data['data']