CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day07/tic-tac-toe.py
Views: 729
1
"""
2
井字棋游戏
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-06
7
"""
8
9
import os
10
11
12
13
def print_board(board):
14
print(board['TL'] + '|' + board['TM'] + '|' + board['TR'])
15
print('-+-+-')
16
print(board['ML'] + '|' + board['MM'] + '|' + board['MR'])
17
print('-+-+-')
18
print(board['BL'] + '|' + board['BM'] + '|' + board['BR'])
19
20
21
def main():
22
init_board = {
23
'TL': ' ', 'TM': ' ', 'TR': ' ',
24
'ML': ' ', 'MM': ' ', 'MR': ' ',
25
'BL': ' ', 'BM': ' ', 'BR': ' '
26
}
27
begin = True
28
while begin:
29
curr_board = init_board.copy()
30
begin = False
31
turn = 'x'
32
counter = 0
33
os.system('clear')
34
print_board(curr_board)
35
while counter < 9:
36
move = input('轮到%s走棋, 请输入位置: ' % turn)
37
if curr_board[move] == ' ':
38
counter += 1
39
curr_board[move] = turn
40
if turn == 'x':
41
turn = 'o'
42
else:
43
turn = 'x'
44
os.system('clear')
45
print_board(curr_board)
46
choice = input('再玩一局?(yes|no)')
47
begin = choice == 'yes'
48
49
50
if __name__ == '__main__':
51
main()
52
53