May-06-2025, 03:07 AM
import pygame pygame.init() #game constants WINDOW_SZ = 720 BOARD_SZ = 16 COLOR = [(255,255,255),(0,0,0)] LINE_COLOR = (0,0,0) BOARD_COLOR = (204, 154, 104) #The chessboard data structure, with a default state of 0, #where the player is 1 and the computer is -1 board = [[0 for _ in range(BOARD_SZ+1)] for _ in range(BOARD_SZ+1) ] #the image of board black = pygame.image.load("date/black.png") white = pygame.image.load("date/white.png") #“Map the player and computer onto the image, #with the player defaulting to white and the computer to black dic = { 1:white, -1:black } #set clock clock = pygame.time.Clock() #set font fps_font = pygame.font.SysFont(None,15) char_font = pygame.font.SysFont(None,25) error_font = pygame.font.SysFont(None,25) #condition judgement player_turn = True player_score = 0 computer_score = 0 game_over = False winner = None error_message = None error_start_time = 0 def transform(picture,sqare_sz): """ Scale the image to match the grid """ new_image = pygame.transform.smoothscale(picture,(sqare_sz*0.85,sqare_sz*0.85)) return new_image def draw_line(sz,sqare_sz,color,surface): """draw the grid""" for i in range(1,sz): char = char_font.render('{}'.format(i),True,color) pygame.draw.line(surface,color,(i*sqare_sz,sqare_sz),(i*sqare_sz,(sz - 1)*sqare_sz),3) pygame.draw.line(surface,color,(sqare_sz,i*sqare_sz),((sz - 1)*sqare_sz,i*sqare_sz),3) surface.blit(char,(i*sqare_sz-(char.get_width() // 2),sqare_sz-20)) surface.blit(char,(sqare_sz-20,i*sqare_sz-(char.get_height()) // 2)) return surface def low(value,col,row,sqare_sz,surface): """Put the pieces on the board""" new_picture = transform(dic[value],sqare_sz) rect = new_picture.get_rect(center = (col*sqare_sz,row*sqare_sz)) surface.blit(new_picture,rect) return surface def draw_picture(sz,sqare_sz,board,surface): """ draw the board """ for row in range(1,sz): for col in range(1,sz): value = board[row][col] if value == 1 or value == -1: low(value,col,row,sqare_sz,surface) def distance(p1,p2,radius): """Judge the distance""" import math dx = abs(p1[0]-p2[0])**2 dy = abs(p1[1]-p2[1])**2 if math.sqrt(dx + dy) <= radius: return True return False def scope_of_circle(sz,sqare_sz,picture,cur_pos): """ Circular Region of Interest""" for row in range(1,sz): for col in range(1,sz): circle_center = (col*sqare_sz,row*sqare_sz) radius = sqare_sz // 2 if distance(circle_center,cur_pos,radius): return (row,col) def move_of_computer(sz,board): """the movement of computer in each turn""" import random empty = [] for row in range(1,sz): for col in range(1,sz): if board[row][col] == 0: empty.append((row,col)) result = random.choice(empty) return result def get_point(): """ Determine the player’s/computer's scoring conditions""" pass def main(): """ main floo in game """ global game_over,player_turn,winner,player_score,computer_score,error_message,error_start_time #Initialize window CHANCE = 0 sqare_sz = WINDOW_SZ // BOARD_SZ adjusted_sz = sqare_sz * BOARD_SZ window = pygame.display.set_mode((adjusted_sz,adjusted_sz)) pygame.display.set_caption('Gomoku game') running = True while running: clock.tick(60) fps_text = fps_font.render('Current: {:.2f} FPS'.format(clock.get_fps()), True,(0,0,0) ) rate_text = fps_font.render('RunTime: {:.2f} s'.format(pygame.time.get_ticks()//1000), True,(0,0,0) ) window.fill(BOARD_COLOR) window.blit(fps_text,(10,10)) window.blit(rate_text,(120,10)) draw_line(BOARD_SZ,sqare_sz,LINE_COLOR,window) draw_picture(BOARD_SZ,sqare_sz,board,window) #Error message for incorrect move if error_message: now = pygame.time.get_ticks() if now - error_start_time < 3000: error = error_font.render(error_message, True, (255,0,0)) rect = error.get_rect(center=(adjusted_sz // 2, adjusted_sz // 2)) window.fill((125,125,125),(160,310,400,100)) window.blit(error, rect) pygame.display.flip() else: error_message = None #Automatically clears after three seconds for event in pygame.event.get(): #if event.type != pygame.NOEVENT: #print(event) if event.type == pygame.QUIT: running = False break if not game_over and event.type==pygame.MOUSEBUTTONDOWN and player_turn: #player's turn (x,y) = pygame.mouse.get_pos() result = scope_of_circle(BOARD_SZ,sqare_sz,black,(x,y)) if result: (row,col) = result if board[row][col] == 0: board[row][col] = 1 #the player’s score exceeds the computer’s score by 20 points, #game is immediately returned if player_score - computer_score > 20: winner = 'PLAYER!' game_over = True #elif get_points(): #pass CHANCE = 0 else: CHANCE += 1 if CHANCE % 3 != 0: error_message = 'here is already a piece here, please try again.' error_start_time = pygame.time.get_ticks() continue CHANCE = 0 error_message = 'You have wasted three opportunities.Turn the computer' error_start_time = pygame.time.get_ticks() player_turn = False if not game_over and not player_turn: #trun of computer (x,y) = move_of_computer(BOARD_SZ,board) board[x][y] = -1 player_turn = True pass if game_over: pass pygame.display.update() pygame.quit() pass if __name__ == '__main__': main()Hello, above is the Gomoku game code I wrote. I've encountered a problem that's difficult for me to solve, which is how to determine whether the player or the computer scores. According to the game rules, when five pieces are in the same column, the same row, or the same diagonal, the player (or computer) scores. However, my data structure is a two-dimensional matrix, and the probability of scoring seems to be related to the position of the pieces. I don't know how to implement it in a programming language, and I'm completely at a loss.
If you are willing to help me, I would be extremely grateful.
Attached Files