码迷,mamicode.com
首页 > 其他好文 > 详细

九宫格拼图

时间:2019-05-14 20:48:13      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:sys   class   alac   .sh   dom   exce   toolbar   __name__   random   

# 生成随机拼图
jigsaw = [chr(i) for i in range(65,74)]
random.shuffle(jigsaw)

palace = {}
for i in range(9):
if jigsaw[i] == ‘I‘:
palace[i+1]=([‘ ‘, i+1, 0])
else:
palace[i+1]=([jigsaw[i], i+1, 1])

# 测试用数据
# palace = {1: [‘A‘, 1, 1], 2: [‘B‘, 2, 1], 3: [‘C‘, 3, 1], 4: [‘D‘, 4, 1], 5: [‘E‘, 5, 1], 6: [‘F‘, 6, 1], 7: [‘G‘, 7, 1], 8: [‘ ‘, 8, 0], 9: [‘H‘, 9, 1]}

# 移动方块
def move_square(square, pledic):
in_switch = bool()
zero_switch = bool()

for k,v in pledic.items():
pledic[k][1] = k

# 玩家操作, 移动的方块(输入的字符)
if v[0] == square:
in_key = k
in_val = v
in_switch = True

# 找到字符所在, 并作标记
if v[2] == 0:
zero = v[1]
zero_k = k
zero_v = v
zero_switch =True

if zero_switch == in_switch:
# 与空方块交换位置
if zero % 3 == in_val[1] % 3 and abs(zero - in_val[1]) <= 3:
pledic[in_key], pledic[zero_k] = zero_v, in_val
if abs(zero - in_val[1]) == 1 and ((zero+in_val[1])-1)%3 != 0:
pledic[in_key], pledic[zero_k] = zero_v, in_val

return pledic

# 游戏检测
def check_palace(dic):
right_count = 0
if dic[9][2] == 0:
for k,v in dic.items():
if k == ord(v[0])-64:
right_count +=1
if right_count == 8:
return True
return False

# 输出
def print_palace(dic):
print(dic[1][0],dic[2][0],dic[3][0])
print(dic[4][0],dic[5][0],dic[6][0])
print(dic[7][0],dic[8][0],dic[9][0])

# 进行游戏
def play_palace(palace):
ple = deepcopy(palace)
print_palace(ple)
play_dic={}

while True:
try:
square = input(‘input A-H:‘)
if ord(square.upper()) >= 65 and ord(square.upper()) <= 74:
play_dic = move_square(square.upper(), ple)
print_palace(play_dic)
if check_palace(play_dic):
break
else:
print(‘Please again input‘)
except TypeError:
print(‘Please again input‘)
continue

print("All Right! You Win!!")
sys.exit()


if __name__==‘__main__‘:
play_palace(palace)
 

九宫格拼图

标签:sys   class   alac   .sh   dom   exce   toolbar   __name__   random   

原文地址:https://www.cnblogs.com/luchenhui/p/10864394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!