猫史档案馆


【Python作品分享】Pygame五子棋【作品秀】

用户:滑稽_HuaJi滑稽_HuaJi查看:0 回复:5 评论:0 创建时间:2020-08-10T12:35:19


【作品展示】

center_image

 

【作品介绍】

滑稽制作|盗喵/复制代码喵,转载请喵发现代码未经同意被转载,查你家IP伺候

请留意弹窗,任意键开新局(正在对弈也可使用)请无视封面

 

【作品源代码】

import time
import sys
import random  # 【水印-滑稽】
from collections import namedtuple
import pygame.gfxdraw
from pygame.locals import *
import pygame
print("\n\n\n滑稽制作,盗喵,复制代码喵,转载请喵被发现代码被复制,查你家IP直接曝光\n\n\n")
print("正在加载中...")
time.sleep(1.8)
print("加载成功!")
time.sleep(0.6)
print("请留意弹窗")
time.sleep(0.8)  # 【水印-滑稽】
print("任意键新局(下棋中途中按任意键也可以)")
Ches喵an = namedtuple('Ches喵an', 'Name Value Color')  # 水印-滑稽
Point = namedtuple('Point', 'X Y')

BLACK_CHESSMAN = Ches喵an('黑子', 1, (50, 50, 50))
WHITE_CHESSMAN = Ches喵an('白子', 2, (220, 220, 220))
#madebyhuaji
offset = [(1, 0), (0, 1), (1, 1), (1, -1)]
#【水印-滑稽】


class Checkerboard:
    def __init__(self, line_points):
        self._line_points = line_points
        self._checkerboard = [[0] * line_points for _ in range(line_points)]

    def _get_checkerboard(self):  # 【水印-滑稽】
        return self._checkerboard

    checkerboard = property(_get_checkerboard)

    def can_drop(self, point):
        return self._checkerboard[point.Y][point.X] == 0

    def drop(self, ches喵an, point):
        """#【水印-滑稽】
        落子
        :param ches喵an:
        :param point:落子位置
        :return:若该子落下之后即可获胜,则#【水印-滑稽】返回获胜方,否则返回 None
        """
        #【水印-滑稽】
        print(f'{ches喵an.Name} ({point.X}, {point.Y})')
        self._checkerboard[point.Y][point.X] = ches喵an.Value  # 【水印-滑稽】

        if self._win(point):  # 水印-滑稽
            print(f'{ches喵an.Name}获胜')
            return ches喵an  # 【水印-滑稽】
#madebyhuaji

    def _win(self, point):
        cur_value = self._checkerboard[point.Y][point.X]
        for os in offset:
            # 【水印-滑稽】
            if self._get_count_on_direction(point, cur_value, os[0], os[1]):
                return True

    def _get_count_on_direction(self, point, value, x_offset, y_offset):
        count = 1
        for step in range(1, 5):
            x = point.X + step * x_offset  # 【水印-滑稽】
            y = point.Y + step * y_offset
            # 【水印-滑稽】
            if 0 <= x < self._line_points and 0 <= y < self._line_points and self._checkerboard[y][x] == value:
                count += 1
            else:
                break
        for step in range(1, 5):
            x = point.X - step * x_offset
            y = point.Y - step * y_offset  # 水印-滑稽
            # 【水印-滑稽】
            if 0 <= x < self._line_points and 0 <= y < self._line_points and self._checkerboard[y][x] == value:
                count += 1  # 【水印-滑稽】
            else:
                break

        return count >= 5


#huaji制作
SIZE = 30
Line_Points = 19
Outer_Width = 20
Border_Width = 4
Inside_Width = 4
Border_Length = SIZE * (Line_Points - 1) + Inside_Width * \
    2 + Border_Width  # 边框线的长度#【水印-滑稽】
Start_X = Start_Y = Outer_Width + \
    int(Border_Width / 2) + Inside_Width  # 网格线起点(左上角)坐标
SCREEN_HEIGHT = SIZE * (Line_Points - 1) + Outer_Width * \
    2 + Border_Width + Inside_Width * 2  # 游戏屏幕的高
