python day 26:
When i was in Partial Differential Equation class, my deskmate would take out a notebook and play five-in-a-row with me. Many years have passed by, I bearly recall anything about PDE, but i still remember the little definite joy when i was late to class, I knew my deskmate would have the board ready and waiting for me.
Today we will use Python to construct a classical game, Tic Tac Toe, which is an easier version of Five-in-a-row.
Lyo: Only double three counted as Winning for me.
四子不算赢,只有双三才算赢。我从来不靠别人看不到来赢。
Like all the chess-like games, Tic-Tac-Toe/Five-in-a-row has First-hand advantage, but i still struggle to pick go first or second.
Chinese always says, 人生如棋,落子无悔。
Life is like Chess, once set, no regret!
Up to now, I regret frequently about my decisions,
however, as time goes by, i think l also learned try to let the things be, and set myself free.
我慢慢学会放过我自己了!
Python Tic Tac Toe Code:
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 tit_tac_toe(): 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('%s Please make a move: ' % 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('One more?(yes|no)') begin = choice == 'yes'
tit_tac_toe()
Happy studying!
Ref: