From 32af10fd62eace5fe97d114bf17bc43a2f5365ff Mon Sep 17 00:00:00 2001 From: hnu202401010226 <3107859699@qq.com> Date: Tue, 24 Dec 2024 22:47:55 +0800 Subject: [PATCH] ADD file via upload --- ...棋人机作战-戴燕开、党睿璠.py | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 五子棋人机作战-戴燕开、党睿璠.py diff --git a/五子棋人机作战-戴燕开、党睿璠.py b/五子棋人机作战-戴燕开、党睿璠.py new file mode 100644 index 0000000..442855a --- /dev/null +++ b/五子棋人机作战-戴燕开、党睿璠.py @@ -0,0 +1,137 @@ +# -*- coding: utf-8 -*- +""" +Created on Tue Dec 24 21:06:31 2024 + +@author: 戴燕开 +""" + +import pygame +import os +import random + +# 初始化 pygame +pygame.init() + +# 设置窗口长宽 +screen = pygame.display.set_mode((600, 600)) +pygame.display.set_caption('五子棋-人机对战') # 设置 pygame 窗口名 + +space = 20 # 四周留下的边距 +cell_size = 40 # 每个格子大小 +cell_num = 15 #设置线条数 +chess_arr = [] #创建列表记录落子位置 + +def get_one_dire_num(lx, ly, dx, dy, m): + # 将最后的 x 坐标赋值给 tx + tx = lx + # 将最后的 y 坐标赋值给 tx + ty = ly + # 判断连子 + s = 0 + while True: + # 判断上下左右斜方向是否有连子 + tx += dx + ty += dy + # 如果没有的连子的话返回当前连子数量 + if tx < 0 or tx >= cell_num or ty < 0 or ty >= cell_num or m[ty][tx] == 0: + return s + # 连子累加 1 + s += 1 + +def check_win(chess_arr, flag): + m = [[0] * cell_num for i in range(cell_num)] # 先定义一个 15*15 的全 0 的数组 + for x, y, c in chess_arr: + if c == flag: + m[y][x] = 1 # 上面有棋则标 1 + lx = chess_arr[-1][0] # 最后一个子的 x + ly = chess_arr[-1][1] # 最后一个子的 y + dire_arr = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, -1), (1, 1)], + [(-1, 1), (1, -1)]] # 4 个方向数组 + for dire1, dire2 in dire_arr: + # 往左,往上,往左上,往左下判断是否有连子 + dx, dy = dire1 + num1 = get_one_dire_num(lx, ly, dx, dy, m) + # 往右,往下,往右下、往右上判断是否有连子 + dx, dy = dire2 + num2 = get_one_dire_num(lx, ly, dx, dy, m) + # 不同方向加起来的连子数为 5 的则获胜返回 True + if num1 + num2 + 1 >= 5: + return True + return False + +def computer_move(): + valid_moves = [] + for x in range(cell_num): + for y in range(cell_num): + if (x, y, 1) not in chess_arr and (x, y, 2) not in chess_arr: + valid_moves.append((x, y)) + if valid_moves: + move = random.choice(valid_moves) + chess_arr.append((move[0], move[1], 2)) + +flag = 1 # 1=》黑 2=》白 +game_state = 1 # 游戏状态 1.表示正常进行 2.表示白胜 3.表示黑胜 + +# 加载游戏背景音乐 +background_music = pygame.mixer.Sound(os.path.join('C:/Users/戴燕开/Music/宣传片温婉轻柔钢琴音效_爱给网_aigei_com.mp3')) # 替换为背景音乐文件路径 +background_music.play(-1) # -1 表示无限循环播放 + +# 加载落子音乐 +drop_sound = pygame.mixer.Sound(os.path.join('C:/Users/戴燕开/Music/棋子音效d_爱给网_aigei_com.mp3')) # 替换为落子音乐文件路径 + +while True: # 死循环确保窗口一直显示 + for event in pygame.event.get(): # 遍历所有事件 + if event.type == pygame.QUIT: # 如果单击关闭窗口,则退出 + pygame.quit() # 关闭 pygame 程序 + exit() # 结束程序 + if game_state == 1 and flag == 1 and event.type == pygame.MOUSEBUTTONUP: # 玩家(黑方)鼠标弹起 + x, y = pygame.mouse.get_pos() # 获取鼠标位置 + xi = int(round((x - space) * 1.0 / cell_size)) # 获取到 x 方向上取整的序号 + yi = int(round((y - space) * 1.0 / cell_size)) # 获取到 y 方向上取整的序号 + if xi >= 0 and xi < cell_num and yi >= 0 and yi < cell_num and (xi, yi,1) not in chess_arr and (xi, yi,2) not in chess_arr: + chess_arr.append((xi, yi, flag)) + # 播放落子音乐 + drop_sound.play() + # 调用函数判断下的该子是否获胜 + if check_win(chess_arr, flag): + # 若状态值为 1 则表示 3,否则表示 2 + game_state = 3 + else: + # 玩家落子后轮到电脑 + flag = 2 + computer_move() + if check_win(chess_arr, 2): + game_state = 2 + else: + flag = 1 + screen.fill((204, 153, 102)) # 将界面设置棕色 + # 遍历横位置点,步长为 40 + for x in range(0, cell_size * cell_num, cell_size): + pygame.draw.line(screen, (200, 200, 200), (x + space, 0 + space), + (x + space, cell_size * (cell_num - 1) + space), 1) + # 遍历竖位置点,步长为 40 + for y in range(0, cell_size * cell_num, cell_size): + pygame.draw.line(screen, (200, 200, 200), (0 + space, y + space), + (cell_size * (cell_num - 1) + space, y + space), 1) + # 绘制棋子 + for x, y, c in chess_arr: + chess_color = (30, 30, 30) if c == 1 else (225, 225, 225) + pygame.draw.circle(screen, chess_color, [x * cell_size + space, y * cell_size + space], 16, 16) + # 绘制五个点 + if (4, 4, 1) not in chess_arr and (4, 4, 2) not in chess_arr: + pygame.draw.circle(screen, (0, 0, 0), (cell_size * 4 + space, cell_size * 4 + space), 2, 1) + if (10, 10, 1) not in chess_arr and (10, 10, 2) not in chess_arr: + pygame.draw.circle(screen, (0, 0, 0), (cell_size * 10 + space, cell_size * 10 + space), 2, 1) + if (10, 4, 1) not in chess_arr and (10, 4, 2) not in chess_arr: + pygame.draw.circle(screen, (0, 0, 0), (cell_size * 10 + space, cell_size * 4 + space), 2, 1) + if (4, 10, 1) not in chess_arr and (4, 10, 2) not in chess_arr: + pygame.draw.circle(screen, (0, 0, 0), (cell_size * 4 + space, cell_size * 10 + space), 2, 1) + if (7, 7, 1) not in chess_arr and (7, 7, 2) not in chess_arr: + pygame.draw.circle(screen, (0, 0, 0), (cell_size * 7 + space, cell_size * 7 + space), 2, 1) + if game_state!= 1: + myfont = pygame.font.Font(None, 60) + white = 210, 210, 0 + win_text = "%s win" % ('black' if game_state == 3 else 'white') + textImage = myfont.render(win_text, True, white) + screen.blit(textImage, (260, 320)) + pygame.display.update() # 必须调用 update 才能看到绘图显示