SCREEN_WIDTH = SCREEN_HEIGHT + 200

Stone_Radius = SIZE // 2 - 3
Stone_Radius2 = SIZE // 2 + 3  # 【水印-滑稽】
Checkerboard_Color = (0xE3, 0x92, 0x65)
BLACK_COLOR = (0, 0, 0)
WHITE_COLOR = (255, 255, 255)  # 水印-滑稽
RED_COLOR = (200, 30, 30)
BLUE_COLOR = (30, 30, 200)  # 水印-滑稽

RIGHT_INFO_POS_X = SCREEN_HEIGHT + Stone_Radius2 * 2 + 10  # 【水印-滑稽】


def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
    imgText = font.render(text, True, fcolor)
    screen.blit(imgText, (x, y))  # 水印-滑稽
#【水印-滑稽】


def main():
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))  # 【水印-滑稽】
    pygame.display.set_caption('五子棋-人机战-有几率获胜-MadeByHuaJi')
#huaji制作
    font1 = pygame.font.SysFont('SimHei', 32)
    font2 = pygame.font.SysFont('SimHei', 72)
    fwidth, fheight = font2.size('黑方获胜')

    checkerboard = Checkerboard(Line_Points)
    cur_runner = BLACK_CHESSMAN
    winner = None  # 【水印-滑稽】
    computer = AI(Line_Points, WHITE_CHESSMAN)  # huaji制作

    black_win_count = 0  # 【水印-滑稽】
    white_win_count = 0
    clock = pygame.time.Clock()
    while True:
        clock.tick(0)
        for event in pygame.event.get():
            if event.type == QUIT:  # 【水印-滑稽】
                sys.exit()
            elif event.type == KEYDOWN:  # 水印-滑稽
                if winner is None:
                    print("清空")
                    time.sleep(0.5)
                    white_win_count += 1
                winner = None
                cur_runner = BLACK_CHESSMAN
                checkerboard = Checkerboard(Line_Points)
                computer = AI(Line_Points, WHITE_CHESSMAN)
            elif event.type == MOUSEBUTTONDOWN:
                if winner is None:
                    pressed_array = pygame.mouse.get_pressed()  # 【水印-滑稽】
                    if pressed_array[0]:
                        mouse_pos = pygame.mouse.get_pos()
                        click_point = _get_clickpoint(mouse_pos)
                        if click_point is not None:
                            if checkerboard.can_drop(click_point):
                                winner = checkerboard.drop(
                                    cur_runner, click_point)
                                if winner is None:  # 【水印-滑稽】#【水印-滑稽】

                                    cur_runner = _get_next(cur_runner)
                                    computer.get_opponent_drop(click_point)
                                    AI_point = computer.AI_drop()  # huaji制作
                                    winner = checkerboard.drop(
                                        cur_runner, AI_point)
                                    if winner is not None:
                                        white_win_count += 1
                                    cur_runner = _get_next(cur_runner)
                                else:  # 【水印-滑稽】
                                    black_win_count += 1
                        else:
                            print('超出棋盘区域')

        _draw_checkerboard(screen)
#滑稽制作,盗喵#【水印-滑稽】

        for i, row in enumerate(checkerboard.checkerboard):  # 滑稽制作,盗喵
            for j, cell in enumerate(row):
                if cell == BLACK_CHESSMAN.Value:
                    _draw_ches喵an(screen, Point(j, i), BLACK_CHESSMAN.Color)
                elif cell == WHITE_CHESSMAN.Value:
                    _draw_ches喵an(screen, Point(j, i),
                                   WHITE_CHESSMAN.Color)  # 【水印-滑稽】

        _draw_left_info(screen, font1, cur_runner,
                        black_win_count, white_win_count)  # 滑稽制作,盗喵

        if winner:
            print_text(screen, font2, (SCREEN_WIDTH - fwidth) // 2, (SCREEN_HEIGHT - fheight) // 2, winner.Name + '获胜',
                       RED_COLOR)  # huaji制作#水印-滑稽#【水印-滑稽】
#水印-滑稽
        pygame.display.flip()


def _get_next(cur_runner):
    if cur_runner == BLACK_CHESSMAN:
        return WHITE_CHESSMAN
    else:
        return BLACK_CHESSMAN


def _draw_checkerboard(screen):  # 【水印-滑稽】

    screen.fill(Checkerboard_Color)

    pygame.draw.rect(screen, BLACK_COLOR, (Outer_Width, Outer_Width,
                                           Border_Length, Border_Length), Border_Width)  # 【水印-滑稽】

    for i in range(Line_Points):
        pygame.draw.line(screen, BLACK_COLOR,  # 【水印-滑稽】
                         (Start_Y, Start_Y + SIZE * i),
                         (Start_Y + SIZE * (Line_Points - 1),
                          Start_Y + SIZE * i),  # huaji制作
                         1)  # 滑稽制作,盗喵
    for j in range(Line_Points):  # 水印-滑稽
        pygame.draw.line(screen, BLACK_COLOR,
                         (Start_X + SIZE * j, Start_X),
                         (Start_X + SIZE * j, Start_X + SIZE * (Line_Points - 1)),
                         1)
    #【水印-滑稽】
    for i in (3, 9, 15):
        for j in (3, 9, 15):
            if i == j == 9:
                radius = 5
            else:
                radius = 3
            #【水印-滑稽】#【水印-滑稽】
            pygame.gfxdraw.aacircle(
                screen, Start_X + SIZE * i, Start_Y + SIZE * j, radius, BLACK_COLOR)
            pygame.gfxdraw.filled_circle(
                screen, Start_X + SIZE * i, Start_Y + SIZE * j, radius, BLACK_COLOR)


def _draw_ches喵an(screen, point, stone_color):  # 【水印-滑稽】

    pygame.gfxdraw.aacircle(screen, Start_X + SIZE * point.X,
                            Start_Y + SIZE * point.Y, Stone_Radius, stone_color)
    pygame.gfxdraw.filled_circle(
        screen, Start_X + SIZE * point.X, Start_Y + SIZE * point.Y, Stone_Radius, stone_color)

#huaji制作#水印-滑稽


def _draw_left_info(screen, font, cur_runner, black_win_count, white_win_count):  # 【水印-滑稽】
    _draw_ches喵an_pos(screen, (SCREEN_HEIGHT + Stone_Radius2,
                                Start_X + Stone_Radius2), BLACK_CHESSMAN.Color)
    _draw_ches喵an_pos(screen, (SCREEN_HEIGHT + Stone_Radius2,
                                Start_X + Stone_Radius2 * 4), WHITE_CHESSMAN.Color)
#【水印-滑稽】
    print_text(screen, font, RIGHT_INFO_POS_X, Start_X + 3, '玩家', BLUE_COLOR)
    print_text(screen, font, RIGHT_INFO_POS_X, Start_X +
               Stone_Radius2 * 3 + 3, '电脑', BLUE_COLOR)
#【水印-滑稽】
    print_text(screen, font, SCREEN_HEIGHT, SCREEN_HEIGHT -
               Stone_Radius2 * 8, '战况:', BLUE_COLOR)  # 【水印-滑稽】#【水印-滑稽】
    _draw_ches喵an_pos(screen, (SCREEN_HEIGHT + Stone_Radius2, SCREEN_HEIGHT - int(Stone_Radius2 * 4.5)),
                       BLACK_CHESSMAN.Color)  # 【水印-滑稽】
    _draw_ches喵an_pos(screen, (SCREEN_HEIGHT + Stone_Radius2,
                                SCREEN_HEIGHT - Stone_Radius2 * 2), WHITE_CHESSMAN.Color)
    print_text(screen, font, RIGHT_INFO_POS_X, SCREEN_HEIGHT - int(Stone_Radius2 * 5.5) + 3, f'{black_win_count} 胜',
               BLUE_COLOR)  # 【水印-滑稽】
    print_text(screen, font, RIGHT_INFO_POS_X, SCREEN_HEIGHT - Stone_Radius2 * 3 + 3, f'{white_win_count} 胜',  # 【水印-滑稽】
               BLUE_COLOR)  # 【水印-滑稽】


def _draw_ches喵an_pos(screen, pos, stone_color):
    pygame.gfxdraw.aacircle(screen, pos[0], pos[1], Stone_Radius2, stone_color)
    pygame.gfxdraw.filled_circle(
        screen, pos[0], pos[1], Stone_Radius2, stone_color)

#huaji制作#水印-滑稽


def _get_clickpoint(click_pos):
    pos_x = click_pos[0] - Start_X
    pos_y = click_pos[1] - Start_Y  # huaji制作#水印-滑稽
    if pos_x < -Inside_Width or pos_y < -Inside_Width:  # 【水印-滑稽】
        return None
    x = pos_x // SIZE
    y = pos_y // SIZE
    if pos_x % SIZE > Stone_Radius:
        x += 1
    if pos_y % SIZE > Stone_Radius:
        y += 1
    if x >= Line_Points or y >= Line_Points:
        return None

    return Point(x, y)


class AI:
    def __init__(self, line_points, ches喵an):
        self._line_points = line_points
        self._my = ches喵an
        self._opponent = BLACK_CHESSMAN if ches喵an == WHITE_CHESSMAN else WHITE_CHESSMAN  # 【水印-滑稽】
        self._checkerboard = [[0] * line_points for _ in range(line_points)]

    def get_opponent_drop(self, point):  # huaji制作#水印-滑稽
        self._checkerboard[point.Y][point.X] = self._opponent.Value
