Update 五子棋代码.py

master
hnu202109060119 2 years ago
parent 71ccc5d770
commit ab63b6a149

@ -5,7 +5,6 @@ Created on Fri Dec 9 18:27:50 2022
@author: lsy @author: lsy
""" """
import pygame import pygame
pygame.init() pygame.init()
screen = pygame.display.set_mode((600, 600)) screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption('五子棋-EduCoder') pygame.display.set_caption('五子棋-EduCoder')
@ -14,7 +13,6 @@ cell_size = 40
cell_num = 15 cell_num = 15
chess_arr = [] chess_arr = []
def get_one_dire_num(lx, ly, dx, dy, m): def get_one_dire_num(lx, ly, dx, dy, m):
tx = lx tx = lx
ty = ly ty = ly
s = 0 s = 0
@ -22,7 +20,6 @@ def get_one_dire_num(lx, ly, dx, dy, m):
tx += dx tx += dx
ty += dy ty += dy
if tx < 0 or tx >= cell_num or ty < 0 or ty >= cell_num or m[ty][tx] == 0: return s if tx < 0 or tx >= cell_num or ty < 0 or ty >= cell_num or m[ty][tx] == 0: return s
s+=1 s+=1
def check_win(chess_arr, flag): def check_win(chess_arr, flag):
m = [[0] * cell_num for i in range(cell_num)] m = [[0] * cell_num for i in range(cell_num)]
@ -34,13 +31,10 @@ def check_win(chess_arr, flag):
dire_arr = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, -1), (1, 1)], dire_arr = [[(-1, 0), (1, 0)], [(0, -1), (0, 1)], [(-1, -1), (1, 1)],
[(-1, 1), (1, -1)]] [(-1, 1), (1, -1)]]
for dire1, dire2 in dire_arr: for dire1, dire2 in dire_arr:
dx, dy = dire1 dx, dy = dire1
num1 = get_one_dire_num(lx, ly, dx, dy, m) num1 = get_one_dire_num(lx, ly, dx, dy, m)
dx, dy = dire2 dx, dy = dire2
num2 = get_one_dire_num(lx, ly, dx, dy, m) num2 = get_one_dire_num(lx, ly, dx, dy, m)
if num1 + num2 + 1 >= 5: return True if num1 + num2 + 1 >= 5: return True
return False return False
flag = 1 flag = 1
@ -56,27 +50,20 @@ while True:
yi = int(round((y - space) * 1.0 / cell_size)) yi = int(round((y - space) * 1.0 / cell_size))
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: 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)) chess_arr.append((xi, yi,flag))
if check_win(chess_arr, flag): if check_win(chess_arr, flag):
game_state = 2 if flag == 1 else 3 game_state = 2 if flag == 1 else 3
else: else:
flag = 2 if flag == 1 else 1 flag = 2 if flag == 1 else 1
screen.fill((204, 153, 102)) screen.fill((204, 153, 102))
for x in range(0, cell_size * cell_num, cell_size): for x in range(0, cell_size * cell_num, cell_size):
pygame.draw.line(screen, (200, 200, 200), (x + space, 0 + space), pygame.draw.line(screen, (200, 200, 200), (x + space, 0 + space),
(x + space, cell_size * (cell_num - 1) + space), 1) (x + space, cell_size * (cell_num - 1) + space), 1)
for y in range(0, cell_size * cell_num, cell_size): for y in range(0, cell_size * cell_num, cell_size):
pygame.draw.line(screen, (200, 200, 200), (0 + space, y + space), pygame.draw.line(screen, (200, 200, 200), (0 + space, y + space),
(cell_size * (cell_num - 1) + space, y + space), 1) (cell_size * (cell_num - 1) + space, y + space), 1)
for x, y,c in chess_arr: for x, y,c in chess_arr:
chess_color = (30, 30, 30) if c == 1 else (225, 225, 225) 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) 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: 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) 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: if (10, 10, 1) not in chess_arr and (10, 10, 2) not in chess_arr:

Loading…
Cancel
Save