Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code of python stand-alone Gobang

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to write the code about python stand-alone Gobang, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

I believe everyone has played Gobang, so have you ever tried to write Gobang yourself? Today, I will take you to realize the following Gobang.

Def initChessSquare (XQuery y): # initialize the chessboard for i in range (15): # intersection coordinates of each row rowlist = [] for j in range (15): # intersection coordinates of each column pointX = x + juni40 pointY = y + iTun40 sp = StornPoint (pointX,pointY) 0) rowlist.append (sp) initChessList.append (rowlist)

Create the initialization chessboard method initChessSquare (xrey): traverse all the intersection coordinates according to the number of crossing points of the chessboard picture.

Def eventHander (): # listen for various events for event in pygame.event.get (): global initRole if event.type = = QUIT:# event type pygame.quit () sys.exit () if event.type = = MOUSEBUTTONDOWN: # when the mouse is clicked, x Y = pygame.mouse.get_pos () # get the location coordinates of the mouse click item0juni0for temp in initChessList: for point in temp: if x > = point.x-10 and x=point.y-10 and y = 0 and x + 4

< 15 : if initChessList[i][x].value == value and \ initChessList[i][x + 1].value == value and \ initChessList[i][x + 2].value == value and \ initChessList[i][x + 3].value == value and \ initChessList[i][x + 4].value == value : flag = True break pass for x in range(i - 4, i + 5): # 纵向有没有出现5连(在边缘依次逐一遍历,是否五个棋子的类型一样) if x >

= 0 and x + 4

< 15: if initChessList[x][j].value == value and \ initChessList[x + 1][j].value == value and \ initChessList[x + 2][j].value == value and \ initChessList[x + 3][j].value == value and \ initChessList[x + 4][j].value == value: flag = True break pass # 先判断东北方向的对角下输赢 x 列轴, y是行轴 , i 是行 j 是列(右斜向)(在边缘依次逐一遍历,是否五个棋子的类型一样) for x, y in zip(range(j + 4, j - 5, -1), range(i - 4, i + 5)): if x >

= 0 and x + 4

< 15 and y + 4 >

= 0 and y

< 15: if initChessList[y][x].value == value and \ initChessList[y - 1][x + 1].value == value and \ initChessList[y - 2][x + 2].value == value and \ initChessList[y - 3][x + 3].value == value and \ initChessList[y - 4][x + 4].value == value: flag = True # 2、判断西北方向的对角下输赢 x 列轴, y是行轴 , i 是行 j 是列(左斜向)(在边缘依次逐一遍历,是否五个棋子的类型一样) for x, y in zip(range(j - 4, j + 5), range(i - 4, i + 5)): if x >

= 0 and x + 4

< 15 and y >

= 0 and y + 4 < 15: if initChessList [y] [x]. Value = = value and\ initChessList [y + 1] [x + 1] .value = = value and\ initChessList [y + 2] [x + 2] .value = = value and\ initChessList [y + 3] [x + 3] .value = = value and\ InitChessList [y + 4] [x + 4] .value = = value: flag = True if flag: # if the condition holds Prove that Wuzi Lianzhu resultFlag = value # obtains the established chess piece color print ("White wins" if value = = 1 else "Black wins")

The main idea of the above code is to judge whether to achieve Gobang from horizontal, vertical and oblique aspects, and to judge whether to win or lose.

Total

Is not too difficult, the overall amount of code is a little bit long.

On the python stand-alone Gobang code how to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report