Python井字游戏
2024-10-21 14:37:31
import sys def print_board():
for i in range(3):
for j in range(3):
print map[2 - i][j],
if j != 2:
print '|',
print '' def check_done():
for i in range(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 turn = 'X'
done = False
map = [[' ', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' ']]
print "Please select position by typing in a number between 1 and 9, see below for which number that is which position..."
print "7|8|9"
print "4|5|6"
print "1|2|3"
print_board()
while done != True:
print turn + "'s turn"
try:
pos = input("Select: ")
if 1 <= pos <= 9:
X = (pos - 1) / 3
Y = (pos - 1) % 3
if map[X][Y] == ' ':
map[X][Y] = turn
print_board()
done = check_done()
if done == False:
if turn == 'X':
turn = 'O'
else:
turn = 'X'
except:
print "You need to add a numeric value"
最新文章
- Xcode工作区间xxxx.xcworkspace不包含xxxx.xcodeproj
- 临时更换hadoop-ugi
- Oracle基本sql操作
- UVALive 6577 Binary Tree 二叉树的LRU串
- kail ip配置
- 简述Seesion和Cookie
- TCP快速重传和快速恢复
- poptest老李谈数据结构中深度优先和广度优先
- codeforces Gym - 100633J Ceizenpok’s formula
- DD XOFT虚拟键盘鼠标
- Python3 解析XML 层序遍历二叉树
- 1-1Controller之Request
- Safari-IoS调试
- Linux环境编程之同步(四):Posix信号量
- Html生成控件
- [ACM]Codeforces Round #534 (Div. 2)
- 从零开始玩转JMX(四)——Apache Commons Modeler &; Dynamic MBean
- HDU3047 Zjnu Stadium 【带权并查集】
- 关于在ASP.NET MVC 中使用EF的Code First的方式来读取数据库时的Validation failed for one or more entities. See &#39;EntityValidationErrors&#39; property for more details.
- 【Tarjan算法】【DFS】Petrozavodsk Summer Training Camp 2016 Day 9: AtCoder Japanese Problems Selection, Thursday, September 1, 2016 Problem B. Point Pairs