How to resolve the algorithm 15 puzzle game step by step in the 11l programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 15 puzzle game step by step in the 11l programming language

Table of Contents

Problem Statement

Implement the Fifteen Puzzle Game.

The   15-puzzle   is also known as:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 15 puzzle game step by step in the 11l programming language

Source code in the 11l programming language

T Puzzle
   position = 0
   [Int = String] items

   F main_frame()
      V& d = .items
      print(‘+-----+-----+-----+-----+’)
      print(‘|#.|#.|#.|#.|’.format(d[1], d[2], d[3], d[4]))
      print(‘+-----+-----+-----+-----+’)
      print(‘|#.|#.|#.|#.|’.format(d[5], d[6], d[7], d[8]))
      print(‘+-----+-----+-----+-----+’)
      print(‘|#.|#.|#.|#.|’.format(d[9], d[10], d[11], d[12]))
      print(‘+-----+-----+-----+-----+’)
      print(‘|#.|#.|#.|#.|’.format(d[13], d[14], d[15], d[16]))
      print(‘+-----+-----+-----+-----+’)

   F format(=ch)
      ch = ch.trim(‘ ’)
      I ch.len == 1
         R ‘  ’ch‘  ’
      E I ch.len == 2
         R ‘  ’ch‘ ’
      E
         assert(ch.empty)
         R ‘     ’

   F change(=to)
      V fro = .position
      L(a, b) .items
         I b == .format(String(to))
            to = a
            L.break
      swap(&.items[fro], &.items[to])
      .position = to

   F build_board(difficulty)
      L(i) 1..16
         .items[i] = .format(String(i))
      V tmp = 0
      L(a, b) .items
         I b == ‘  16 ’
            .items[a] = ‘     ’
            tmp = a
            L.break
      .position = tmp
      Int diff
      I difficulty == 0
         diff = 10
      E I difficulty == 1
         diff = 50
      E
         diff = 100
      L 0 .< diff
         V lst = .valid_moves()
         [Int] lst1
         L(j) lst
            lst1.append(Int(j.trim(‘ ’)))
         .change(lst1[random:(lst1.len)])

   F valid_moves()
      V pos = .position
      I pos C [6, 7, 10, 11]
         R [.items[pos - 4], .items[pos - 1], .items[pos + 1], .items[pos + 4]]
      E I pos C [5, 9]
         R [.items[pos - 4], .items[pos + 4], .items[pos + 1]]
      E I pos C [8, 12]
         R [.items[pos - 4], .items[pos + 4], .items[pos - 1]]
      E I pos C [2, 3]
         R [.items[pos - 1], .items[pos + 1], .items[pos + 4]]
      E I pos C [14, 15]
         R [.items[pos - 1], .items[pos + 1], .items[pos - 4]]
      E I pos == 1
         R [.items[pos + 1], .items[pos + 4]]
      E I pos == 4
         R [.items[pos - 1], .items[pos + 4]]
      E I pos == 13
         R [.items[pos + 1], .items[pos - 4]]
      E
         assert(pos == 16)
         R [.items[pos - 1], .items[pos - 4]]

   F game_over()
      V flag = 0B
      L(a, b) .items
         I b != ‘     ’
            I a == Int(b.trim(‘ ’))
               flag = 1B
            E
               flag = 0B
      R flag

V g = Puzzle()
g.build_board(Int(input("Enter the difficulty : 0 1 2\n2 => highest 0 => lowest\n")))
g.main_frame()
print(‘Enter 0 to exit’)
L
   print("Hello user:\nTo change the position just enter the no. near it")
   V lst = g.valid_moves()
   [Int] lst1
   L(i) lst
      lst1.append(Int(i.trim(‘ ’)))
      print(i.trim(‘ ’)" \t", end' ‘’)
   print()
   V x = Int(input())
   I x == 0
      L.break
   E I x !C lst1
      print(‘Wrong move’)
   E
      g.change(x)
   g.main_frame()
   I g.game_over()
      print(‘You WON’)
      L.break

  

You may also check:How to resolve the algorithm Call an object method step by step in the VBA programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Janet programming language
You may also check:How to resolve the algorithm Semiprime step by step in the Phix programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Lua programming language
You may also check:How to resolve the algorithm Langton's ant step by step in the PureBasic programming language