python华容道游戏代码
时间: 2023-12-01 17:41:18 浏览: 82
以下是一个简单的Python华容道游戏代码,其中使用了列表来存储游戏数据:
```python
# 初始化游戏数据
data = [1, 2, 3, 4, 5, 6, 7, 8, 0]
# 打印游戏界面
def speak():
print(data[0], data[1], data[2])
print(data[3], data[4], data[5])
print(data[6], data[7], data[8])
# 判断游戏是否结束
def isCorrect():
for i in range(8):
if data[i] != i + 1:
return False
return True
# 移动游戏数据
def move():
x = int(input("请输入要移动的数字:"))
index = data.index(x)
if index == 0:
if data[1] == 0:
data[1], data[0] = data[0], data[1]
elif data[3] == 0:
data[3], data[0] = data[0], data[3]
elif index == 1:
if data[0] == 0:
data[1], data[0] = data[0], data[1]
elif data[2] == 0:
data[2], data[1] = data[1], data[2]
elif data[4] == 0:
data[4], data[1] = data[1], data[4]
elif index == 2:
if data[1] == 0:
data[2], data[1] = data[1], data[2]
elif data[5] == 0:
data[5], data[2] = data[2], data[5]
elif index == 3:
if data[0] == 0:
data[3], data[0] = data[0], data[3]
elif data[4] == 0:
data[4], data[3] = data[3], data[4]
elif data[6] == 0:
data[6], data[3] = data[3], data[6]
elif index == 4:
if data[1] == 0:
data[4], data[1] = data[1], data[4]
elif data[3] == 0:
data[4], data[3] = data[3], data[4]
elif data[5] == 0:
data[4], data[5] = data[5], data[4]
elif data[7] == 0:
data[7], data[4] = data[4], data[7]
elif index == 5:
if data[2] == 0:
data[5], data[2] = data[2], data[5]
elif data[4] == 0:
data[5], data[4] = data[4], data[5]
elif data[8] == 0:
data[8], data[5] = data[5], data[8]
elif index == 6:
if data[3] == 0:
data[6], data[3] = data[3], data[6]
elif data[7] == 0:
data[7], data[6] = data[6], data[7]
elif index == 7:
if data[4] == 0:
data[7], data[4] = data[4], data[7]
elif data[6] == 0:
data[7], data[6] = data[6], data[7]
elif data[8] == 0:
data[8], data[7] = data[7], data[8]
elif index == 8:
if data[5] == 0:
data[8], data[5] = data[5], data[8]
elif data[7] == 0:
data[8], data[7] = data[7], data[8]
# 开始游戏
def play():
speak()
while(isCorrect()!=True):
move()
speak()
print("成功")
play()
```
阅读全文