In [1]:
# sliding tiles puzzle

import random
In [2]:
# define the print_board function
def print_board(board):
    
    board_size = len(board)
    
    print('The current board is')
    for i in range(board_size):
        for j in range(board_size):
            print(board[i][j],'',end='')
        print('')
In [3]:
# define the initialize_board function
def initialize_board(board):
    
    board_size = len(board)
    num_list = list(range(1,board_size*board_size))
    
    random.shuffle(num_list)
    num_list.append(0)
    
    index = 0
    for i in range(board_size):
        for j in range(board_size):
            board[i][j] = num_list[index]
            index += 1
In [4]:
# define the is_ended function
def is_ended(board,sol_board):
    
    is_end = True
    
    board_size = len(board)
    
    for i in range(board_size):
        for j in range(board_size):
            if board[i][j] != sol_board[i][j]:
                is_end = False
                break
                
    return is_end
In [5]:
# define the is_valid_move function
def is_valid_move(board,move):
    
    is_valid = True
    board_size = len(board)
    
    # find the zero location
    row_index = 0
    col_index = 0
    
    for i in range(board_size):
        for j in range(board_size):
            if board[i][j] == 0:
                row_index = i
                col_index = j
                break
    
    if move != 'u' and move != 'd' and move != 'l' and move != 'r':
        is_valid = False
    elif row_index == 0 and move == 'd':
        is_valid = False
    elif row_index == board_size-1 and move == 'u':
        is_valid = False
    elif col_index == 0 and move == 'r':
        is_valid = False
    elif col_index == board_size-1 and move == 'l':
        is_valid = False
        
    return is_valid
In [6]:
# define the change_board function
def change_board(board,move):
    
    board_size = len(board)
    
    row_index = 0
    col_index = 0
    for i in range(board_size):
        for j in range(board_size):
            if board[i][j] == 0:
                row_index = i
                col_index = j
                break
                
    if move == 'u':
        board[row_index][col_index] = board[row_index+1][col_index]
        board[row_index+1][col_index] = 0
    elif move == 'd':
        board[row_index][col_index] = board[row_index-1][col_index]
        board[row_index-1][col_index] = 0
    elif move == 'l':
        board[row_index][col_index] = board[row_index][col_index+1]
        board[row_index][col_index+1] = 0
    elif move == 'r':
        board[row_index][col_index] = board[row_index][col_index-1]
        board[row_index][col_index-1] = 0
In [7]:
# define the main function
def main():

    print('Welcome to the Sliding Tiles Puzzle V1.0')
    
    board_size = 3  # size of the board
    board = [[0,0,0],
             [0,0,0],
             [0,0,0]] # board
    
    sol_board = [[1,2,3],
                 [4,5,6],
                 [7,8,0]] # the final board
    
    num_moves = 0
    move = ''  # input from user
    
    # initialize the game
    initialize_board(board)
                
    while not is_ended(board,sol_board):
    
        # print the current board
        print_board(board)
    
        move = input("Enter your move ('u','d','l','r'): ")
        
        while not is_valid_move(board,move):
            print('Invalid move! please try again.')
            move = input("Enter your move ('u','d','l','r'): ")
        
        num_moves += 1
        change_board(board,move)
        
    print('Congratulation!')
    print('Number of moves : ', num_moves)
In [ ]:
# call the main function
main()
Welcome to the Sliding Tiles Puzzle V1.0
The current board is
6 5 3 
7 4 1 
2 8 0 
Enter your move ('u','d','l','r'): r
The current board is
6 5 3 
7 4 1 
2 0 8 
Enter your move ('u','d','l','r'): r
The current board is
6 5 3 
7 4 1 
0 2 8 
Enter your move ('u','d','l','r'): u
Invalid move! please try again.
Enter your move ('u','d','l','r'): d
The current board is
6 5 3 
0 4 1 
7 2 8 
In [ ]:
 
In [ ]: