华容道Python实现方法

import random

def creatList1(dimension):
    global li1
    if 1 <= dimension <= 10:
        li1 = [i for i in range(1,dimension**2)]
        li1.append(0)
        random.shuffle(li1)
        return li1
    elif dimension < 0 or dimension > 10:
        print('Wrong dimension interval.')
    else:
        print('Wrong input')

def judge(array):
    
    inversion = 0
    rowNumber = int(array.index(0) / dimension)
    index = array.index(0)
    array.remove(0)
    for i in range(dimension**2-1):
        for j in range(i,dimension**2-1):
            if array[i] > array[j]:
                inversion += 1
    array.insert(index,0)

    if dimension % 2 == 1:
        if inversion % 2 == 0:
            return True
        else:
            return False
    
    elif dimension % 2 == 0:
        
        if rowNumber % 2 == 1:
            if inversion % 2 == 0:
                return True
            else:
                return False
        if rowNumber % 2 == 0:
            if inversion % 2 == 1:
                return True
            else:
                return False

def printPuzzle(array):

    for i in range(dimension**2):
        if (i % dimension == 0):
            print('\n')
        if(array[i] == 0 ):
            print(" ",end = "\t")
            continue
        
        print (array[i],'\t',end='')

def motivation(li1,move):
    
    if move == right:
        if (position0 % dimension) == 0:
            print('wrong move')
        else:
            li1[position0] = li1[position0 - 1]
            li1[position0 - 1] = 0
    elif move == left:
        if (position0 % dimension) == (dimension - 1):
            print('wrong move')
        else:
            li1[position0] = li1[position0 + 1]
            li1[position0 +1] = 0
    elif move == down:
        if int(position0 / dimension) == 0:
            print('wrong move')
        else:
            li1[position0] = li1[position0 - dimension]
            li1[position0 - dimension] = 0
    elif move == up:
        if int(position0 / dimension) == (dimension - 1):
            print('wrong move')
        else:
            li1[position0] = li1[position0 + dimension]
            li1[position0 + dimension] = 0

def leftmovement(li1):
    canmove = ['up-w','left-a','right-d','down-s']
    if (position0 % dimension) == 0:
        canmove.remove('right-d')
    if (position0 % dimension) == (dimension - 1):
        canmove.remove('left-a')
    if int(position0 / dimension) == 0:
        canmove.remove('down-s')
    if int(position0 / dimension) == (dimension - 1):
        canmove.remove('up-w')
    return canmove

up = 'w'
left = 'a'
down = 's'
right = 'd'
li1 = []
print('Welcome to Sliding Puzzle game...')
dimension = int(input('Enter the desired dimension of the puzzle:'))
n = 0

while True:

    creatList1(dimension)
    if judge(li1):
        break

while True:
    position0 = li1.index(0)

    answer = [i for i in range(1,dimension**2)]
    answer.append(0)

    printPuzzle(li1)
    print('\n Enter your move','(',leftmovement(li1),')',end='')
    move = input(':')
    motivation(li1,move)
    n += 1
    
    if li1 == answer:
        print('Congratulations! You solved the puzzle in',n,'moves!')
        newgame = input('Enter \'n\' to start a new game or enter\'q\'to end the game:')
        if newgame == 'n':
            continue
        elif newgame == 'q':
            break
        else:
            print('Wrong input')
            break

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值