Author Topic: Simple locking puzzle  (Read 2997 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Simple locking puzzle
« on: July 12, 2021, 01:39:18 am »
Move the colored items off the screen, or flush with the white goalposts, by click & drag.
A demo of what can be done in less than a hundred lines. 
Code: QB64: [Select]
  1. Dim b(21, 14)
  2. For by = 1 To 20 '                                         initialize
  3.     Read d$
  4.     For bx = 1 To 13
  5.         b(by, bx) = Val(Mid$(d$, bx, 1))
  6.     Next bx
  7. Next by
  8. begin: '                                                   MAIN LOOP
  9. Locate 2, 2: Print "Move "; moves; '                       display
  10. For ty = 1 To 21
  11.     For tx = 1 To 14
  12.         Color b(ty, tx)
  13.         Locate 2 + ty, 18 + tx * 3
  14.         Print String$(3, 219);
  15.     Next tx
  16. Next ty
  17. x = 0: y = 0 '                                             get move
  18.     sx = _MouseX: sy = _MouseY
  19.     If (sx > 0) And (sx < 80) And (sy > 0) And (sy < 25) Then t = Screen(sy, sx, 1)
  20.     If (t > 0) And (t < 5) Then p = t
  21.     lft = _MouseButton(1)
  22.     While lft
  23.         While _MouseInput: Wend
  24.         dx = _MouseX - sx: dy = _MouseY - sy
  25.         If Abs(dx) > Abs(dy) Then x = (dx < 0) - (dx > 0)
  26.         If Abs(dy) > Abs(dx) Then y = (dy < 0) - (dy > 0)
  27.         If x Or y Then Exit Do
  28.         While _MouseInput: Wend
  29.         lft = _MouseButton(1)
  30.     Wend
  31. Loop Until (p > 0) And (x Or y)
  32. For by = 1 To 21 '                                         check if move is valid
  33.     For bx = 1 To 14
  34.         If p = b(by, bx) Then
  35.             tx = bx + x: ty = by + y
  36.             If (tx > 0) And (ty > 0) And (tx < 14) And (ty < 21) Then
  37.                 d = b(ty, tx)
  38.                 If (d <> p) And (d <> 0) Then GoTo begin
  39.             End If
  40.         End If
  41.     Next bx
  42. Next by
  43. moves = moves + 1 '                                        make the move
  44. v1 = 1: v2 = 21: v3 = 1: v4 = 14: v5 = 1
  45. If (x > 0) Or (y > 0) Then Swap v1, v2: Swap v3, v4: v5 = -v5
  46. For by = v1 To v2 Step v5
  47.     For bx = v3 To v4 Step v5
  48.         If p = b(by, bx) Then
  49.             tx = x + bx: ty = y + by
  50.             If (tx > 0) And (ty > 0) And (tx < 14) And (ty < 21) Then b(y + by, x + bx) = p
  51.             b(by, bx) = 0
  52.         End If
  53.     Next bx
  54. Next by
  55. For by = 8 To 17 '                                         check if done
  56.     For bx = 30 To 50
  57.         If Screen(by, bx, 1) Then GoTo begin
  58.     Next bx
  59. Next by
  60. Data "     111     "
  61. Data "      1      "
  62. Data "      1      "
  63. Data "      1      "
  64. Data "  74  11117  "
  65. Data "   4444441   "
  66. Data "   4 222411  "
  67. Data "   4  2441   "
  68. Data "4  4  2  1   "
  69. Data "44444 2111  2"
  70. Data "4 434 2122222"
  71. Data "  433 222   2"
  72. Data "  4 3 233    "
  73. Data "  4 3 223    "
  74. Data "    33333    "
  75. Data "  7    3  7  "
  76. Data "       3     "
  77. Data "       3     "
  78. Data "       3     "
  79. Data "      333    "
burr.png
* burr.png (Filesize: 3.98 KB, Dimensions: 405x350, Views: 170)
« Last Edit: July 12, 2021, 01:40:40 am by Richard Frost »
It works better if you plug it in.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simple locking puzzle
« Reply #1 on: July 12, 2021, 06:05:24 am »
:) Well thats almost as good as coffee for waking up (dont tell johnno56).

 
Puzzle by R Frost.PNG


Oh dang it counted moves, LOL, maybe better next time.

Thanks @Richard Frost

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Simple locking puzzle
« Reply #2 on: July 12, 2021, 08:48:21 am »
"Almost as good as coffee?" Not even close.... Moo Ha Ha...
Logic is the beginning of wisdom.