The examples in this article share the specific code of Python to implement the Tic-Tac-Toe game for your reference. The specific content is as follows
import os
def print_board(board):print(board['TL']+'|'+ board['TM']+'|'+ board['TR'])print('-+-+-')print(board['ML']+'|'+ board['MM']+'|'+ board['MR'])print('-+-+-')print(board['BL']+'|'+ board['BM']+'|'+ board['BR'])
def main():
init_board ={'TL':' ','TM':' ','TR':' ','ML':' ','MM':' ','MR':' ','BL':' ','BM':' ','BR':' '}
begin = True
while begin:
curr_board = init_board.copy()
begin = False
turn ='x'
counter =0
os.system('clear')print_board(curr_board)while counter <9:
move =input('Turn%s move,Please enter location: '% turn)if curr_board[move]==' ':
counter +=1
curr_board[move]= turn
if turn =='x':
turn ='o'else:
turn ='x'
os.system('clear')print_board(curr_board)
choice =input('Play another round?(yes|no)')
begin = choice =='yes'if __name__ =='__main__':main()
Effect picture:
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts