Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Gobang Game with python Code

Shulou Source: shulou.com Published: 2022-05-31 18:31:27 10月01日 Update

This article mainly introduces "how to use python code to achieve Gobang game" related knowledge, the editor through the actual case to show you the operation process, the operation method is simple and fast, practical, I hope that this "how to use python code to achieve Gobang game" article can help you solve the problem.

Put the code first.

# call the pygame library import pygameimport sys# call the commonly used keyword constant from pygame.locals import QUIT,KEYDOWNimport numpy as np# to initialize pygamepygame.init () # to get access to the display system, and create a window screen# window size 670x670screen = pygame.display.set_mode ((670670)) screen_color= [238jue 154je 73] # set canvas color, [238jue 154jue 73] corresponds to brownish yellow line_color = [0J 0c0] # set line color Corresponding to black def check_win (over_pos): # judge Wuzi Lianxin mp=np.zeros ([15recover15]) Dtype=int) for val in over_pos: x=int ((val [0] [0]-27) / 44) y=int ((val [0] [1]-27) / 44) if val [1] = white_color: mp [x] [y] = white else: mp [x] [y] = sunspot for i in range (15): Pos1= [] pos2= [] for j in range (15): if mp [I] [j] = = 1: pos1.append ([I) J]) else: pos1= [] if mp [I] [j] = 2: pos2.append ([iMagnej]) else: pos2= [] if len (pos1) > = 5purl # Wuzi Lianxin return [1m POS 1] if len (pos2) > = 5: return [2 Pos2] for j in range (15): pos1= [] pos2= [] for i in range (15): if mp [I] [j] = = 1: pos1.append ([iMae j]) else: pos1= [] if mp [I] [j] = 2: pos2.append ([I] J]) else: pos2= [] if len (pos1) > = 5: return [1mempos1] if len (pos2) > = 5: return [2 Pos2] for i in range (15): for j in range (15): pos1= [] pos2= [] for k in range (15): if or > = 15: break if mp [iExk] [junik] = = 1: pos1.append ) else: pos1= [] if mp [iExk] [junik] = = 2: pos2.append ([ionomkMagazine]) else: pos2= [] if len (pos1) > = 5: return [1 Pos1] if len (pos2) > = 5: return [2 or j-k=5 pos2] for i in range (15): for j in range (15): pos1= [] pos2= [] for k in range (15): if iTunk > = 15 or j-k=5: return [1 Pos1] if len (pos2) > = 5: return [2mempos2] return [0, [] def find_pos (x Y): # find the location shown where you can fall for i in range (27 and x=R1 and y=L1 and x=R1 and y=L1 and x=R1 and y=L1 and x=R1 and 670 Magazine 44): for j in range (27 record670 Magazine 44): L1=i-22 L2=i+22 R1=j-22 R2=j+22 if x > = L1 and x=R1 and y=L1 and x=R1 and y=L1 and x=R1 and y=L1 and x=R1 and yellow5 L1=i-22 L2=i+22 R1=j-22 R2=j+22 if # five sons linked to the heart return [1 Pos1] if len (pos2) > = 5: return [2 pos1.append 2] for j in range (15): pos1= [] pos2= [] for i in range (15): if mp [I] [j] = 1: pos1.append ([I] J]) else: pos1= [] if mp [I] [j] = 2: pos2.append ([iMagnej]) else: pos2= [] if len (pos1) > = 5: return [1 if len] if len (pos2) > = 5: return [2 Pos2] for i in range (15): for j in range (15): pos1= [] pos2= [] for k in range (15): if or > = 15: break if mp [iExk] [junik] = = 1: pos1.append ) else: pos1= [] if mp [iExk] [junik] = = 2: pos2.append ([ionomkMagazine]) else: pos2= [] if len (pos1) > = 5: return [1 Pos1] if len (pos2) > = 5: return [2 or j-k=5 pos2] for i in range (15): for j in range (15): pos1= [] pos2= [] for k in range (15): if iTunk > = 15 or j-k=5: return [1 Pos1] if len (pos2) > = 5: return [2mempos2] return [0, []]

If the run is not successful, download pip install pygame,pip install numpy on pycharm

My version is pycharm3.7.

On "how to use python code to achieve Gobang game" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Position mouse color information five pieces code chessboard canvas size edges lines coordinates chess pieces heart-to-heart Gobang continuous events keywords keywords commonly used Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft Shulou Information Huawei Docker