In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to use python to achieve Gobang". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The specific code is as follows
# make a chessboard "" + + "" def createBoard (): board = [] for i in range (15): board.append (["+"] * 15) return boarddef printBoard (): # print (", end=") for i in range (1,16): number = str (I) .rjust (3) print (number) End= "") print () # Line wrap after printing the column number for i in range (15): number = str (I + 1) .ljust (3) # pay attention to this function Is to align each line number to the left, accounting for 3 digits To achieve the chessboard alignment function print (number, end= "") # print line number for j in range (15): print (Board [I] [j] End= "") print () def isGameOver (): count = 0 # first judge continuous for x in range (15): for y in range (15): if board [x] [y]! = "+": count + = 1 # right if y
< 11: flag = True #表示默认游戏结束 for c in range(y + 1, y + 5): if board[x][y] != board[x][c]: flag = False break if flag: if board[x][y] == "●": print(">> > Black wins!) else: print ("> White wins!") return True; # means the game is over # Down if x
< 11: if board[x][y] == board[x+1][y] == board[x+2][y] == board[x+3][y] == board[x+4][y]: if board[x][y] == "●": print(">> > Black wins!) else: print ("> White wins!") return True # lower right if x
< 11 and y < 11: if board[x][y] == board[x+1][y+1] == board[x+2][y+2] == board[x+3][y+3] == board[x+4][y+4]: if board[x][y] == "●": print(">> > Black wins!) else: print ("> White wins!") return True # upper right if x > 3 and y
< 11: if board[x][y] == board[x-1][y+1] == board[x-2][y+2] == board[x-3][y+3] == board[x-4][y+4]: if board[x][y] == "●": print(">> > Black wins! ") Else: print ("> White wins!") Return True # then judge the draw if count = = 15 * * 2: print ("> draw!") Return True return False# if the above even chess does not appear, continue the game def chess (c): X, y = eval (input ("Please enter coordinates:") x-= 1 y-= 1 if board [x] [y] = "+": board [x] [y] = c return True else: print ("> there are already pieces here, please play chess again!") The start of the return False# program board = createBoard () PrintBoard () player = 0while not isGameOver (): # Operation if player% 2 = = 0: print ("> Let black play chess,", end= ") if not chess (" ● "): continue else: print (", end= ") if not chess (" ○ "): continue printBoard () player + = 1print (" > game over! ")
"how to use python to achieve Gobang" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.