How to write the code of tic-tac-toe chess game implemented by python
Python to achieve tic-tac-toe chess game code how to write, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Introduction to tic-tac-toe chess
Tic-tac-toe chess is also called Gobang, which is called Tic Tac Toe in English. The specific way to play is that on a 3x3 chessboard, one player uses X as a pawn and the other uses O as a pawn. He who first draws three pieces on a row, column or diagonal of the chessboard will win. If no one wins on the chessboard, it will be a draw.
For the function to be used in the example code #, you can look at def panduan1 (afield b): # to determine whether to win or not when the main program uses it. If you return 1, it proves that the chess of this color wins, x\ x\ {0,0\}: for i in range (3): # retrieve line I of the two-dimensional array If 0 for j in range (3): # retrieves all elements of line I list if a [I] [j] = b: Q chess 1 every row has the same color as the given b chess Q then add an if qscene 3 if # if all three elements in this line are the same as the color of the given chess, return 1 QR 1 if a [j] [I] = = bbureijjposition upside down To detect the three elements in each column, if each column has one of the same color as the given b chess, Q will add an if paired chess 3VR # if all three elements in this column are the same color as the given chess, return 1 return 1 qroom0 If 0 for i in range (3): # judge whether the color of the three elements of chess on the diagonal is the same as that of b chess a [I] [I] = = bvet # judge the positive diagonal qchess diagonal every piece has the same color as b Q to add an if a [I] [2muri] = = bvl # to judge another diagonal line the negative diagonal of a chess piece is the same as the color of b, w adds an if of the same color, w adds a diagonal of the same color as the chess of b, where a diagonal and a chess of b are all the same color If no value is returned for the above conditions of 1 return 1 return, run to this step Returns 0def xiaqi (all,qi) A): # play the specified qi chess (white or black) to the specified a position all [int (qi [1])-1] [int (qi [0])-1] = adef shuru1 (): # enter the position to be played and return the corresponding 9-key position list x=input () # enter the position to play suoyou='123456789' if suoyou.find (x) =-1 # limit range is 1-9 The 9-key position of the simulated mobile phone print ('input does not meet the requirements:') return 'no'# does not meet the requirements to return the position under no yard0 sum=''# storage for j in range (1 line 4): # line number, the whole cycle x times Because it is restricted by the following break statement to for z in range (1Magol 4): # column number yawning cycle one at a time Used to match whether the nine keys of the keyboard sum=str (z) +','+ str (j) # and the corresponding coordinates all [j] [z] if y==int (x): # match the same number as the input number. Starting from 1, if the match is the same, the list of positions corresponding to the 9 key is returned and the loop is interrupted. For example, enter 2 and return [2Magne1] Corresponding to the 9 keys, the first row and the second column return (sum.split (',')) # returns the location list of the corresponding 9 keys, breakdef panduan2 (all) X): # determine whether there are already pieces in this position on the chessboard hang=int (x [1]) lie=int (x [0]) if all [hang-1] [lie-1]! ='-': # you only need to determine whether the position is a'- 'sign to return 1 for return There are already chess pieces in this position. Else: return chess returns 0. This is the position where there is no chess piece def dayin (all): # print function: used to print the current state of the chessboard for i in range (3): print ('. Join (all [I])) # the main program starts all= [['-','-','-'], ['-','-','-'], ['-','-','-'], ['-','-']] # set up the chessboard The "-" sign indicates that there are no pieces on the chessboard. W=0print ('game begins!\ nBlack chess first:') while tactile chess 0 and black chess 0 and white chess 0 dayin # shows the victory of black and white chess, 1 is to win, the initial 0 is to make an overall cycle, until one party wins, then jump out of the cycle black (black:') # play black first The identifier is'1' hei=shuru1 () # enter the position to be played in black while hei=='no' or panduan2 (all,hei) = = 1 all,hei # set to re-enter the judgment command, and re-enter the judgment command when you enter a number other than 1-9 or when there is a game in this position Print the chessboard print ('Please re-enter:') hei=shuru1 () # again until you exit the loop dayin (all) # when you type it correctly # enter the 1-9 digits xiaqi (all,hei,'1') # play black t=panduan1 (all,'1') # to judge whether black wins If yes, return 1 dayin (all) # print again at this time the chessboard if tacks 1if # determine whether the hei returns 1, if so, it means hei wins and exits the loop print ('black wins!') Break all1=all [0] + all [1] + all [2] # add the chessboard to a string to determine whether there is still room to play chess if all1.count ('-') = = 0 if all1.count # if there is no'- 'sign, then the chessboard is full, draw, exit the loop dayin (all) # print the chessboard again, to look good, move this sentence to print under print (' tie!') Break print ('it's White's turn:') # the following is the same as black, which is the way of playing white. White logo is'0' bai=shuru1 () while bai=='no' or panduan2 (all,bai) = = 1: dayin (all) print ('Please re-enter:') bai=shuru1 () xiaqi (all,bai,'0') w=panduan1 (all,'0') if Whitehead 1: print ('White wins!') Dayin (all) all1=all [0] + all [1] + all [2] if all1.count ('-') = 0: dayin (all) print ('draw!') Breakprint ('Game over!')
Here are the running results, so play with your buddies:
After reading the above, have you mastered how to write the code for python to implement tic-tac-toe chess game? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!