In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you the relevant knowledge of how to achieve Gobang game in python. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
I. basic process
The implementation logic of the Gobang game is as follows:
1. Create and initialize 3x3 chessboard
2. If the player holds the U son, he will fall first.
3. Win or lose [win, lose, draw]. If the result is not divided, it will continue as follows.
4. The computer holds the T child and carries on the falling child.
5. To decide whether to win or lose. If the result is not divided, proceed from step 2.
Second, basic steps 1. Menu interface
Option 1 is to start the game, and option 2 is to quit the game
Def menu (): print ('- * 20) print ('1murbegin`) print (' 2murexit`) print ('please select begin or exit') print (' -'* 20) while (1): select = input ('please input:') if select =' 1mm: Begin_games () pass elif select = = '2clients: print (' exit the game') break # pass pass2, Initialize chessboard, print chessboard
The backgammon chessboard is a 3-to-3 square, which is stored as a list in python.
Chess_board = [[0,0,0], [0,0,0], [0,0,0]]
So how can this storage list be printed out and become a chessboard?
Def init_cheaa_board (chess_board): # initialize the list first for i in range (MAX_ROW): for j in range (MAX_COL): chess_ chess_ [I] [j] =''passdef print_chess_board (chess_board): # Checkboard print print (' *'+'-'* 7') *') for i in range (MAX_ROW): print ('|'+'* 3roomchess_ board[ I] [0] +'* 3room` |'+'* 3roomchessboard` [I] +'* 3Secretchessboardboard`] [2] +'* 3room` |') print ('*'+'-'* 7 +'*'+'-'*) 7 +'* +'-'* 7 +'*) pass pass
3. Players fall.
The player selects the horizontal and vertical coordinates of the falling son in the 3: 3 chessboard. The coordinate point needs to be satisfied: 1, the point is in the chessboard; 2, the point has not yet been set.
Def player_first (chess_board): while (1): X = int (input ('please input XRV')) y = int (input ('please input YGV')) if (chess_ Board [x] [y]! =''): # reselect the coordinate print ('This position is already occupied coordinates') if it has been set Pass elif (x > = MAX_ROW or y > = MAX_COL or x
< 0 or y < 0): #所选坐标超出棋盘范围,重新选择坐标 print('This position is beyond the chessboard!') pass else: #若坐标可以落子,则将该坐标置为玩家的棋子U chess_board[x][y] = 'U' print_chess_board(chess_board) #return x,y break pass pass4、电脑落子 电脑落子算法: 4.1、先检查一下棋盘,看电脑已占有棋面中是否已经有两子连成、即将成棋的状态。若已有,则获取可以促成胜利的坐标点,进行落子T; 4.2、若4.1不满足,则再去检查一下棋盘,看玩家已占有棋面中是否已经有两子连成、即将成棋的状态。若已有,则获取玩家即将胜利的坐标点,落子T进行拦截; 4.3、若4.1、4.2均不满足,则在棋面中选择电脑端有利的点进行落子; A、先判断中心位置[1][1]处是否被占领,若未被占领,则这是最有利点。当占领[1][1]点时,则阻断了玩家的横、纵、正对角线、副对角线四条线路; B、次有利点则是3*3棋盘的四个角,每占领一个角,则会阻断玩家的三条线路; C、最后有利的点则是每条边的中心位置,会阻断玩家的两条线路; def Intercept_player(chess_board,key): count2 = 0 index2 = [] intercept_index = {'x':-1,'y':-1} for i in range(MAX_ROW): index = [] count = 0 count1 = 0 index1 = [] allindex = [0,1,2] for j in range(MAX_ROW): if(chess_board[i][j] == key): #每一行的玩家落子情况 count += 1 index.append(j) if(chess_board[j][i] == key): #每一列的玩家落子情况 #print('j'+str(j)+',i'+str(i)+'='+chess_board[j][i]) count1 += 1 index1.append(j) if (i == j and chess_board[j][i] == key): # 在主对角线中的玩家落子情况 count2 += 1 index2.append(j) if(count == 2): #在每一行中 获取具体的可以拦截的位置坐标 需要排除掉已经填充的位置 result = list(set(allindex).difference(set(index))) result = result[0] if(chess_board[i][result] == ' '): #当这个位置可以进行拦截时,进行坐标返回 #return i,result intercept_index['x'] = i intercept_index['y'] = result return intercept_index #print(count1,'------->', index1) if (count1==2): # to get specific interceptable location coordinates in each column, you need to exclude the filled position result = list (set (allindex) .difference (set (index1)) result = result [0] # print (' count1==2,result:') Result) if (chess_ Board [result] [I] = ='): # when this location can be intercepted Coordinate return intercept_index ['x'] = result intercept_index ['y'] = I return intercept_index # return I Result if (count2 = = 2): # to get specific interceptable location coordinates on the main diagonal, you need to exclude the filled position result = list (set (allindex) .difference (set (index2) result = result [0] if (chess_ Board [I] [result] = =''): # when this position can be intercepted Coordinate return intercept_index ['x'] = I intercept_index ['y'] = result return intercept_index # return I Result count3 = 0 if (chess_board [0] [2] = = key): count3 + = 1 if (chess_board [1] [1] = = key): count3 + = 1 if (chess_board [2] [0] = = key): count3 + = 1 if (count3 = = 2): if (chess_board [0] [2] = ='): intercept_index ['x'] = 0 Intercept_index ['y'] = 2 elif (chess_board [1] [1] = =''): intercept_index ['x'] = 1 intercept_index ['y'] = 1 elif (chess_board [2] [0] = =''): intercept_index ['x'] = 2 intercept_index ['y'] = 0 return intercept_ Index def computer_second (chess_board): # computer intelligent chess # 1. First check the computer to see if there are two pieces of chess. Then get the empty position coordinates of your own chess intercept_index = Intercept_player (chess_board 'T') if (intercept_index ['x'] =-1 and intercept_index ['y'] =-1): pass else: # computer drop x = intercept_index ['x'] y = intercept_index ['y'] chess_ Board [x] [y] ='T' return # 2, If the player becomes chess quickly, he will first intercept intercept_index = Intercept_player (chess_board). 'U') # if the player has two pieces of chess, get the coordinates of the empty position # print (' intercept_index---:') # print (intercept_index) if (intercept_index ['x'] =-1 and intercept_index ['y'] =-1): pass else: # computer accessible x = intercept_index ['x'] y = intercept_index ['y '] chess_ Board [x] [y] =' T 'return # 3, If not, Then the computer side arranges chess to promote the completion of chess # 3.1. occupy the central position if the central position is not occupied if (chess_board [1] [1] = =''): chess_board [1] [1] ='T 'return # 3.2, Occupy the corner position if [0 0] [0mem2] [2Power0] [2Power2] if (chess_board [0] [0] = =''): chess_board [0] [0] ='T 'return if (chess_board [0] [2] = =''): chess_board [0] [2] = 'T'return if (chess_board [2] [0) ] ='): chess_board [2] [0] ='T 'return if (chess_board [2] [2] = =''): chess_board [2] [2] ='T 'return # 3.3, Occupy the center position of each side if [0rect 1] [1] [1] [2] [2] unoccupied if (chess_board [0] [1] = =''): chess_board [0] [1] ='T 'return if (chess_board [1] [0] = =''): chess_board [1] [0] ='T 'return if (chess_board [1) ] [2] = =''): chess_board [1] [2] ='T 'return if (chess_board [2] [1] = =''): chess_board [2] [1] ='T 'return5, Win or lose decision
The final result: lose, win, draw D
Decision process: judge whether there is a player U or computer T connected into three on each horizontal, vertical and diagonal line, and if so, that side wins; when the whole chess surface is occupied, but the player and the computer are not into chess, it means a draw.
Def chess_board_isfull (chess_board): # determine whether the chessboard is filled with for i in range (MAX_ROW): if ('in chess_ in chess_ [I]): return 0 return 1 pass def Win_or_lose (chess_board): isfull = chess_board_isfull (chess_board) for i in range (MAX_ROW): # judgement if ( Chess_board [0] [I] = = chess_board [1] [I] = = chess_board [2] [I]): return chess_board [0] [I] pass pass for i in range (MAX_ROW): # judgment if for each line (chess_ Board [I] [0] = = chess_ Board [I] [1] = chess_ Board [I] [2]): Return chess_ Board [I] [0] pass pass if (chess_board [0] [0] = = chess_board [1] [1] = chess_board [2] [2]): # judge the positive diagonal of the chessboard return chess_board [0] [0] if (chess_board [0] [2] = = chess_board [1] [1] = chess_board [2] [0]): # judgment Broken chessboard against corners return chess_board [0] [2] if isfull: return 'D' # after the above judgment They are not satisfied (neither win nor lose), but the chessboard is full. Then explain the draw else: return''3, the overall code # coding=utf-8import randomMAX_ROW = 3MAX_COL = 3#array = [0,0,0,0] chess_board = [0,0,0], [0,0,0], [0,0] 0]] # [array] * 3def init_cheaa_board (chess_board): for i in range (MAX_ROW): for j in range (MAX_COL): chess_ for [I] [j] =''passdef print_chess_board (chess_board): print (' *'+'-'* 7'). In range (MAX_ROW): print ('|'+'* 3roomchess_ board[ I] [0] +'* 3Secretchessboard``) print ('*'+'-'* 7 +'*'-'* 7 +'*'') -'* 7 +'*') pass passdef player_first (chess_board): while (1): X = int (input ('please input XRV')) y = int (input ('please input YRV')) if (chess_ Board [x] [y]! =''): print ('This position is already occupied premises') Pass elif (x > = MAX_ROW or y > = MAX_COL or x
< 0 or y < 0): print('This position is beyond the chessboard!') pass else: chess_board[x][y] = 'U' print_chess_board(chess_board) #return x,y break pass passdef chess_board_isfull(chess_board): #判断棋盘是否填充满 for i in range(MAX_ROW): if (' ' in chess_board[i]): return 0 return 1 passdef Win_or_lose(chess_board): isfull = chess_board_isfull(chess_board) for i in range(MAX_ROW): #每一列的判断 if( chess_board[0][i] == chess_board[1][i] == chess_board[2][i]): return chess_board[0][i] pass pass for i in range(MAX_ROW): # 每一行的判断 if( chess_board[i][0] == chess_board[i][1] == chess_board[i][2]): return chess_board[i][0] pass pass if (chess_board[0][0] == chess_board[1][1] == chess_board[2][2]): # 判断棋盘正对角线 return chess_board[0][0] if (chess_board[0][2] == chess_board[1][1] == chess_board[2][0]): # 判断棋盘反对角线 return chess_board[0][2] if isfull: return 'D' # 经过以上的判断,都不满足(既没赢也没输),但是棋盘也已经填充满,则说明和棋 else: return ' 'def computer_second_random(chess_board): #电脑随机出棋 while(1): x = random.randint(0,2) y = random.randint(0,2) if(chess_board[x][y] != ' '): continue else: chess_board[x][y] = 'T' breakdef Intercept_player(chess_board,key): count2 = 0 index2 = [] intercept_index = {'x':-1,'y':-1} for i in range(MAX_ROW): index = [] count = 0 count1 = 0 index1 = [] allindex = [0,1,2] for j in range(MAX_ROW): if(chess_board[i][j] == key): #每一行的玩家落子情况 count += 1 index.append(j) if(chess_board[j][i] == key): #每一列的玩家落子情况 #print('j'+str(j)+',i'+str(i)+'='+chess_board[j][i]) count1 += 1 index1.append(j) if (i == j and chess_board[j][i] == key): # 在主对角线中的玩家落子情况 count2 += 1 index2.append(j) if(count == 2): #在每一行中 获取具体的可以拦截的位置坐标 需要排除掉已经填充的位置 result = list(set(allindex).difference(set(index))) result = result[0] if(chess_board[i][result] == ' '): #当这个位置可以进行拦截时,进行坐标返回 #return i,result intercept_index['x'] = i intercept_index['y'] = result return intercept_index #print(count1,'------->', index1) if (count1==2): # to get specific interceptable location coordinates in each column, you need to exclude the filled position result = list (set (allindex) .difference (set (index1)) result = result [0] # print (' count1==2,result:') Result) if (chess_ Board [result] [I] = ='): # when this location can be intercepted Coordinate return intercept_index ['x'] = result intercept_index ['y'] = I return intercept_index # return I Result if (count2 = = 2): # to get specific interceptable location coordinates on the main diagonal, you need to exclude the filled position result = list (set (allindex) .difference (set (index2) result = result [0] if (chess_ Board [I] [result] = =''): # when this position can be intercepted Coordinate return intercept_index ['x'] = I intercept_index ['y'] = result return intercept_index # return I Result count3 = 0 if (chess_board [0] [2] = = key): count3 + = 1 if (chess_board [1] [1] = = key): count3 + = 1 if (chess_board [2] [0] = = key): count3 + = 1 if (count3 = = 2): if (chess_board [0] [2] = ='): intercept_index ['x'] = 0 Intercept_index ['y'] = 2 elif (chess_board [1] [1] = =''): intercept_index ['x'] = 1 intercept_index ['y'] = 1 elif (chess_board [2] [0] = =''): intercept_index ['x'] = 2 intercept_index ['y'] = 0 return intercept_ Indexdef computer_second (chess_board): # computer intelligent chess # 1. First check the computer to see if there are two pieces of chess. Then get the empty position coordinates of your own chess intercept_index = Intercept_player (chess_board 'T') if (intercept_index ['x'] =-1 and intercept_index ['y'] =-1): pass else: # computer drop x = intercept_index ['x'] y = intercept_index ['y'] chess_ Board [x] [y] ='T' return # 2, If the player becomes chess quickly, he will first intercept intercept_index = Intercept_player (chess_board). 'U') # if the player has two pieces of chess, get the coordinates of the empty position # print (' intercept_index---:') # print (intercept_index) if (intercept_index ['x'] =-1 and intercept_index ['y'] =-1): pass else: # computer accessible x = intercept_index ['x'] y = intercept_index ['y '] chess_ Board [x] [y] =' T 'return # 3, If not, Then the computer side arranges chess to promote the completion of chess # 3.1. occupy the central position if the central position is not occupied if (chess_board [1] [1] = =''): chess_board [1] [1] ='T 'return # 3.2, Occupy the corner position if [0 0] [0mem2] [2Power0] [2Power2] if (chess_board [0] [0] = =''): chess_board [0] [0] ='T 'return if (chess_board [0] [2] = =''): chess_board [0] [2] = 'T'return if (chess_board [2] [0) ] ='): chess_board [2] [0] ='T 'return if (chess_board [2] [2] = =''): chess_board [2] [2] ='T 'return # 3.3, Occupy the center position of each side if [0rect 1] [1] [1] [2] [2] unoccupied if (chess_board [0] [1] = =''): chess_board [0] [1] ='T 'return if (chess_board [1] [0] = =''): chess_board [1] [0] ='T 'return if (chess_board [1) ] [2] = =''): chess_board [1] [2] ='T 'return if (chess_board [2] [1] = =''): chess_board [2] [1] ='T 'returndef begin_games (): global chess_board init_cheaa_board (chess_board) result =' 'while (1): print_chess_board (chess) _ board) player_first (chess_board) result = Win_or_lose (chess_board) if (result! =''): break else: # the chessboard is not full yet The computer plays chess # computer_second_random (chess_board) computer_second (chess_board) result = Win_or_lose (chess_board) if (result! =''): break print_chess_board (chess_board) if (result = ='U'): print ('Congratulations on your victorylike') Elif (result = ='T'): print ('Unfortunately, you failed to beat the computer.') Elif (result = ='D'): print ('The two sides broke even.') def menu (): print (' -'* 20) print ('1murbegin') print ('2murex') print ('please select begin or exit') print (' -'* 20) while (1): Select = input ('please input:') if select = =' 1times: begin_games () pass elif select = = '2miles: print (' exit the game') break # pass passif _ name__ = = "_ _ main__": menu () pass 4, Result display
4.1 in the screenshot below, the process of computer interception, occupying a favorable position, and taking the lead in chess is shown.
These are all the contents of the article "how to play Gobang in python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.