|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
from random import shuffle
lst = list(range(9))
shuffle(lst)
def pr():
global lst
bl = [[0]*3 for i in range(3)]
for i in range(3):
for j in range(3):
bl[i][j]=str(lst[3*i+j]) if lst[3*i+j] != 0 else ' '
print (' '.join(bl[i]))
def mov(n):
global lst
d={0:(1,3),1:(0,2,4),2:(1,5),
3:(0,4,6),4:(1,3,5,7),5:(2,4,8),
6:(3,7),7:(4,6,8),8:(5,7)}
for i in range(9):
if not lst[i]:
zero = i
if zero in d[lst.index(n)]:
lst[lst.index(n)],lst[zero] = lst[zero],lst[lst.index(n)]
else:
print ('wrong input!')
def win():
global lst
return True if lst[:8] == list(range(1,9)) else False
print ('Welcome to play 9 blocks!')
pr()
while True:
n = int(input('Move which number?'))
mov(n)
pr()
if win():
print ('You win!')
break |
|