Python with Tic Tac Toe

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()
 | | 
-+-+-
 | | 
-+-+-
 | |
 | | 
-+-+-
 |x| 
-+-+-
 | |
 | | 
-+-+-
o|x| 
-+-+-
 | |
 | | 
-+-+-
o|x|x
-+-+-
 | |
o| | 
-+-+-
o|x|x
-+-+-
 | |
o| | 
-+-+-
o|x|x
-+-+-
x| |
o| |o
-+-+-
o|x|x
-+-+-
x| |
o|x|o
-+-+-
o|x|x
-+-+-
x| |
o|x|o
-+-+-
o|x|x
-+-+-
x|o|
o|x|o
-+-+-
o|x|x
-+-+-
x|o|x

Happy studying!

Ref:

https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/07.%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%92%8C%E5%B8%B8%E7%94%A8%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.md

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Social media & sharing icons powered by UltimatelySocial
%d bloggers like this: