猫史档案馆


【Python】五子棋代码

用户:企鹅不是鹅企鹅不是鹅查看:0 回复:0 评论:0 创建时间:2023-05-01T08:04:07


此代码需搭配PyQt5使用。Python版本为3.11。代码如下,谢谢。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox, QPushButton
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt, QPoint

class ChessBoard(QWidget):
    def __init__(self):
        super().__init__()
        
        # 棋盘大小、格子大小、落子标记大小
        self.board_size = 500
        self.grid_size = self.board_size // 15
        self.mark_size = self.grid_size // 2
        
        # 初始化棋盘数据,0表示空位,1表示黑棋,2表示白棋
        self.chess_board = [[0] * 15 for _ in range(15)]
        
        # 当前玩家,默认为黑方先手
        self.current_player = 1
        
        # 标记是否结束游戏
        self.game_over = False
        
        # 设置窗口大小和标题
        self.setFixedSize(self.board_size + 50, self.board_size + 50)
        self.setWindowTitle('五子棋')
        
        # 设置按钮,点击后重新开始游戏
        self.reset_button = QPushButton('重新开始', self)
        self.reset_button.move(self.board_size + 10, 10)
        self.reset_button.clicked.connect(self.start_new_game)
        
        self.show()
    
    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing, True)
        painter.setBrush(QColor(249, 214, 91))
        painter.drawRect(0, 0, self.board_size, self.board_size)
        pen = QPen()
        pen.setWidth(2)
        pen.setColor(Qt.black)
        painter.setPen(pen)
        for i in range(15):
            painter.drawLine(self.grid_size // 2, self.grid_size // 2 + i * self.grid_size,
                             self.board_size - self.grid_size // 2, self.grid_size // 2 + i * self.grid_size)
            painter.drawLine(self.grid_size // 2 + i * self.grid_size, self.grid_size // 2,
                             self.grid_size // 2 + i * self.grid_size, self.board_size - self.grid_size // 2)
        for i in range(15):
            for j in range(15):
                if self.chess_board[i][j] == 1:
                    painter.setBrush(Qt.black)
                    painter.drawEllipse(QPoint((i + 0.5) * self.grid_size, (j + 0.5) * self.grid_size), self.mark_size, self.mark_size)
                elif self.chess_board[i][j] == 2:
                    painter.setBrush(Qt.white)
                    painter.drawEllipse(QPoint((i + 0.5) * self.grid_size, (j + 0.5) * self.grid_size), self.mark_size, self.mark_size)
    
    def mousePressEvent(self, event):
        if self.game_over:
            return
        x = event.pos().x() - self.grid_size // 2
        y = event.pos().y() - self.grid_size // 2
        if x < 0 or y < 0 or x > self.board_size or y > self.board_size:
            return
        row = x // self.grid_size
        col = y // self.grid_size
        if self.chess_board[row][col] != 0:
            return
        self.chess_board[row][col] = self.current_player
        self.update()
        if self.check_win(row, col):
            self.game_over = True
            QMessageBox.information(self, '游戏结束', ('黑方' if self.current_player == 1 else '白方') + '获胜!')
            return
        if self.check_full():
            self.game_over = True
            QMessageBox.information(self, '游戏结束', '平局!')
            return
        self.current_player = 3 - self.current_player
    
    def check_win(self, row, col):
        count = 1
        
        # 检查水平方向
        i = 1
        while row - i >= 0 and self.chess_board[row - i][col] == self.current_player:
            count += 1
            i += 1
        i = 1
        while row + i < 15 and self.chess_board[row + i][col] == self.current_player:
            count += 1
            i += 1
        if count >= 5:
            return True
        
        # 检查竖直方向
        count = 1
        i = 1
        while col - i >= 0 and self.chess_board[row][col - i] == self.current_player:
            count += 1
            i += 1
        i = 1
        while col + i < 15 and self.chess_board[row][col + i] == self.current_player:
            count += 1
            i += 1
        if count >= 5:
            return True
        
        # 检查左上到右下方向
        count = 1
        i = 1
        while row - i >= 0 and col - i >= 0 and self.chess_board[row - i][col - i] == self.current_player:
            count += 1
            i += 1
        i = 1
        while row + i < 15 and col + i < 15 and self.chess_board[row + i][col + i] == self.current_player:
            count += 1
            i += 1
        if count >= 5:
            return True
        
        # 检查左下到右上方向
        count = 1
        i = 1
        while row - i >= 0 and col + i < 15 and self.chess_board[row - i][col + i] == self.current_player:
            count += 1
            i += 1
        i = 1
        while row + i < 15 and col - i >= 0 and self.chess_board[row + i][col - i] == self.current_player:
            count += 1
            i += 1
        if count >= 5:
            return True
        
        return False
    
    def check_full(self):
        for i in range(15):
            for j in range(15):
                if self.chess_board[i][j] == 0:
                    return False
        return True
    
    def start_new_game(self):
        self.chess_board = [[0] * 15 for _ in range(15)]
        self.current_player = 1
        self.game_over = False
        self.update()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    chess_board = ChessBoard()
    sys.exit(app.exec_())


回复

上一页1 页 / 共 0下一页