1

I am new to programming and trying to create a sliding puzzle program without class and only standard python module. I have found a code from internet about the sliding puzzle game and tried to modify the board dimension and the input to move the numbers. The input to move the numbers should be in wasd, where w = slide/move the number up to empty space, s = move down, a = move left, and d = move right.

This is the code from internet that I already modify to custom the board size from 3x3 to 10x10. The input to move in this program is the number that is desired to move to the empty space.

import random, sys

n = input('Please input the board dimension: ')
n = int(n)

def board():
    '''Make matrix board of random numbers'''
    list1 = [*range(0, n**2, 1)]
    random.shuffle(list1)
    matrix = []
    while list1 !=[]:
        matrix.append(list1[:n])
        list1 = list1[n:]
    return matrix

def zero(board):
    '''function to find where the zero is'''
    empty_space = None
    for x,item in enumerate(board):
        for y,item in enumerate(board):
            if board[x][y] == 0:
                empty_space = (x,y)
    return empty_space

def draw_board(board):
    '''function to draw the board'''
    print('\n\t')
    for x,item in enumerate(board):
        for y,item in enumerate(board):
            if board[x][y] == 0:
                print('\t|  ' , end='')
            else:
                print('\t|  ' + '{:02d}' .format(board[x][y]), end=' ')
        print('\n\t')

def ask_number(board):
    ''' function to ask for the number to move'''
    num = input('\nplease type the number of the piece to move : ( q ) to quit  ')
    if num in ['q','Q']:
        print('\n\ngame over  ')
        sys.exit()
    num = int(num)
    piece = ()
    for i,item in enumerate(board):
        for j,item in enumerate(board):
            if num == board[i][j]:
                piece = (i,j)
    return piece , num

def game():
    '''Run the game logic'''
    matrix = board()
    empty_space = zero(matrix)
    game_on = True
    move = 0
    while game_on:
        draw_board(matrix)
        piece,num = ask_number(matrix)
        if num > n**2:
            print('illegal move , try again  ')
        else:
            if(empty_space==(piece[0]-1,piece[1]))\
            or(empty_space==(piece[0]+1,piece[1]))\
            or(empty_space==(piece[0],piece[1]-1))\
            or(empty_space==(piece[0],piece[1]+1)):
                matrix[empty_space[0]][empty_space[1]]=num
                matrix[piece[0]][piece[1]]=0
                empty_space=(piece[0],piece[1])
                move = move +1
                print()
                print('you have made ',move , 'moves so far ')
                print(2*'\n')
            else:
                print('illegal move , try again ')
                
if __name__ == '__main__':
    game()

However, when I finished programming for the input to move the numbers, my sliding puzzle board won't appear. This is the program after modifying the input:

import random, sys

n = input('Please input the board dimension: ')
n = int(n)

def board():
    '''Make matrix board of random numbers'''
    list1 = [*range(0, n**2, 1)]
    random.shuffle(list1)
    matrix = []
    while list1 !=[]:
        matrix.append(list1[:n])
        list1 = list1[n:]
    return matrix

def zero(board):
    '''function to find where the zero is'''
    empty_space = None
    for x,item in enumerate(board):
        for y,item in enumerate(board):
            if board[x][y] == 0:
                empty_space = (x,y)
    return empty_space

def draw_board(board):
    '''function to draw the board'''
    print('\n\t')
    for x,item in enumerate(board):
        for y,item in enumerate(board):
            if board[x][y] == 0:
                print('\t|  ' , end='')
            else:
                print('\t|  ' + '{:02d}' .format(board[x][y]), end=' ')
        print('\n\t')

def make_move(board):
    ''' function to ask for the number to switch'''
    switch = input('\nplease type the direction of the piece to switch : ( q ) to quit  ')
    switch = str(switch)
    if switch in ['q','Q']:
        print('\n\ngame over  ')
        sys.exit()
    empty_space = zero(board)
    piece = ()
    for i,item in enumerate(board):
        for j,item in enumerate(board):
            matrix = board()
            empty_space = zero(matrix)
            empty_space_nearby = ()
    return piece , empty_space_nearby , switch

def game():
    '''Run the game logic'''
    matrix = board()
    game_on = True
    move = 0
    while game_on:
        make_move(board)
        draw_board(matrix)
        piece,empty_space_nearby,switch = make_move(matrix)
        if(switch != 'w')\
            or(switch != 'a')\
            or(switch != 's')\
            or(switch != 'd'):
            print('illegal move , try again  ')
        else:
            if switch == 'a':
                (empty_space==(piece[0]-1,piece[1]))
                matrix[empty_space[0]][empty_space[1]]=empty_space_nearby
                matrix[piece[0]][piece[1]]=0
                empty_space=(piece[0],piece[1])
                move = move +1
                print()
                print('you have made ',move , 'moves so far ')
                print(2*'\n')
            elif switch == 'd':
                (empty_space==(piece[0]+1,piece[1]))
                matrix[empty_space[0]][empty_space[1]]=empty_space_nearby
                matrix[piece[0]][piece[1]]=0
                empty_space=(piece[0],piece[1])
                move = move +1
                print()
                print('you have made ',move , 'moves so far ')
                print(2*'\n')
            elif switch == 's':
                (empty_space==(piece[0],piece[1]-1))
                matrix[empty_space[0]][empty_space[1]]=empty_space_nearby
                matrix[piece[0]][piece[1]]=0
                empty_space=(piece[0],piece[1])
                move = move +1
                print()
                print('you have made ',move , 'moves so far ')
                print(2*'\n')
            elif switch == 'w':
                (empty_space==(piece[0],piece[1]+1))
                matrix[empty_space[0]][empty_space[1]]=empty_space_nearby
                matrix[piece[0]][piece[1]]=0
                empty_space=(piece[0],piece[1])
                move = move +1
                print()
                print('you have made ',move , 'moves so far ')
                print(2*'\n')
            else:
                print('illegal move , try again ')
                
if __name__ == '__main__':
    game()

Are there any ways to fix this problem without using pygames or class? Thank you in advance!

i'm-anon
  • 11
  • 3

0 Answers0