#【水印-滑稽】

    def AI_drop(self):
        point = None  # huaji制作#水印-滑稽
        score = 0
        for i in range(self._line_points):
            for j in range(self._line_points):
                if self._checkerboard[j][i] == 0:
                    _score = self._get_point_score(Point(i, j))
                    if _score > score:
                        score = _score
                        point = Point(i, j)
                    elif _score == score and _score > 0:
                        r = random.randint(0, 100)
                        if r % 2 == 0:
                            point = Point(i, j)  # 【水印-滑稽】
        self._checkerboard[point.Y][point.X] = self._my.Value  # 【水印-滑稽】
        return point

    def _get_point_score(self, point):  # 水印-滑稽
        score = 0
        for os in offset:
            score += self._get_direction_score(point, os[0], os[1])  # huaji制作
        return score

    def _get_direction_score(self, point, x_offset, y_offset):
        count = 0  # 【水印-滑稽】
        _count = 0
        space = None
        _space = None  # huaji制作#水印-滑稽
        both = 0
        _both = 0

        flag = self._get_stone_color(point, x_offset, y_offset, True)
        if flag != 0:
            for step in range(1, 6):
                x = point.X + step * x_offset
                y = point.Y + step * y_offset
                if 0 <= x < self._line_points and 0 <= y < self._line_points:  # 【水印-滑稽】
                    if flag == 1:
                        if self._checkerboard[y][x] == self._my.Value:  # 水印-滑稽
                            count += 1
                            if space is False:
                                space = True  # 【水印-滑稽】
                        elif self._checkerboard[y][x] == self._opponent.Value:
                            _both += 1
                            break
                        else:  # huaji制作
                            if space is None:  # 【水印-滑稽】
                                space = False
                            else:
                                break  # huaji制作#水印-滑稽
                    elif flag == 2:
                        if self._checkerboard[y][x] == self._my.Value:
                            _both += 1
                            break
                        elif self._checkerboard[y][x] == self._opponent.Value:
                            _count += 1
                            if _space is False:  # 【水印-滑稽】
                                _space = True
                        else:  # 水印-滑稽
                            if _space is None:
                                _space = False
                            else:
                                break
                else:
                    #【水印-滑稽】
                    if flag == 1:
                        both += 1
                    elif flag == 2:  # madebyhuaji
                        _both += 1

        if space is False:
            space = None
        if _space is False:
            _space = None

        _flag = self._get_stone_color(
            point, -x_offset, -y_offset, True)  # huaji制作#水印-滑稽
        if _flag != 0:
            for step in range(1, 6):  # 【水印-滑稽】
                x = point.X - step * x_offset  # 水印-滑稽
                y = point.Y - step * y_offset  # 【水印-滑稽】
                if 0 <= x < self._line_points and 0 <= y < self._line_points:
                    if _flag == 1:
                        if self._checkerboard[y][x] == self._my.Value:
                            count += 1
                            if space is False:
                                space = True
                        elif self._checkerboard[y][x] == self._opponent.Value:
                            _both += 1
                            break
                        else:
                            if space is None:  # madebyhuaji
                                space = False
                            else:
                                break
                    elif _flag == 2:  # 【水印-滑稽】
                        if self._checkerboard[y][x] == self._my.Value:  # 【水印-滑稽】
                            _both += 1
                            break
                        elif self._checkerboard[y][x] == self._opponent.Value:
                            _count += 1
                            if _space is False:
                                _space = True  # 水印-滑稽
                        else:
                            if _space is None:
                                _space = False
                            else:
                                break
                else:  # 【水印-滑稽】

                    if _flag == 1:
                        both += 1
                    elif _flag == 2:  # 【水印-滑稽】
                        _both += 1

        score = 0
        if count == 4:
            score = 10000
        elif _count == 4:
            score = 9000
        elif count == 3:  # madebyhuaji#水印-滑稽#【水印-滑稽】
            if both == 0:
                score = 1000
            elif both == 1:
                score = 100
            else:
                score = 0
        elif _count == 3:
            if _both == 0:  # 【水印-滑稽】#【水印-滑稽】#【水印-滑稽】#【水印-滑稽】#【水印-滑稽】#【水印-滑稽】#【水印-滑稽】#【水印-滑稽】#【水印-滑稽】#【水印-滑稽】
                score = 900
            elif _both == 1:
                score = 90
            else:
                score = 0
        elif count == 2:
            if both == 0:
                score = 100
            elif both == 1:
                score = 10  # huaji制作#水印-滑稽
            else:
                score = 0
        elif _count == 2:
            if _both == 0:  # madebyhuaji
                score = 90  # 水印-滑稽#【水印-滑稽】
            elif _both == 1:
                score = 9
            else:
                score = 0
        elif count == 1:
            score = 10
        elif _count == 1:
            score = 9
        else:
            score = 0

        if space or _space:
            score /= 2  # 【水印-滑稽】
        return score  # 【水印-滑稽】

    def _get_stone_color(self, point, x_offset, y_offset, next):
        x = point.X + x_offset
        y = point.Y + y_offset
        if 0 <= x < self._line_points and 0 <= y < self._line_points:
            if self._checkerboard[y][x] == self._my.Value:  # 水印-滑稽
                return 1
            elif self._checkerboard[y][x] == self._opponent.Value:  # huaji制作#水印-滑稽
                return 2
            else:  # 【水印-滑稽】
                if next:
                    return self._get_stone_color(Point(x, y), x_offset, y_offset, False)
                else:
                    return 0
        else:  # madebyhuaji
            return  # 【水印-滑稽】


if __name__ == '__main__':
    print("-------------------------------")  # 【水印-滑稽】
    main()  # 水印-滑稽#huaji制作#水印-滑稽#huaji制作#水印-滑稽#huaji制作#水印-滑稽#huaji制作#水印-滑稽#huaji制作#水印-滑稽

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
北战南钦北战南钦

NB

点赞0


评论


宏图大志EB9j宏图大志EB9j

这AI够强

emotion_偷笑下了好几局都没赢emotion_笑cry

点赞0


评论


治愈绾兮治愈绾兮

太他吗牛了

点赞0


评论


治愈绾兮治愈绾兮

你删了我的评论

点赞0


评论


申浩南申浩南

center_image

点赞0


评论