The python implementation code for three pieces of chess, for your reference, is as follows
I. Basic process
The logic for the implementation of the three-child game is as follows:
1. Create an initialized 3*3 board;
2. The player holds the U-token and makes the first drop;
3、Winner and loser judgment [win, lose, draw], if the winner and loser are not divided, then continue as follows
4. The computer holds the T-sub and makes a drop;
5. Determination of winners and losers, if the winners and losers have not been divided, then continue from step 2 to carry out
II. Basic steps
1、Menu interface
Choice 1 is to start the game, choice 2 is to exit the game
def menu(): print('-'*20) print('1---------------begin') print('2---------------exit') print('please select begin or exit') print('-' * 20) while(1): select = input('please input:') if select == '1': begin_games() pass elif select == '2': print('exit the game') break #pass pass
2、Initialize the board, print the board
The triton board is a 3*3 square, which is stored in python using lists.
chess_board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
So how do you print out this stored list as a checkerboard?
def init_cheaa_board(chess_board): # Initialize the list first for i in range(MAX_ROW): for j in range(MAX_COL): chess_board[i][j] = ' ' pass def print_chess_board(chess_board): #Chessboard Printing print('*'+'-'*7+'*'+'-'*7+'*'+'-'*7+'*') for i in range(MAX_ROW): print('|'+' '*3+chess_board[i][0]+' '*3+'|'+' '*3+chess_board[i][1]+' '*3+'|'+' '*3+chess_board[i][2]+' '*3+'|') print('*' + '-' * 7 + '*' + '-' * 7 + '*' + '-' * 7 + '*') pass pass
3、Player drop
The player chooses the horizontal and vertical coordinates of the point on the 3*3 board. The coordinates need to satisfy: 1) the point is within the board; 2) the point has not yet been placed.
def player_first(chess_board): while(1): x = int(input('please input x:')) y = int(input('please input y:')) if(chess_board[x][y] != ' '): # Re-select the coordinates if they have been subselected print('This position is already occupied!') pass elif(x >= MAX_ROW or y >= MAX_COL or x < 0 or y < 0): #Selected coordinates are out of board range, re-select coordinates print('This position is beyond the chessboard!') pass else: # If a coordinate is available for discs, the coordinate is set to the player's disc U chess_board[x][y] = 'U' print_chess_board(chess_board) #return x,y break pass pass
4、Computer drop
Computerized drop algorithm:
4.1 First, check the board to see if the computer has already taken possession of the board with two discs connected and about to form a game. If so, obtain the coordinates that can lead to a victory, and make a move to T;
4.2. If 4.1 is not satisfied, check the board again to see if the player has already occupied the board in a state where two discs are already connected and about to be formed. If there is, then obtain the coordinates of the point where the player is about to win, and drop T to intercept;
4.3. If neither 4.1 nor 4.2 is satisfied, a favorable point on the board is chosen for the computer to make a move;
A. First determine whether the center position at [1][1] is occupied; if it is not occupied, then this is the most favorable point. When point [1][1] is occupied, the player's four routes, horizontal, vertical, positive diagonal, and negative diagonal, are blocked;
B. The secondary vantage points are the four corners of the 3*3 board, and each occupied corner blocks three of the player's lines;
C. The final vantage point is then the center of each side, which will block both of the player's lines;
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): # of players in each row who have made a move count += 1 (j) if(chess_board[j][i] == key): # of players in each column who have made a move #print('j'+str(j)+',i'+str(i)+'='+chess_board[j][i]) count1 += 1 (j) if (i == j and chess_board[j][i] == key): # Players falling in the main diagonal count2 += 1 (j) if(count == 2): # In each line, get the coordinates of specific locations that can be intercepted, excluding those that are already populated. result = list(set(allindex).difference(set(index))) result = result[0] if(chess_board[i][result] == ' '): # When this location is available for interception, make a coordinate return #return i,result intercept_index['x'] = i intercept_index['y'] = result return intercept_index #print(count1,'------->',index1) if (count1 == 2): # In each column, get the coordinates of specific locations that can be intercepted, excluding those that are already populated. result = list(set(allindex).difference(set(index1))) result = result[0] #print('count1==2,result:',result) if (chess_board[result][i] == ' '): # Coordinate return when this location is available for interception intercept_index['x'] = result intercept_index['y'] = i return intercept_index #return i, result if (count2 == 2): # On the main diagonal Get the coordinates of specific locations that can be intercepted Need to exclude already filled locations result = list(set(allindex).difference(set(index2))) result = result[0] if (chess_board[i][result] == ' '): # Coordinate return when this location is available for interception 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): # The computer is smart enough to play chess #First, check if the computer has two pawns. If it does, get the coordinates of the empty squares and make a move yourself. intercept_index = Intercept_player(chess_board, 'T') if (intercept_index['x'] == -1 and intercept_index['y'] == -1): pass else: # The computer can drop the ball x = intercept_index['x'] y = intercept_index['y'] chess_board[x][y] = 'T' return #2. If a player is about to make a move, intercept first. intercept_index = Intercept_player(chess_board,'U') #If the player has two pieces, get the coordinates of the empty square. #print('intercept_index---:') #print(intercept_index) if(intercept_index['x'] == -1 and intercept_index['y'] == -1): pass else: #Computerscanfall x = intercept_index['x'] y = intercept_index['y'] chess_board[x][y] = 'T' return #3. If not, the computer lines up the pieces to facilitate the formation of the game. #3.1, Occupy Center Position If Center Position [1,1] is not occupied if(chess_board[1][1] == ' '): chess_board[1][1] = 'T' return #3.2. Occupy the four corners if [0,0] [0,2] [2,0] [2,2] is not occupied. 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 of each side if [0,1] [1,0] [1,2] [2,1] is not occupied. 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' return
5. Determination of winners and losers
End result: lose, win, draw D
Judgment process: Judge whether there is a player U or computer T on each horizontal line, vertical line, or diagonal line that connects to three discs; if there is, then that side wins; when the entire board is occupied, but neither the player nor the computer has made a move, then it means a draw.
def chess_board_isfull(chess_board): # Determine if the board is full for i in range(MAX_ROW): if (' ' in chess_board[i]): return 0 return 1 pass def Win_or_lose(chess_board): isfull = chess_board_isfull(chess_board) for i in range(MAX_ROW): # Each column's judgment 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): # Each line of judgment 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]): # Judging the board diagonally return chess_board[0][0] if (chess_board[0][2] == chess_board[1][1] == chess_board[2][0]): # Judging the board's anti-diagonal return chess_board[0][2] if isfull: return 'D' # If none of the above judgments are satisfied (neither win nor lose), but the board is also filled, then a draw is made else: return ' '
III. Overall code
# coding=utf-8import random MAX_ROW = 3 MAX_COL = 3 #array = ['0','0','0'] chess_board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] #[array] * 3 def init_cheaa_board(chess_board): for i in range(MAX_ROW): for j in range(MAX_COL): chess_board[i][j] = ' ' pass def print_chess_board(chess_board): print('*'+'-'*7+'*'+'-'*7+'*'+'-'*7+'*') for i in range(MAX_ROW): print('|'+' '*3+chess_board[i][0]+' '*3+'|'+' '*3+chess_board[i][1]+' '*3+'|'+' '*3+chess_board[i][2]+' '*3+'|') print('*' + '-' * 7 + '*' + '-' * 7 + '*' + '-' * 7 + '*') pass pass def player_first(chess_board): while(1): x = int(input('please input x:')) y = int(input('please input y:')) if(chess_board[x][y] != ' '): print('This position is already occupied!') 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 pass def chess_board_isfull(chess_board): # Determine if the board is full for i in range(MAX_ROW): if (' ' in chess_board[i]): return 0 return 1 pass def Win_or_lose(chess_board): isfull = chess_board_isfull(chess_board) for i in range(MAX_ROW): # Each column's judgment 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): # Each line of judgment 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]): # Judging the board diagonally return chess_board[0][0] if (chess_board[0][2] == chess_board[1][1] == chess_board[2][0]): # Judging the board's anti-diagonal return chess_board[0][2] if isfull: return 'D' # If none of the above judgments are satisfied (neither win nor lose), but the board is also filled, then a draw is made else: return ' ' def computer_second_random(chess_board): # The computer randomizes the moves while(1): x = (0,2) y = (0,2) if(chess_board[x][y] != ' '): continue else: chess_board[x][y] = 'T' break 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): # of players in each row who have made a move count += 1 (j) if(chess_board[j][i] == key): # of players in each column who have made a move #print('j'+str(j)+',i'+str(i)+'='+chess_board[j][i]) count1 += 1 (j) if (i == j and chess_board[j][i] == key): # Players falling in the main diagonal count2 += 1 (j) if(count == 2): # In each line, get the coordinates of specific locations that can be intercepted, excluding those that are already populated. result = list(set(allindex).difference(set(index))) result = result[0] if(chess_board[i][result] == ' '): # When this location is available for interception, make a coordinate return #return i,result intercept_index['x'] = i intercept_index['y'] = result return intercept_index #print(count1,'------->',index1) if (count1 == 2): # In each column, get the coordinates of specific locations that can be intercepted, excluding those that are already populated. result = list(set(allindex).difference(set(index1))) result = result[0] #print('count1==2,result:',result) if (chess_board[result][i] == ' '): # Coordinate return when this location is available for interception intercept_index['x'] = result intercept_index['y'] = i return intercept_index #return i, result if (count2 == 2): # On the main diagonal Get the coordinates of specific locations that can be intercepted Need to exclude already filled locations result = list(set(allindex).difference(set(index2))) result = result[0] if (chess_board[i][result] == ' '): # Coordinate return when this location is available for interception 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): # The computer is smart enough to play chess #First, check if the computer has two pawns. If it does, get the coordinates of the empty squares and make a move yourself. intercept_index = Intercept_player(chess_board, 'T') if (intercept_index['x'] == -1 and intercept_index['y'] == -1): pass else: # The computer can drop the ball x = intercept_index['x'] y = intercept_index['y'] chess_board[x][y] = 'T' return #2. If a player is about to make a move, intercept first. intercept_index = Intercept_player(chess_board,'U') #If the player has two pieces, get the coordinates of the empty square. #print('intercept_index---:') #print(intercept_index) if(intercept_index['x'] == -1 and intercept_index['y'] == -1): pass else: #Computerscanfall x = intercept_index['x'] y = intercept_index['y'] chess_board[x][y] = 'T' return #3. If not, the computer lines up the pieces to facilitate the formation of the game. #3.1, Occupy Center Position If Center Position [1,1] is not occupied if(chess_board[1][1] == ' '): chess_board[1][1] = 'T' return #3.2. Occupy the four corners if [0,0] [0,2] [2,0] [2,2] is not occupied. 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 of each side if [0,1] [1,0] [1,2] [2,1] is not occupied. 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' return def 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 board's not full. It's the computer's move. #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 victory!') 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('1---------------begin') print('2---------------exit') print('please select begin or exit') print('-' * 20) while(1): select = input('please input:') if select == '1': begin_games() pass elif select == '2': print('exit the game') break #pass pass if __name__ == "__main__": menu() pass
IV. Presentation of results
4.1 In the following screenshot, it is shown that the computer intercepts, occupies a favorable position, and takes the lead in the game
This is the whole content of this article.