3x3 Shift Puzzle not fully finished

 avatar
DillFire55
python
23 days ago
1.4 kB
0
Indexable
Never
import random

board = ["1", "2", "3", "4", "5", "6", "7", "8",]

random.shuffle(board)

board.append(".")

print(board[0], end=' ')
print(board[1], end=' ')
print(board[2])
print(board[3], end=' ')
print(board[4], end=' ')
print(board[5])
print(board[6], end=' ')
print(board[7], end=' ')
print(board[8])

while True:
    while True:
        move = input("Enter the number you want to move: ")
        if move == "1" or move == "2" or move == "3" or move == "4" or move == "5" or move == "6" or move == "7" or move == "8":
            break

    num_index = board.index(move)
    dot_index = board.index(".")
    
    if num_index + 1 == dot_index or num_index - 1 == dot_index or num_index + 3 == dot_index or num_index - 3 == dot_index:
        board[num_index], board[dot_index] = board[dot_index], board[num_index]
    else:
        print("Invalid move!")

    print(board[0], end=' ')
    print(board[1], end=' ')
    print(board[2])
    print(board[3], end=' ')
    print(board[4], end=' ')
    print(board[5])
    print(board[6], end=' ')
    print(board[7], end=' ')
    print(board[8])

    if board[0] == "1" and board[1] == "2" and board[2] == "3" and board[3] == "4" and board[4] == "5" and board[5] == "6" and board[6] == "7" and board[7] == "8" and board[8] == ".":
        print("You won!")
        break
Leave a Comment