- def print_board():
- print '*'*50
- for i in range(0,3):
- for j in range(0,3):
- if map[i][j] != " ":
- print map[i][j],
- else:
- print "%d" %(i*3+j+1),
- if j != 2:
- print "|",
- print ""
- print '*'*50
- def check_done():
- for i in range(0,3):
- if map[i][0] == map[i][1] == map[i][2] != " " \\
- or map[0][i] == map[1][i] == map[2][i] != " ":
- print turn, "won!!!"
- return True
- if map[0][0] == map[1][1] == map[2][2] != " " \\
- or map[0][2] == map[1][1] == map[2][0] != " ":
- print turn, "won!!!"
- return True
- if " " not in map[0] and " " not in map[1] and " " not in map[2]:
- print "Draw"
- return True
- return False
- def help():
- print "*" *76
- print "*" *76
- print "** Welcome to the tic-tac world,it's modified by me(hunter xue). **"
- print "** the origin source is here: **"
- print "** \\"http://jiabin.tk/2013/07/22/tic-tac-toe-in-python/\\" **"
- print "** Hope you enjoy this simple game **"
- print "*" *76
- print "*" *76
- turn = "X"
- map = [[" "," "," "],
- [" "," "," "],
- [" "," "," "]]
- done = False
- help()
- while done != True:
- print_board()
- print turn, "'s turn=>",
- moved = False
- while moved != True:
- try:
- pos = input("Select: ")
- if pos <=9 and pos >=1:
- Y = pos/3
- X = pos%3
- if X != 0:
- X -=1
- else:
- X = 2
- Y -=1
- if map[Y][X] == " ":
- map[Y][X] = turn
- moved = True
- done = check_done()
- if done == False:
- if turn == "X":
- turn = "O"
- else:
- turn = "X"
- else:
- print "The position is occupied by ",turn
- else:
- print "You need to add a numeric value between 1-9"
- except:
- print "You need to add a numeric value between 1-9"
- #该片段来自于http://www.codesnippet.cn/detail/291020136748.html
来源: http://www.codesnippet.cn/detail/291020136748.html